Skip to content

Commit e23bfd9

Browse files
committed
Merge pull request #1 from tomscytale/master
allow user specification of configuration
2 parents 521cbf5 + 5ca4f15 commit e23bfd9

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

README.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,25 @@ You can also pass any options through meta tags in your HTML:
101101
102102
pdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape
103103
104+
Configuration
105+
-------------
106+
107+
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:
108+
109+
* ``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).
110+
* ``meta_tag_prefix`` - the prefix for ``pdfkit`` specific meta tags - by default this is ``pdfkit-``
111+
112+
Example - for when ``wkhtmltopdf`` is not on ``$PATH``:
113+
114+
.. code-block:: python
115+
116+
config = pdfkit.configuration.Configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf'))
117+
pdfkit.from_string(html_string, output_file, configuration=config)
118+
119+
104120
Troubleshooting
105121
---------------
106122

107123
- ``IOError: 'No wkhtmltopdf executable found'``:
108124

109-
Make sure that you have wkhtmltopdf in your PATH. *where wkhtmltopdf* in Windows or *which wkhtmltopdf* on Linux should return actual path to binary.
125+
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.

pdfkit/api.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .pdfkit import PDFKit
44

55

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

18-
r = PDFKit(url, 'url', options=options, toc=toc, cover=cover)
19+
r = PDFKit(url, 'url', options=options, toc=toc, cover=cover,
20+
configuration=configuration)
1921

2022
r.to_file(output_path)
2123

2224

23-
def from_file(input, output_path, options=None, toc=None, cover=None, css=None):
25+
def from_file(input, output_path, options=None, toc=None, cover=None, css=None,
26+
configuration=None):
2427
"""
2528
Convert HTML file or files to PDF document
2629
@@ -30,14 +33,17 @@ def from_file(input, output_path, options=None, toc=None, cover=None, css=None):
3033
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
3134
:param cover: (optional) string with url/filename with a cover html page
3235
:param css: (optional) string with path to css file which will be added to a single input file
36+
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
3337
"""
3438

35-
r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css)
39+
r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css,
40+
configuration=configuration)
3641

3742
r.to_file(output_path)
3843

3944

40-
def from_string(input, output_path, options=None, toc=None, cover=None, css=None):
45+
def from_string(input, output_path, options=None, toc=None, cover=None, css=None,
46+
configuration=None):
4147
"""
4248
Convert given string or strings to PDF document
4349
@@ -47,9 +53,10 @@ def from_string(input, output_path, options=None, toc=None, cover=None, css=None
4753
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
4854
:param cover: (optional) string with url/filename with a cover html page
4955
:param css: (optional) string with path to css file which will be added to a input string
50-
56+
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
5157
"""
5258

53-
r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css)
59+
r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css,
60+
configuration=configuration)
5461

55-
r.to_file(output_path)
62+
r.to_file(output_path)

pdfkit/configuration.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55

66
class Configuration(object):
7-
def __init__(self):
8-
self.meta_tag_prefix = 'pdfkit-'
7+
def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-'):
8+
self.meta_tag_prefix = meta_tag_prefix
99

10-
self.wkhtmltopdf = ''
10+
self.wkhtmltopdf = wkhtmltopdf
1111

1212
if not self.wkhtmltopdf:
1313
if sys.platform == 'win32':
@@ -21,6 +21,6 @@ def __init__(self):
2121
with open(self.wkhtmltopdf) as f:
2222
pass
2323
except IOError:
24-
raise IOError('No wkhtmltopdf executable found: %s\n'
24+
raise IOError('No wkhtmltopdf executable found: "%s"\n'
2525
'Please install wkhtmltopdf - '
2626
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)

pdfkit/pdfkit.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ def __init__(self, msg):
1919
def __str__(self):
2020
return self.msg
2121

22-
def __init__(self, url_or_file, type_, options=None, toc=None, cover=None, css=None):
22+
def __init__(self, url_or_file, type_, options=None, toc=None, cover=None,
23+
css=None, configuration=None):
2324
options = {} if options is None else options
2425
toc = {} if toc is None else toc
2526
self.source = Source(url_or_file, type_)
26-
self.configuration = Configuration()
27+
if configuration is None:
28+
self.configuration = Configuration()
29+
else:
30+
self.configuration = configuration
2731
self.options = dict()
2832
self.stylesheets = []
2933

0 commit comments

Comments
 (0)