Coverage for cc_modules/cc_pdf.py: 50%

10 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-15 14:23 +0100

1""" 

2camcops_server/cc_modules/cc_pdf.py 

3 

4=============================================================================== 

5 

6 Copyright (C) 2012, University of Cambridge, Department of Psychiatry. 

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

8 

9 This file is part of CamCOPS. 

10 

11 CamCOPS is free software: you can redistribute it and/or modify 

12 it under the terms of the GNU General Public License as published by 

13 the Free Software Foundation, either version 3 of the License, or 

14 (at your option) any later version. 

15 

16 CamCOPS is distributed in the hope that it will be useful, 

17 but WITHOUT ANY WARRANTY; without even the implied warranty of 

18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

19 GNU General Public License for more details. 

20 

21 You should have received a copy of the GNU General Public License 

22 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>. 

23 

24=============================================================================== 

25 

26**PDF functions.** 

27 

28""" 

29 

30# ============================================================================= 

31# Imports 

32# ============================================================================= 

33 

34from typing import Any, Dict, TYPE_CHECKING 

35 

36from cardinal_pythonlib.pdf import get_pdf_from_html 

37 

38from camcops_server.cc_modules.cc_constants import ( 

39 PDF_ENGINE, 

40 WKHTMLTOPDF_OPTIONS, 

41) 

42 

43if TYPE_CHECKING: 

44 from camcops_server.cc_modules.cc_request import CamcopsRequest 

45 

46 

47# ============================================================================= 

48# pdf_from_html 

49# ============================================================================= 

50 

51 

52def pdf_from_html( 

53 req: "CamcopsRequest", 

54 html: str, 

55 header_html: str = None, 

56 footer_html: str = None, 

57 extra_wkhtmltopdf_options: Dict[str, Any] = None, 

58) -> bytes: 

59 """ 

60 Create and return a PDF from the HTML provided. 

61 """ 

62 extra_wkhtmltopdf_options = ( 

63 extra_wkhtmltopdf_options or {} 

64 ) # type: Dict[str, Any] 

65 wkhtmltopdf_options = dict( 

66 WKHTMLTOPDF_OPTIONS, **extra_wkhtmltopdf_options 

67 ) 

68 cfg = req.config 

69 return get_pdf_from_html( 

70 html, 

71 header_html=header_html, 

72 footer_html=footer_html, 

73 processor=PDF_ENGINE, 

74 wkhtmltopdf_filename=cfg.wkhtmltopdf_filename, 

75 wkhtmltopdf_options=wkhtmltopdf_options, 

76 )