Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion django_pdfkit/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_html(client):
"""
Test that the query string parameter ?html outputs HTML.
"""
response = client.get('%s?html' % reverse('pdf'))
response = client.get('%s' % reverse('html'))
with open(os.path.join(TESTS_TEMPLATE_DIR, 'basic.html'), 'rb') as basic_file:
expected = basic_file.read()
assert response.content == expected
23 changes: 20 additions & 3 deletions django_pdfkit/tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
from django_pdfkit.views import PDFView

urlpatterns = [ # pylint: disable=invalid-name
url(r'^pdf/', PDFView.as_view(template_name='basic.html'), name='pdf'),
url(r'^pdf-inline/', PDFView.as_view(inline=True, template_name='basic.html'), name='pdf-inline'),
url(r'^pdf-filename/', PDFView.as_view(filename='foo.pdf', template_name='basic.html'), name='pdf-filename'),
url(
r'^pdf/',
PDFView.as_view(template_name='basic.html', html=False),
name='pdf'
),
url(
r'^pdf-inline/',
PDFView.as_view(inline=True, template_name='basic.html', html=False),
name='pdf-inline'
),
url(
r'^pdf-filename/',
PDFView.as_view(filename='foo.pdf', template_name='basic.html', html=False),
name='pdf-filename'
),
url(
r'^html/',
PDFView.as_view(template_name='basic.html'),
name='html'
),
]
20 changes: 11 additions & 9 deletions django_pdfkit/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
from __future__ import absolute_import, print_function, unicode_literals

import os

from os.path import basename, splitext

import pdfkit

from django.conf import settings
from django.http import HttpResponse
from django.template import loader
from django.test import override_settings
from django.views.generic import TemplateView
import pdfkit


class PDFView(TemplateView):
Expand All @@ -28,13 +30,16 @@ class PDFView(TemplateView):
#: Set pdfkit options dict.
pdfkit_options = None

#: Set to false if you don't want render html
html = True

def get(self, request, *args, **kwargs):
"""
Return a HTTPResponse either of a PDF file or HTML.

:rtype: HttpResponse
"""
if 'html' in request.GET:
if self.html is True:
# Output HTML
content = self.render_html(*args, **kwargs)
return HttpResponse(content)
Expand Down Expand Up @@ -79,13 +84,10 @@ def get_pdfkit_options(self):

:rtype: dict
"""
options = {'page-size': 'A4', 'encoding': 'UTF-8'}
if self.pdfkit_options is not None:
return self.pdfkit_options

return {
'page-size': 'A4',
'encoding': 'UTF-8',
}
options.update(self.pdfkit_options)
return options

def get_filename(self):
"""
Expand All @@ -110,6 +112,6 @@ def render_html(self, *args, **kwargs):

with override_settings(STATIC_URL=static_url, MEDIA_URL=media_url):
template = loader.get_template(self.template_name)
context = self.get_context_data(*args, **kwargs)
context = self.get_context_data(**kwargs)
html = template.render(context)
return html
5 changes: 2 additions & 3 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Simply use the class based ``PDFView`` which is a drop in replacement for ``Temp
from django_pdfkit import PDFView

...
url(r'^my-pdf/$', PDFView.as_view(template_name='my-pdf.html'), name='my-pdf'),
url(r'^my-pdf/$', PDFView.as_view(template_name='my-pdf.html', html=False), name='my-pdf'),
...


Expand All @@ -29,8 +29,6 @@ The following query parameters can adjust the views behaviour.
- ``inline`` - don't set the ``CONTENT-DISPOSITION`` header, causing the PDF to be
displayed inline if the browser supports it.
- ``download`` - set the ``CONTENT-DISPOSITION`` header (default).
- ``html`` - don't render as a PDF, render as HTML, useful for debugging your
HTML output.
- ``debug`` - turn on debug mode when calling pdfkit - only works when
``settings.DEBUG`` is ``True``.

Expand All @@ -44,6 +42,7 @@ Define any of the following properties either as a kwarg to ``PDFView.as_view``
or as a property on the view.

- ``filename`` - set the downloadable filename.
- ``html`` - set to false to render pdf.
- ``inline`` - default to display PDF inline, can be overridden with the
``download`` query string parameter.

Expand Down