gws.lib.htmlx¶
HTML utilities.
Escapes strings for HTML and renders HTML documents to PDF or PNG files with the
external wkhtmltopdf and wkhtmltoimage tools. The HTML is first written to
<out_path>.html, which is left in place, and the tool converts it to out_path.
JavaScript is disabled and local files can be loaded.
Example:
html = f'<h1>{gws.lib.htmlx.escape(title)}</h1>'
gws.lib.htmlx.render_to_pdf(html, '/tmp/out.pdf', page_size=(210, 297, gws.Uom.mm))
Source code: gws.lib.htmlx
Package Contents¶
- gws.lib.htmlx.escape(s: str, quote=True) str¶
Escape a string for use in HTML.
- Parameters:
s – A string.
quote – If
True, also escape double and single quotes.
- Returns:
The escaped string.
- gws.lib.htmlx.render_to_pdf(html: str, out_path: str, page_size: gws.UomSize = None, page_margin: gws.UomExtent = None) str¶
Render an HTML string to a PDF file with
wkhtmltopdf.- Parameters:
html – HTML content.
out_path – Path of the PDF file to create.
page_size – Page size, converted to mm. A4 portrait by default.
page_margin – Page margins (top, right, bottom, left). The values are passed to
wkhtmltopdfas millimetres, the unit is not converted. No margins by default.
- Returns:
The output path.
- Raises:
gws.lib.osx.Error – If the command fails.
- gws.lib.htmlx.render_to_png(html: str, out_path: str, page_size: gws.UomSize = None, page_margin: list[int] = None) str¶
Render an HTML string to a PNG image with
wkhtmltoimage.The image has a transparent background.
- Parameters:
html – HTML content.
out_path – Path of the PNG file to create.
page_size – Image size, converted to pixels at
gws.lib.uom.PDF_DPI. If set, the image is cropped to this size.page_margin – Margins in pixels (top, right, bottom, left), applied as a body style.
- Returns:
The output path.
- Raises:
gws.lib.osx.Error – If the command fails.