save_html_as_pdf

pyhelpers.store.save_html_as_pdf(data, path_to_file, if_exists='replace', page_size='A4', zoom=1.0, encoding='UTF-8', wkhtmltopdf_options=None, wkhtmltopdf_path=None, verbose=False, print_kwargs=None, raise_error=False, **kwargs)[source]

Save a web page or local HTML document as a PDF file using wkhtmltopdf.

This function wraps the pdfkit library to convert a URL or a local HTML file into a PDF. It automatically handles executable discovery, conditional overwriting and verbose logging.

Parameters:
  • data (str | os.PathLike) – The URL of a web page or the path to a local HTML file.

  • path_to_file (str | os.PathLike) – The destination path where the PDF file will be saved.

  • if_exists (str) – Action to take if the target PDF file already exists. Options are 'replace' (default) or 'pass'.

  • page_size (str) – The page size configuration. Defaults to 'A4'.

  • zoom (float) – Magnification scale for zooming in or out. Defaults to 1.0.

  • encoding (str) – The character encoding format. Defaults to 'UTF-8'.

  • wkhtmltopdf_options (dict | None) – Options for wkhtmltopdf; defaults to None. Refer to the documentation of the pdfkit project for more details.

  • wkhtmltopdf_path (str | None) – The path to the wkhtmltopdf executable. If None (default), the function searches standard installation paths, e.g. “C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe” on Windows.

  • verbose (bool | int) – Whether to print progress to the console; defaults to False. Set to 2 to see full diagnostic output from the wkhtmltopdf executable.

  • print_kwargs (dict | None) – Additional keyword arguments passed to pyhelpers.store._check_saving_path(). Defaults to None.

  • raise_error (bool) – Whether to raise an exception on failure; defaults to False.

  • kwargs – Additional parameters passed to pdfkit.from_url() or pdfkit.from_file().

Returns:

None

Return type:

None

Examples:

>>> from pyhelpers.store import save_html_as_pdf
>>> from pyhelpers.dirs import cd
>>> import subprocess

>>> pdf_pathname = cd("tests", "documents", "pyhelpers.pdf")
>>> web_page_url = 'https://pyhelpers.readthedocs.io/en/latest/'

>>> save_html_as_pdf(web_page_url, pdf_pathname)
>>> subprocess.Popen(str(pdf_pathname), shell=True)  # Open the PDF file

>>> # Close the PDF file (if opened with Foxit Reader)
>>> # subprocess.call("taskkill /f /im FoxitPDFReader.exe", shell=True)
>>> wkhtmltopdf_options = {'margin-top': '0', 'orientation': 'Landscape'}

>>> # Using custom options for margins and orientation
>>> save_html_as_pdf(
...     web_page_url, pdf_pathname,
...     wkhtmltopdf_options=wkhtmltopdf_options, verbose=True
... )
>>> subprocess.Popen(str(pdf_pathname), shell=True)

>>> # subprocess.call("taskkill /f /im FoxitPDFReader.exe", shell=True)
>>> web_page_file = cd("docs", "build", "html", "index.html")
>>> save_html_as_pdf(web_page_file, pdf_pathname, verbose=2)
Updating "pyhelpers.pdf" in "./tests/documents/" ...
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

>>> subprocess.Popen(str(pdf_pathname), shell=True)
>>> # subprocess.call("taskkill /f /im FoxitPDFReader.exe", shell=True)
>>> save_html_as_pdf(web_page_file, pdf_pathname, verbose=True)
Updating "pyhelpers.pdf" in "./tests/documents/" ... Done.

>>> subprocess.Popen(str(pdf_pathname), shell=True)
>>> # subprocess.call("taskkill /f /im FoxitPDFReader.exe", shell=True)