Name | pdfkit JSON |
Version |
1.0.0
JSON |
| download |
home_page | |
Summary | Wkhtmltopdf python wrapper to convert html to pdf using the webkit rendering engine and qt |
upload_time | 2021-11-14 19:28:51 |
maintainer | |
docs_url | None |
author | Golovanov Stanislav |
requires_python | |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
Python-PDFKit: HTML to PDF wrapper
==================================
.. image:: https://github.com/JazzCore/python-pdfkit/actions/workflows/main.yaml/badge.svg?branch=master
:target: https://github.com/JazzCore/python-pdfkit/actions/workflows/main.yaml
.. image:: https://badge.fury.io/py/pdfkit.svg
:target: http://badge.fury.io/py/pdfkit
Python 2 and 3 wrapper for wkhtmltopdf utility to convert HTML to PDF using Webkit.
This is adapted version of `ruby PDFKit <https://github.com/pdfkit/pdfkit>`_ library, so big thanks to them!
Installation
------------
1. Install python-pdfkit::
$ pip install pdfkit (or pip3 for python3)
2. Install wkhtmltopdf:
* Debian/Ubuntu::
$ sudo apt-get install wkhtmltopdf
* macOS::
$ brew install homebrew/cask/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 <http://wkhtmltopdf.org/>`_ site or you can use `this script <https://github.com/JazzCore/python-pdfkit/blob/master/travis/before-script.sh>`_.
* Windows and other options: check wkhtmltopdf `homepage <http://wkhtmltopdf.org/>`_ 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::
# Without output_path, PDF is returned for assigning to a variable
pdf = pdfkit.from_url('http://google.com')
You can specify all wkhtmltopdf `options <http://wkhtmltopdf.org/usage/wkhtmltopdf.txt>`_. 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).
::
options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'custom-header': [
('Accept-Encoding', 'gzip')
],
'cookie': [
('cookie-empty-value', '""')
('cookie-name1', 'cookie-value1'),
('cookie-name2', 'cookie-value2'),
],
'no-outline': None
}
pdfkit.from_url('http://google.com', 'out.pdf', options=options)
By default, PDFKit will run ``wkhtmltopdf`` with ``quiet`` option turned on, since in most cases output is not needed and can cause excessive memory usage and corrupted results. If need to get ``wkhtmltopdf`` output you should pass ``verbose=True`` to API calls::
pdfkit.from_url('google.com', 'out.pdf', verbose=True)
Due to wkhtmltopdf command syntax, **TOC** and **Cover** options must be specified separately. If you need cover before TOC, use ``cover_first`` option::
toc = {
'xsl-style-sheet': 'toc.xsl'
}
cover = 'cover.html'
pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)
pdfkit.from_file('file.html', options=options, toc=toc, cover=cover, cover_first=True)
You can specify external CSS files when converting files or strings using *css* option.
**Warning** This is a workaround for `this bug <http://code.google.com/p/wkhtmltopdf/issues/detail?id=144>`_ 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)
Also you can use ``configuration()`` call to check if wkhtmltopdf is present in ``$PATH``::
try:
config = pdfkit.configuration()
pdfkit.from_string(html_string, output_file)
except OSError:
#not present in PATH
Troubleshooting
---------------
Debugging issues with PDF generation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If you struggling to generate correct PDF firstly you should check ``wkhtmltopdf`` output for some clues, you can get it by passing ``verbose=True`` to API calls::
pdfkit.from_url('http://google.com', 'out.pdf', verbose=True)
If you are getting strange results in PDF or some option looks like its ignored you should try to run ``wkhtmltopdf`` directly to see if it produces the same result. You can get CLI command by creating ``pdfkit.PDFKit`` class directly and then calling its ``command()`` method::
import pdfkit
r = pdfkit.PDFKit('html', 'string', verbose=True)
print(' '.join(r.command()))
# try running wkhtmltopdf to create PDF
output = r.to_pdf()
Common errors:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- ``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
---------
* `1.0.0`
* By default PDFKit now passes "quiet" option to wkhtmltopdf. Now if you need to get output you should pass "verbose=True" to API calls
* Fix different issues with searching for wkhtmltopdf binary
* Update error handling for wkhtmltopdf
* Fix different issues with options handling
* Better handling of unicode input
* Switch from Travis to GitHub Actions
* Update README
* `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
Raw data
{
"_id": null,
"home_page": "",
"name": "pdfkit",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Golovanov Stanislav",
"author_email": "stgolovanov@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/58/bb/6ddc62b4622776a6514fd749041c2b4bccd343e006d00de590f8090ac8b1/pdfkit-1.0.0.tar.gz",
"platform": "",
"description": "Python-PDFKit: HTML to PDF wrapper\r\n==================================\r\n\r\n\r\n.. image:: https://github.com/JazzCore/python-pdfkit/actions/workflows/main.yaml/badge.svg?branch=master\r\n :target: https://github.com/JazzCore/python-pdfkit/actions/workflows/main.yaml\r\n\r\n.. image:: https://badge.fury.io/py/pdfkit.svg\r\n :target: http://badge.fury.io/py/pdfkit\r\n\r\nPython 2 and 3 wrapper for wkhtmltopdf utility to convert HTML to PDF using Webkit.\r\n\r\nThis is adapted version of `ruby PDFKit <https://github.com/pdfkit/pdfkit>`_ library, so big thanks to them!\r\n\r\nInstallation\r\n------------\r\n\r\n1. Install python-pdfkit::\r\n\r\n\t$ pip install pdfkit (or pip3 for python3)\r\n\r\n2. Install wkhtmltopdf:\r\n\r\n* Debian/Ubuntu::\r\n\r\n\t$ sudo apt-get install wkhtmltopdf\r\n\r\n* macOS::\r\n\r\n\t$ brew install homebrew/cask/wkhtmltopdf\r\n\r\n**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 <http://wkhtmltopdf.org/>`_ site or you can use `this script <https://github.com/JazzCore/python-pdfkit/blob/master/travis/before-script.sh>`_.\r\n\r\n* Windows and other options: check wkhtmltopdf `homepage <http://wkhtmltopdf.org/>`_ for binary installers\r\n\r\nUsage\r\n-----\r\n\r\nFor simple tasks::\r\n\r\n\timport pdfkit\r\n\r\n\tpdfkit.from_url('http://google.com', 'out.pdf')\r\n\tpdfkit.from_file('test.html', 'out.pdf')\r\n\tpdfkit.from_string('Hello!', 'out.pdf')\r\n\r\nYou can pass a list with multiple URLs or files::\r\n\r\n\tpdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')\r\n\tpdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')\r\n\r\nAlso you can pass an opened file::\r\n\r\n with open('file.html') as f:\r\n pdfkit.from_file(f, 'out.pdf')\r\n\r\nIf you wish to further process generated PDF, you can read it to a variable::\r\n\r\n # Without output_path, PDF is returned for assigning to a variable\r\n pdf = pdfkit.from_url('http://google.com')\r\n\r\nYou can specify all wkhtmltopdf `options <http://wkhtmltopdf.org/usage/wkhtmltopdf.txt>`_. 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).\r\n\r\n::\r\n\r\n\toptions = {\r\n\t 'page-size': 'Letter',\r\n\t 'margin-top': '0.75in',\r\n\t 'margin-right': '0.75in',\r\n\t 'margin-bottom': '0.75in',\r\n\t 'margin-left': '0.75in',\r\n\t 'encoding': \"UTF-8\",\r\n\t 'custom-header': [\r\n\t \t('Accept-Encoding', 'gzip')\r\n\t ],\r\n\t 'cookie': [\r\n\t \t('cookie-empty-value', '\"\"')\r\n\t \t('cookie-name1', 'cookie-value1'),\r\n\t \t('cookie-name2', 'cookie-value2'),\r\n\t ],\r\n\t 'no-outline': None\r\n\t}\r\n\r\n\tpdfkit.from_url('http://google.com', 'out.pdf', options=options)\r\n\r\nBy default, PDFKit will run ``wkhtmltopdf`` with ``quiet`` option turned on, since in most cases output is not needed and can cause excessive memory usage and corrupted results. If need to get ``wkhtmltopdf`` output you should pass ``verbose=True`` to API calls::\r\n\r\n pdfkit.from_url('google.com', 'out.pdf', verbose=True)\r\n\r\nDue to wkhtmltopdf command syntax, **TOC** and **Cover** options must be specified separately. If you need cover before TOC, use ``cover_first`` option::\r\n\r\n\ttoc = {\r\n\t 'xsl-style-sheet': 'toc.xsl'\r\n\t}\r\n\r\n\tcover = 'cover.html'\r\n\r\n\tpdfkit.from_file('file.html', options=options, toc=toc, cover=cover)\r\n\tpdfkit.from_file('file.html', options=options, toc=toc, cover=cover, cover_first=True)\r\n\r\nYou can specify external CSS files when converting files or strings using *css* option.\r\n\r\n**Warning** This is a workaround for `this bug <http://code.google.com/p/wkhtmltopdf/issues/detail?id=144>`_ in wkhtmltopdf. You should try *--user-style-sheet* option first.\r\n\r\n::\r\n\r\n # Single CSS file\r\n css = 'example.css'\r\n pdfkit.from_file('file.html', options=options, css=css)\r\n\r\n # Multiple CSS files\r\n css = ['example.css', 'example2.css']\r\n pdfkit.from_file('file.html', options=options, css=css)\r\n\r\nYou can also pass any options through meta tags in your HTML::\r\n\r\n\tbody = \"\"\"\r\n\t <html>\r\n\t <head>\r\n\t <meta name=\"pdfkit-page-size\" content=\"Legal\"/>\r\n\t <meta name=\"pdfkit-orientation\" content=\"Landscape\"/>\r\n\t </head>\r\n\t Hello World!\r\n\t </html>\r\n\t \"\"\"\r\n\r\n\tpdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape\r\n\r\nConfiguration\r\n-------------\r\n\r\nEach 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:\r\n\r\n* ``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).\r\n* ``meta_tag_prefix`` - the prefix for ``pdfkit`` specific meta tags - by default this is ``pdfkit-``\r\n\r\nExample - for when ``wkhtmltopdf`` is not on ``$PATH``::\r\n\r\n config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf')\r\n pdfkit.from_string(html_string, output_file, configuration=config)\r\n\r\nAlso you can use ``configuration()`` call to check if wkhtmltopdf is present in ``$PATH``::\r\n\r\n\ttry:\r\n\t config = pdfkit.configuration()\r\n\t pdfkit.from_string(html_string, output_file)\r\n\texcept OSError:\r\n\t #not present in PATH\r\n\r\n\r\nTroubleshooting\r\n---------------\r\n\r\nDebugging issues with PDF generation\r\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n\r\nIf you struggling to generate correct PDF firstly you should check ``wkhtmltopdf`` output for some clues, you can get it by passing ``verbose=True`` to API calls::\r\n\r\n\tpdfkit.from_url('http://google.com', 'out.pdf', verbose=True)\r\n\r\nIf you are getting strange results in PDF or some option looks like its ignored you should try to run ``wkhtmltopdf`` directly to see if it produces the same result. You can get CLI command by creating ``pdfkit.PDFKit`` class directly and then calling its ``command()`` method::\r\n\r\n\timport pdfkit\r\n\r\n\tr = pdfkit.PDFKit('html', 'string', verbose=True)\r\n\tprint(' '.join(r.command()))\r\n\t# try running wkhtmltopdf to create PDF\r\n\toutput = r.to_pdf()\r\n\r\nCommon errors:\r\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n\r\n- ``IOError: 'No wkhtmltopdf executable found'``:\r\n\r\n 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.\r\n\r\n- ``IOError: 'Command Failed'``\r\n\r\n 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)\r\n\r\n\r\nChangelog\r\n---------\r\n* `1.0.0`\r\n * By default PDFKit now passes \"quiet\" option to wkhtmltopdf. Now if you need to get output you should pass \"verbose=True\" to API calls\r\n * Fix different issues with searching for wkhtmltopdf binary\r\n * Update error handling for wkhtmltopdf\r\n * Fix different issues with options handling\r\n * Better handling of unicode input\r\n * Switch from Travis to GitHub Actions\r\n * Update README\r\n* `0.6.1`\r\n * Fix regression on python 3+ when trying to decode pdf output\r\n* `0.6.0`\r\n * Support repeatable options\r\n * Support multiple values for some options\r\n * Fix some corner cases when specific argument order is required\r\n * Some Python 3+ compatibility fixes\r\n * Update README\r\n* `0.5.0`\r\n * Allow passing multiple css files\r\n * Fix problems with external file encodings\r\n * Rise an error when X server is missing on \\*nix systems\r\n * Fix tests that was broken with latest wkhtmltopdf release\r\n * Update README\r\n* `0.4.1`\r\n * More easier custom configuration setting\r\n * Update README\r\n* `0.4.0`\r\n * Allow passing file-like objects\r\n * Ability to return PDF as a string\r\n * Allow user specification of configuration\r\n * API calls now returns True on success\r\n * bugfixes\r\n* `0.3.0`\r\n * Python 3 support\r\n* `0.2.4`\r\n * Add History\r\n * Update setup.py\r\n* `0.2.3`\r\n * Fix installing with setup.py\r\n * Update README\r\n\r\n\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Wkhtmltopdf python wrapper to convert html to pdf using the webkit rendering engine and qt",
"version": "1.0.0",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "eb49ec961672085773a0e548ff6fa971",
"sha256": "cc122e5aed594198ff7aaa566f2950d2163763576ab891c161bb1f6c630f5a8e"
},
"downloads": -1,
"filename": "pdfkit-1.0.0-py2-none-any.whl",
"has_sig": false,
"md5_digest": "eb49ec961672085773a0e548ff6fa971",
"packagetype": "bdist_wheel",
"python_version": "py2",
"requires_python": null,
"size": 12105,
"upload_time": "2021-11-14T19:28:49",
"upload_time_iso_8601": "2021-11-14T19:28:49.085112Z",
"url": "https://files.pythonhosted.org/packages/bc/66/1d2f525977dc15efe527c274e2106db1acd5432a64cb9cc302c9c7271a26/pdfkit-1.0.0-py2-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "fd81276a8da8a461466edf6e490e5bc0",
"sha256": "a7a4ca0d978e44fa8310c4909f087052430a6e8e0b1dd7ceef657f139789f96f"
},
"downloads": -1,
"filename": "pdfkit-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fd81276a8da8a461466edf6e490e5bc0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 12099,
"upload_time": "2021-11-14T19:28:50",
"upload_time_iso_8601": "2021-11-14T19:28:50.440754Z",
"url": "https://files.pythonhosted.org/packages/64/1b/26c080096dd93936dccfd32c682bed3d5630a84aae9d493ff68afb2ae0fb/pdfkit-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "9736fcd7bcd9c2e20faf108f8ea67a8e",
"sha256": "992f821e1e18fc8a0e701ecae24b51a2d598296a180caee0a24c0af181da02a9"
},
"downloads": -1,
"filename": "pdfkit-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "9736fcd7bcd9c2e20faf108f8ea67a8e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 13288,
"upload_time": "2021-11-14T19:28:51",
"upload_time_iso_8601": "2021-11-14T19:28:51.672448Z",
"url": "https://files.pythonhosted.org/packages/58/bb/6ddc62b4622776a6514fd749041c2b4bccd343e006d00de590f8090ac8b1/pdfkit-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-11-14 19:28:51",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "pdfkit"
}