Skip to content
18 changes: 17 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,25 @@ You can also pass any options through meta tags in your 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.Configuration()`` - 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``:

.. code-block:: python

config = pdfkit.configuration.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. *where wkhtmltopdf* in Windows or *which wkhtmltopdf* on Linux should return actual path to binary.
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.
23 changes: 15 additions & 8 deletions pdfkit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .pdfkit import PDFKit


def from_url(url, output_path, options=None, toc=None, cover=None):
def from_url(url, output_path, options=None, toc=None, cover=None, configuration=None):
#TODO rework
"""
Convert file of files from URLs to PDF document
Expand All @@ -13,14 +13,17 @@ def from_url(url, output_path, options=None, toc=None, cover=None):
:param options: (optional) dict with wkhtmltopdf global and page options, with or w/o '--'
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
"""

r = PDFKit(url, 'url', options=options, toc=toc, cover=cover)
r = PDFKit(url, 'url', options=options, toc=toc, cover=cover,
configuration=configuration)

r.to_file(output_path)


def from_file(input, output_path, options=None, toc=None, cover=None, css=None):
def from_file(input, output_path, options=None, toc=None, cover=None, css=None,
configuration=None):
"""
Convert HTML file or files to PDF document

Expand All @@ -30,14 +33,17 @@ def from_file(input, output_path, options=None, toc=None, cover=None, css=None):
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param css: (optional) string with path to css file which will be added to a single input file
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
"""

r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css)
r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css,
configuration=configuration)

r.to_file(output_path)


def from_string(input, output_path, options=None, toc=None, cover=None, css=None):
def from_string(input, output_path, options=None, toc=None, cover=None, css=None,
configuration=None):
"""
Convert given string or strings to PDF document

Expand All @@ -47,9 +53,10 @@ def from_string(input, output_path, options=None, toc=None, cover=None, css=None
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param css: (optional) string with path to css file which will be added to a input string

:param configuration: (optional) instance of pdfkit.configuration.Configuration()
"""

r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css)
r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css,
configuration=configuration)

r.to_file(output_path)
r.to_file(output_path)
8 changes: 4 additions & 4 deletions pdfkit/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@


class Configuration(object):
def __init__(self):
self.meta_tag_prefix = 'pdfkit-'
def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-'):
self.meta_tag_prefix = meta_tag_prefix

self.wkhtmltopdf = ''
self.wkhtmltopdf = wkhtmltopdf

if not self.wkhtmltopdf:
if sys.platform == 'win32':
Expand All @@ -21,6 +21,6 @@ def __init__(self):
with open(self.wkhtmltopdf) as f:
pass
except IOError:
raise IOError('No wkhtmltopdf executable found: %s\n'
raise IOError('No wkhtmltopdf executable found: "%s"\n'
'Please install wkhtmltopdf - '
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
8 changes: 6 additions & 2 deletions pdfkit/pdfkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ def __init__(self, msg):
def __str__(self):
return self.msg

def __init__(self, url_or_file, type_, options=None, toc=None, cover=None, css=None):
def __init__(self, url_or_file, type_, options=None, toc=None, cover=None,
css=None, configuration=None):
options = {} if options is None else options
toc = {} if toc is None else toc
self.source = Source(url_or_file, type_)
self.configuration = Configuration()
if configuration is None:
self.configuration = Configuration()
else:
self.configuration = configuration
self.options = dict()
self.stylesheets = []

Expand Down