Skip to main content

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

Project description

https://travis-ci.org/JazzCore/python-pdfkit.png?branch=master

Python 2 and 3 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

Warning! Version in debian/ubuntu repos have reduced functionality (because it compiled without the wkhtmltopdf QT patches), such as adding outlines, headers, footers, TOC etc. To use this options you should install static binary from wkhtmltopdf site or you can use this script.

  • 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')

Also you can pass an opened file:

with open('file.html') as f:
    pdfkit.from_file(f, 'out.pdf')

If you wish to further process generated PDF, you can read it to a variable:

# Use False instead of output path to save pdf to a variable
pdf = pdfkit.from_url('http://google.com', False)

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)

By default, PDFKit will show all wkhtmltopdf output. If you dont want it, you need to pass quiet option:

options = {
    'quiet': ''
    }

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

Due to wkhtmltopdf 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 files 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.

# Single CSS file
css = 'example.css'
pdfkit.from_file('file.html', options=options, css=css)

# Multiple CSS files
css = ['example.css', 'example2.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

Configuration

Each API call takes an optional configuration paramater. This should be an instance of pdfkit.configuration() API call. It takes the configuration options as initial paramaters. The available options are:

  • wkhtmltopdf - the location of the wkhtmltopdf binary. By default pdfkit will attempt to locate this using which (on UNIX type systems) or where (on Windows).

  • meta_tag_prefix - the prefix for pdfkit specific meta tags - by default this is pdfkit-

Example - for when wkhtmltopdf is not on $PATH:

config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf'))
pdfkit.from_string(html_string, output_file, configuration=config)

Troubleshooting

  • IOError: 'No wkhtmltopdf executable found':

    Make sure that you have wkhtmltopdf in your $PATH or set via custom configuration (see preceding section). where wkhtmltopdf in Windows or which wkhtmltopdf on Linux should return actual path to binary.

  • IOError: 'Command Failed'

    This error means that PDFKit was unable to process an input. You can try to directly run a command from error message and see what error caused failure (on some wkhtmltopdf versions this can be cause by segmentation faults)

Changelog

  • 0.5.0
    • Allow passing multiple css files

    • Fix problems with external file encodings

    • Rise an error when X server is missing on *nix systems

    • Fix tests that was broken with latest wkhtmltopdf release

    • Update README

  • 0.4.1
    • More easier custom configuration setting

    • Update README

  • 0.4.0
    • Allow passing file-like objects

    • Ability to return PDF as a string

    • Allow user specification of configuration

    • API calls now returns True on success

    • bugfixes

  • 0.3.0
    • Python 3 support

  • 0.2.4
    • Add History

    • Update setup.py

  • 0.2.3
    • Fix installing with setup.py

    • Update README

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.5.0.zip (15.7 kB view details)

Uploaded Source

Built Distributions

pdfkit-0.5.0-py2.7.egg (14.9 kB view details)

Uploaded Source

pdfkit-0.5.0-py2-none-any.whl (11.6 kB view details)

Uploaded Python 2

File details

Details for the file pdfkit-0.5.0.zip.

File metadata

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

File hashes

Hashes for pdfkit-0.5.0.zip
Algorithm Hash digest
SHA256 6e176cf460d8f8996d4c316aef1189e1c1c134d28d47a1e5e783e250ee3335dc
MD5 5cbe42c43d463f0794272a01e37a553f
BLAKE2b-256 c41c6d931e5761eb463c1b3d03adc6e9655cebe712c35f970e8ad02363ee7531

See more details on using hashes here.

File details

Details for the file pdfkit-0.5.0-py2.7.egg.

File metadata

  • Download URL: pdfkit-0.5.0-py2.7.egg
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pdfkit-0.5.0-py2.7.egg
Algorithm Hash digest
SHA256 7b4f515243a4c308fadc34430460b2fdea0d11ed12c448838bf401a16fc2c1ac
MD5 1a167208efdbe1fec86ae9cddf86fa92
BLAKE2b-256 3a16fd94ba0926639ed9ae6f818d142ec163882a4d8213fedc2b934bf5a90f54

See more details on using hashes here.

File details

Details for the file pdfkit-0.5.0-py2-none-any.whl.

File metadata

File hashes

Hashes for pdfkit-0.5.0-py2-none-any.whl
Algorithm Hash digest
SHA256 fd218c21ddb4260b4c52b02f3aca32dbf663f91ee16b363f40d348a465d1047d
MD5 e2b2e66be414e6d7bbcd0dceaa2ce2a6
BLAKE2b-256 bc981e0bc428aea39c71ceba0c35849bf218a6cd7ce23719cc48f26cf618873c

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