aboutsummaryrefslogtreecommitdiffstats
path: root/wkhtmltopdf/utils.py
diff options
context:
space:
mode:
authorSimon Law <simon.law@ecometrica.com>2012-07-20 15:52:03 -0400
committerSimon Law <simon.law@ecometrica.com>2012-07-20 15:52:03 -0400
commita377496b5a9bfab824b52a7f2e69a64f194930b8 (patch)
treec8d6da1b37cab0cfc2fad04a89f64e0a5ffc768b /wkhtmltopdf/utils.py
parenteae470eaaa1d3c95f8e58c5296ed28a01bcd74aa (diff)
downloaddjango-wkhtmltopdf-a377496b5a9bfab824b52a7f2e69a64f194930b8.tar.gz
django-wkhtmltopdf-a377496b5a9bfab824b52a7f2e69a64f194930b8.tar.bz2
django-wkhtmltopdf-a377496b5a9bfab824b52a7f2e69a64f194930b8.zip
Reliable command-line argument parsing for wkhtmltopdf().
The API for wkhtmltopdf has changed. Long arguments that take no parameters now use True and not the empty string. In addition, argument-parameters may now be Unicode.
Diffstat (limited to 'wkhtmltopdf/utils.py')
-rw-r--r--wkhtmltopdf/utils.py55
1 files changed, 37 insertions, 18 deletions
diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py
index 7994a39..aab4152 100644
--- a/wkhtmltopdf/utils.py
+++ b/wkhtmltopdf/utils.py
@@ -1,6 +1,8 @@
from __future__ import absolute_import
+from itertools import chain
from os import fdopen
+import sys
from tempfile import mkstemp
from django.conf import settings
@@ -11,12 +13,24 @@ from .subprocess import check_output
WKHTMLTOPDF_CMD = getattr(settings, 'WKHTMLTOPDF_CMD', 'wkhtmltopdf')
+
+def _options_to_args(**options):
+ """Converts ``options`` into a string of command-line arguments."""
+ flags = []
+ for name in sorted(options):
+ value = options[name]
+ flags.append('--' + name.replace('_', '-'))
+ if value is not True:
+ flags.append(unicode(value))
+ return flags
+
+
def wkhtmltopdf(pages, output=None, **kwargs):
"""
Converts html to PDF using http://code.google.com/p/wkhtmltopdf/.
pages: List of file paths or URLs of the html to be converted.
- output: Optional output file path.
+ output: Optional output file path. If None, the output is returned.
**kwargs: Passed to wkhtmltopdf via _extra_args() (See
https://github.com/antialize/wkhtmltopdf/blob/master/README_WKHTMLTOPDF
for acceptable args.)
@@ -24,31 +38,36 @@ def wkhtmltopdf(pages, output=None, **kwargs):
{'footer_html': 'http://example.com/foot.html'}
becomes
'--footer-html http://example.com/foot.html'
- Where there is no value passed, use a blank string. e.g.:
- {'disable_javascript': ''}
+ Where there is no value passed, use True. e.g.:
+ {'disable_javascript': True}
becomes:
- '--disable-javascript '
+ '--disable-javascript'
example usage:
- wkhtmltopdf(html_path="~/example.html",
+ wkhtmltopdf(pages=['/tmp/example.html'],
dpi=300,
- orientation="Landscape",
- disable_javascript="")
+ orientation='Landscape',
+ disable_javascript=True)
"""
+ if isinstance(pages, basestring):
+ # Support a single page.
+ pages = [pages]
- def _extra_args(**kwargs):
- """Converts kwargs into a string of flags to be passed to wkhtmltopdf."""
- flags = ''
- for k, v in kwargs.items():
- flags += ' --%s %s' % (k.replace('_', '-'), v)
- return flags
+ if output is None:
+ # Standard output.
+ output = '-'
- if not isinstance(pages, list):
- pages = [pages]
+ # Default options:
+ options = {
+ 'quiet': True,
+ }
+ options.update(kwargs)
- kwargs['quiet'] = ''
- args = '%s %s %s %s' % (WKHTMLTOPDF_CMD, _extra_args(**kwargs), ' '.join(pages), output or '-')
- return check_output(args, shell=True)
+ args = list(chain([WKHTMLTOPDF_CMD],
+ _options_to_args(**options),
+ list(pages),
+ [output]))
+ return check_output(args, stderr=sys.stderr)
def template_to_temp_file(template_name, dictionary=None, context_instance=None):