Coverage for gws-app/gws/lib/htmlx/__init__.py: 97%
30 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 22:59 +0200
1import html
3import gws
4import gws.lib.osx
5import gws.lib.uom
8def escape(s: str, quote=True) -> str:
9 """Escapes a string for use in HTML."""
10 return html.escape(s, quote=quote)
13def render_to_pdf(html: str, out_path: str, page_size: gws.UomSize = None, page_margin: gws.UomExtent = None) -> str:
14 """Renders an HTML string to a PDF file.
16 Args:
17 html: The HTML content to be converted into a PDF.
18 out_path: The output file path for the generated PDF.
19 page_size: The size of the page in user-defined units. Defaults to None.
20 page_margin: The margins of the page in user-defined units. Defaults to None.
22 Returns:
23 The output file path of the generated PDF.
24 """
25 mar = page_margin or (0, 0, 0, 0, gws.Uom.mm)
27 # Page sizes need to be in mm.
28 psz = (210, 297, gws.Uom.mm)
29 if page_size:
30 psz = gws.lib.uom.size_to_mm(page_size, gws.lib.uom.PDF_DPI)
32 gws.u.write_file(out_path + '.html', html)
34 cmd = [
35 'wkhtmltopdf',
36 '--disable-javascript',
37 '--disable-smart-shrinking',
38 '--load-error-handling',
39 'ignore',
40 '--enable-local-file-access',
41 '--dpi',
42 _int_str(gws.lib.uom.PDF_DPI),
43 '--margin-top',
44 _int_str(mar[0]),
45 '--margin-right',
46 _int_str(mar[1]),
47 '--margin-bottom',
48 _int_str(mar[2]),
49 '--margin-left',
50 _int_str(mar[3]),
51 '--page-width',
52 _int_str(psz[0]),
53 '--page-height',
54 _int_str(psz[1]),
55 'page',
56 out_path + '.html',
57 out_path,
58 ]
60 gws.lib.osx.run(cmd)
61 return out_path
64def render_to_png(html: str, out_path: str, page_size: gws.UomSize = None, page_margin: list[int] = None) -> str:
65 """Renders an HTML string to a PNG image.
67 Args:
68 html: The HTML content to be converted into an image.
69 out_path: The output file path for the generated PNG.
70 page_size: The size of the image in user-defined units. Defaults to None.
71 page_margin: The margins of the image in pixels (top, right, bottom, left). Defaults to None.
73 Returns:
74 The output file path of the generated PNG.
75 """
76 if page_margin:
77 mar = page_margin
78 html = f"""
79 <body style="margin:{mar[0]}px {mar[1]}px {mar[2]}px {mar[3]}px">
80 {html}
81 </body>
82 """
84 gws.u.write_file(out_path + '.html', html)
86 cmd = ['wkhtmltoimage']
88 if page_size:
89 # Page sizes need to be in pixels.
90 psz = gws.lib.uom.size_to_px(page_size, gws.lib.uom.PDF_DPI)
91 w, h, _ = psz
92 cmd.extend(
93 [
94 '--width',
95 _int_str(w),
96 '--height',
97 _int_str(h),
98 '--crop-w',
99 _int_str(w),
100 '--crop-h',
101 _int_str(h),
102 ]
103 )
105 cmd.extend(
106 [
107 '--disable-javascript',
108 '--disable-smart-width',
109 '--transparent',
110 '--enable-local-file-access',
111 out_path + '.html',
112 out_path,
113 ]
114 )
116 gws.lib.osx.run(cmd)
117 return out_path
120def _int_str(x) -> str:
121 return str(int(x))