Latest version

Last released:

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

Oct 09, 2015  @ashkulz Now we can pass file url arrays, or pass one page htmlString to wkhtmltopdf, I have a requirement to pass html strings to wkhtmltopdf to implement pagination, can you help me?

Project description

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:

  2. Install wkhtmltopdf:

  • Debian/Ubuntu:

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
Wkhtmltopdf c html from string example

Usage

For simple tasks:

You can pass a list with multiple URLs or files:

Also you can pass an opened file:

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

You can specify all wkhtmltopdf options. You can drop ‘–’ in option name. If option without value, use None, False or ‘’ for dict value:. For repeatable options (incl. allow, cookie, custom-header, post, postfile, run-script, replace) you may use a list or a tuple. With option that need multiple values (e.g. –custom-header Authorization secret) we may use a 2-tuple (see example below).

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

Due to wkhtmltopdf command syntax, TOC and Cover options must be specified separately. If you need cover before TOC, use cover_first option:

Wkhtmltopdf C Html From String

Wkhtmltopdf C Html From String Tutorial

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.

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

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:

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'Age of empires byzantines.

    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.6.1
    • Fix regression on python 3+ when trying to decode pdf output
  • 0.6.0
    • Support repeatable options
    • Support multiple values for some options
    • Fix some corner cases when specific argument order is required
    • Some Python 3+ compatibility fixes
    • Update README
  • 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

Release historyRelease notifications

0.6.1

0.6.0

0.5.0

0.4.1

0.4.0

0.3.0

0.2.4

0.2.3

Download files

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

Filename, size & hash File typePython versionUpload date
pdfkit-0.6.1-py2.7.egg (16.3 kB) Egg 2.7
pdfkit-0.6.1-py2-none-any.whl (12.8 kB) Wheel 2.7
pdfkit-0.6.1-py3-none-any.whl (12.8 kB) Wheel 3.5
pdfkit-0.6.1.tar.gz (9.5 kB) Source None

i have a string variable with html code and need convert to pdf the code of the variable, the variable is filled with the data of one gridview

and i need convert this table in pdf including the row style. currently use itext library but don't add the style on the document

miguelSnammiguelSnam

2 Answers

If you want a lot of painstaking work - create your own html -> pdf converter using PDFSharp (http://pdfsharp.com/). However, depending on the usage, I've heard using the internet explorer COM is a good solution. You can always print an HTML page to a virtual PDF printer.

RutledgePaulVRutledgePaulV
1,7212 gold badges15 silver badges40 bronze badges

Use PDFizer, whenever you have valid xHTML (which you should always have).

It's pretty much the most reliable library out there.

Jan BerktoldJan Berktold

Not the answer you're looking for? Browse other questions tagged c#wkhtmltopdf or ask your own question.