Skip to main content

Wkhtmltopdf python wrapper to convert html to pdf using the webkit rendering engine and qt

Project description

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

This is adapted version of ruby python-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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pdfkit-0.2.3.zip (8.0 kB view details)

Uploaded Source

File details

Details for the file pdfkit-0.2.3.zip.

File metadata

  • Download URL: pdfkit-0.2.3.zip
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pdfkit-0.2.3.zip
Algorithm Hash digest
SHA256 7ab1c9b4336c6632a81974a3f2650d0d23c9ae2a9ba75d0a5b034ab4c5632be7
MD5 8b9dabb1ef921a0754a7aa374ca39c71
BLAKE2b-256 dc08695e2a563fadda44b7eaa9ff20785c2a21eaf03cab514f81dd46a4e37e80

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page