Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ Deprecation Warning

This library has been deprecated to match the `wkhtmltopdf project status <https://wkhtmltopdf.org/status.html>`_.

Security
--------

**Warning!** Never pass untrusted HTML, such as HTML from users, to ``from_string()``. It reads ``<meta name="pdfkit-...">`` tags in the HTML as ``wkhtmltopdf`` options, so whoever writes the HTML can set any option, including ones that read files or run JavaScript (`CVE-2025-26240 <https://www.cve.org/CVERecord?id=CVE-2025-26240>`_).

To convert untrusted HTML:

* Use ``from_file()``, which ignores meta tags, and turn off JavaScript and local file access, as below.
* Never add ``--enable-local-file-access`` or ``--allow``, and never put user input in ``options``, ``toc``, ``cover`` or ``css``.
* Run ``wkhtmltopdf`` where it cannot reach your secrets or internal network. The HTML can still load images and pages from any address.

.. code-block:: python

import io

options = {
'disable-javascript': None,
'disable-local-file-access': None
}

pdfkit.from_file(io.StringIO(untrusted_html), 'out.pdf', options=options)

Installation
------------

Expand Down
8 changes: 8 additions & 0 deletions pdfkit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ def from_url(url, output_path=None, options=None, toc=None, cover=None,
"""
Convert file of files from URLs to PDF document

Warning: never pass untrusted URLs. wkhtmltopdf also opens file:// URLs and addresses
on your internal network. See Security in the README.

:param url: URL or list of URLs to be saved
:param output_path: (optional) path to output PDF file. By default, PDF will be returned for assigning to a variable.
:param options: (optional) dict with wkhtmltopdf global and page options, with or w/o '--'
Expand All @@ -32,6 +35,8 @@ def from_file(input, output_path=None, options=None, toc=None, cover=None, css=N
"""
Convert HTML file or files to PDF document

Unlike from_string(), this ignores <meta name="pdfkit-..."> tags. See Security in the README.

:param input: path to HTML file or list with paths or file-like object
:param output_path: (optional) path to output PDF file. By default, PDF will be returned for assigning to a variable.
:param options: (optional) dict with wkhtmltopdf options, with or w/o '--'
Expand All @@ -56,6 +61,9 @@ def from_string(input, output_path=None, options=None, toc=None, cover=None, css
"""
Convert given string or strings to PDF document

Warning: never pass untrusted HTML. Its <meta name="pdfkit-..."> tags become wkhtmltopdf
options. Use from_file() instead. See Security in the README.

:param input: string with a desired text. Could be a raw text or a html file
:param output_path: (optional) path to output PDF file. By default, PDF will be returned for assigning to a variable.
:param options: (optional) dict with wkhtmltopdf options, with or w/o '--'
Expand Down
3 changes: 2 additions & 1 deletion pdfkit/pdfkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class PDFKit(object):

:param url_or_file: str - either a URL, a path to a file or a string containing HTML
to convert
:param type_: str - either 'url', 'file' or 'string'
:param type_: str - either 'url', 'file' or 'string'. Never use 'string' with untrusted
HTML (see from_string)
:param options: dict (optional) with wkhtmltopdf options, with or w/o '--'
:param toc: dict (optional) - toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: str (optional) - url/filename with a cover html page
Expand Down