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, **kwargs)[source]
Save a web page as a PDF file by wkhtmltopdf.
- Parameters:
data (str) – URL of a web page or pathname of an HTML file.
path_to_file (str) – Path where a PDF file is saved.
if_exists (str) – Indicate the action if the .pdf file exsits; defaults to
'replace'
; valid options include'replace'
,'pass'
and'append'
.page_size (str) – Page size; defaults to
'A4'
.zoom (float) – Magnification for zooming in/out; defaults to
1.0
.encoding (str) – Encoding format; defaults to
'UTF-8'
.wkhtmltopdf_options (dict | None) – Specify wkhtmltopdf options; defaults to
None
; check also the project description of pdfkit.wkhtmltopdf_path (str | None) – An absolute path to ‘wkhtmltopdf.exe’; defaults to
None
; whenwkhtmltopdf_exe=None
, use the default installation path, such as “C:Program Fileswkhtmltopdfbinwkhtmltopdf.exe” (on Windows).verbose (bool | int) – Whether to print relevant information in console; defaults to
False
.kwargs – [Optional] parameters of pdfkit.from_url.
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) >>> # Open the PDF file using the system's default application >>> subprocess.Popen(pdf_pathname, shell=True) >>> web_page_file = cd("docs\build\html\index.html") >>> save_html_as_pdf(web_page_file, pdf_pathname, verbose=True) Updating "pyhelpers.pdf" at "tests\documents\" ... Done. >>> subprocess.Popen(pdf_pathname, shell=True) >>> save_html_as_pdf(web_page_file, pdf_pathname, verbose=2) Updating "pyhelpers.pdf" at "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(pdf_pathname, shell=True)