Skip to main content

Python-PDFKit: HTML to PDF wrapper

Python wrapper for wkhtmltopdf utility to convert HTML to PDF using Webkit.

This is adapted version of ruby PDFKit library, so big thanks to them!

Installation

  1. Install python-pdfkit:

$ pip install pdfkit
  1. Install wkhtmltopdf:

  • Debian/Ubuntu:

$ sudo apt-get install wkhtmltopdf
  • Windows and other options: check wkhtmltopdf homepage for binary installers

Usage

For simple tasks:

import pdfkit

pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')

You can pass a list with multiple URLs or files:

pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')

You can specify all wkhtmltopdf options. You can drop ‘–’ in option name. If option without value, use None, False or ‘’ for dict value:

options = {
    'page-size': 'Letter',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'encoding': "UTF-8",
    'no-outline': None
}

pdfkit.from_url('http://google.com', 'out.pdf', options=options)

Due to wkhmtltopdf command syntax, TOC and Cover options must be specified separately:

toc = {
    'xsl-style-sheet': 'toc.xsl'
}

cover = 'cover.html'

pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)

You can specify external CSS file when converting files or strings using css option.

Warning This is a workaround for this bug in wkhtmltopdf. You should try –user-style-sheet option first.

css = 'example.css'

pdfkit.from_file('file.html', options=options, css=css)

You can also pass any options through meta tags in your HTML:

body = """
    <html>
      <head>
        <meta name="pdfkit-page-size" content="Legal"/>
        <meta name="pdfkit-orientation" content="Landscape"/>
      </head>
      Hello World!
      </html>
    """

pdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape

Troubleshooting

  • IOError: 'No wkhtmltopdf executable found':

    Make sure that you have wkhtmltopdf in your PATH. where wkhtmltopdf in Windows or which wkhtmltopdf on Linux should return actual path to binary.

Changelog

  • 0.2.4
    • Add History

    • Update setup.py

  • 0.2.3
    • Fix installing with setup.py

    • Update README

Metadata

Release files for pdfkit 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pdfkit 0.2.4
File Size Uploaded Provenance Actions
pdfkit-0.2.4.zip 8.4 kB Without provenance Details for pdfkit-0.2.4.zip

Release files / pdfkit-0.2.4.zip

Download URL pdfkit-0.2.4.zip
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e815926f61135ee99dd87c293fe4483c298e2889bf01658e674f7266cc696c42
BLAKE2b-256 checksum
How to use checksums
9383ebb9495702bd0d9056e703efc90bf810202a7a83c19397f69d9acc415a19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pdfkit 0.2.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page