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 using wkhtmltopdf.
- Parameters:
data (str) – The URL of a web page or the pathname of an HTML file.
path_to_file (str) – The path where the PDF file will be saved.
if_exists (str) – Action to take if the .pdf file already exists; options are
'replace'
(default),'pass'
and'append'
.page_size (str) – The page size; defaults to
'A4'
.zoom (float) – Magnification for zooming in/out; defaults to
1.0
.encoding (str) – The encoding format; defaults to
'UTF-8'
.wkhtmltopdf_options (dict | None) – Options for wkhtmltopdf; defaults to
None
. Refer to the description of pdfkit project for more details.wkhtmltopdf_path (str | None) – The path to “wkhtmltopdf.exe”; when
wkhtmltopdf_path=None
(default), the default installation path will be used, e.g. “C:Program Fileswkhtmltopdfbinwkhtmltopdf.exe” (on Windows).verbose (bool | int) – Whether to print relevant information to the console; defaults to
False
.kwargs – [Optional] Additional parameters for the function 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) >>> # Close the PDF file (if opened with Foxit Reader) >>> # 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=True) Updating "pyhelpers.pdf" at "tests\documents\" ... Done. >>> subprocess.Popen(pdf_pathname, shell=True) >>> # subprocess.call("taskkill /f /im FoxitPDFReader.exe", 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)