Coverage for cc_modules/cc_pdf.py: 55%
11 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
1#!/usr/bin/env python
3"""
4camcops_server/cc_modules/cc_pdf.py
6===============================================================================
8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
9 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
11 This file is part of CamCOPS.
13 CamCOPS is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
18 CamCOPS is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
26===============================================================================
28**PDF functions.**
30"""
32# =============================================================================
33# Imports
34# =============================================================================
36from typing import Any, Dict, TYPE_CHECKING
38from cardinal_pythonlib.pdf import get_pdf_from_html
40from camcops_server.cc_modules.cc_constants import (
41 PDF_ENGINE,
42 WKHTMLTOPDF_OPTIONS,
43)
45if TYPE_CHECKING:
46 from camcops_server.cc_modules.cc_request import CamcopsRequest
49# =============================================================================
50# pdf_from_html
51# =============================================================================
54def pdf_from_html(
55 req: "CamcopsRequest",
56 html: str,
57 header_html: str = None,
58 footer_html: str = None,
59 extra_wkhtmltopdf_options: Dict[str, Any] = None,
60) -> bytes:
61 """
62 Create and return a PDF from the HTML provided.
63 """
64 extra_wkhtmltopdf_options = (
65 extra_wkhtmltopdf_options or {}
66 ) # type: Dict[str, Any]
67 wkhtmltopdf_options = dict(
68 WKHTMLTOPDF_OPTIONS, **extra_wkhtmltopdf_options
69 )
70 cfg = req.config
71 return get_pdf_from_html(
72 html,
73 header_html=header_html,
74 footer_html=footer_html,
75 processor=PDF_ENGINE,
76 wkhtmltopdf_filename=cfg.wkhtmltopdf_filename,
77 wkhtmltopdf_options=wkhtmltopdf_options,
78 )