Skip to content

Commit

Permalink
Merge pull request #146 from JoshLabs/master
Browse files Browse the repository at this point in the history
Adding support for cover page
  • Loading branch information
maxpeterson committed Jun 5, 2018
2 parents d31e839 + 717c404 commit 2e5ea58
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
27 changes: 23 additions & 4 deletions wkhtmltopdf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def wkhtmltopdf(pages, output=None, **kwargs):
if output is None:
# Standard output.
output = '-'
has_cover = kwargs.pop('has_cover', False)

# Default options:
options = getattr(settings, 'WKHTMLTOPDF_CMD_OPTIONS', None)
Expand All @@ -126,6 +127,10 @@ def wkhtmltopdf(pages, output=None, **kwargs):
cmd = 'WKHTMLTOPDF_CMD'
cmd = getattr(settings, cmd, os.environ.get(cmd, 'wkhtmltopdf'))

# Adding 'cover' option to add cover_file to the pdf to generate.
if has_cover:
pages.insert(0, 'cover')

ck_args = list(chain(shlex.split(cmd),
_options_to_args(**options),
list(pages),
Expand All @@ -141,19 +146,24 @@ def wkhtmltopdf(pages, output=None, **kwargs):

return check_output(ck_args, **ck_kwargs)

def convert_to_pdf(filename, header_filename=None, footer_filename=None, cmd_options=None):
def convert_to_pdf(filename, header_filename=None, footer_filename=None, cmd_options=None, cover_filename=None):
# Clobber header_html and footer_html only if filenames are
# provided. These keys may be in self.cmd_options as hardcoded
# static files.
# The argument `filename` may be a string or a list. However, wkhtmltopdf
# will coerce it into a list if a string is passed.
cmd_options = cmd_options if cmd_options else {}
if cover_filename:
pages = [cover_filename, filename]
cmd_options['has_cover'] = True
else:
pages = [filename]

if header_filename is not None:
cmd_options['header_html'] = header_filename
if footer_filename is not None:
cmd_options['footer_html'] = footer_filename
return wkhtmltopdf(pages=filename, **cmd_options)
return wkhtmltopdf(pages=pages, **cmd_options)

class RenderedFile(object):
"""
Expand All @@ -180,7 +190,8 @@ def __del__(self):
if self.temporary_file is not None:
self.temporary_file.close()

def render_pdf_from_template(input_template, header_template, footer_template, context, request=None, cmd_options=None):
def render_pdf_from_template(input_template, header_template, footer_template, context, request=None, cmd_options=None,
cover_template=None):
# For basic usage. Performs all the actions necessary to create a single
# page PDF from a single template and context.
cmd_options = cmd_options if cmd_options else {}
Expand Down Expand Up @@ -211,11 +222,19 @@ def render_pdf_from_template(input_template, header_template, footer_template, c
request=request
)
footer_filename = footer_file.filename
cover = None
if cover_template:
cover = RenderedFile(
template=cover_template,
context=context,
request=request
)

return convert_to_pdf(filename=input_file.filename,
header_filename=header_filename,
footer_filename=footer_filename,
cmd_options=cmd_options)
cmd_options=cmd_options,
cover_filename=cover.filename if cover else None)

def content_disposition_filename(filename):
"""
Expand Down
9 changes: 7 additions & 2 deletions wkhtmltopdf/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, request, template, context=None,
filename=None, show_content_in_browser=None,
header_template=None, footer_template=None,
cmd_options=None, *args, **kwargs):
cover_template = kwargs.pop('cover_template', None)

super(PDFTemplateResponse, self).__init__(request=request,
template=template,
Expand All @@ -54,7 +55,7 @@ def __init__(self, request, template, context=None,

self.header_template = header_template
self.footer_template = footer_template

self.cover_template = cover_template
if cmd_options is None:
cmd_options = {}
self.cmd_options = cmd_options
Expand All @@ -75,7 +76,9 @@ def rendered_content(self):
self.resolve_template(self.footer_template),
context=self.resolve_context(self.context_data),
request=self._request,
cmd_options=cmd_options
cmd_options=cmd_options,
cover_template=self.resolve_template(self.cover_template)

)

class PDFTemplateView(TemplateView):
Expand All @@ -91,6 +94,7 @@ class PDFTemplateView(TemplateView):
template_name = None
header_template = None
footer_template = None
cover_template = None

# TemplateResponse classes for PDF and HTML
response_class = PDFTemplateResponse
Expand Down Expand Up @@ -147,6 +151,7 @@ def render_to_response(self, context, **response_kwargs):
header_template=self.header_template,
footer_template=self.footer_template,
cmd_options=cmd_options,
cover_template=self.cover_template,
**response_kwargs
)
else:
Expand Down

0 comments on commit 2e5ea58

Please sign in to comment.