Django PDFKit¶
Django view that converts HTML to PDF using webkit - via pdfkit and wkhtmltopdf.


Contents¶
Installation¶
Prerequisites¶
You need to install wkhtmltopdf.
The versions in Debian/Ubuntu repositories (installed using apt-get install wkhtmltopdf
)
have reduced functionality and you will most likely want to install one of the
stable binaries provided at http://wkhtmltopdf.org/downloads.html which provide
increased functionality (headless, patched Qt, better rendering support).
Installation of the pre-compiled binaries on Ubuntu is as simple as;
wget http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
tar -xf wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
sudo cp -r wkhtmltox/* /usr/
pdfkit will automatically look for the wkhtmltohtml
binary on your path
however if it’s not on your path you can set it.
Install¶
You can install django-pdfkit either via the Python Package Index (PyPI) or from github.
To install using pip;
$ pip install django-pdfkit
From github;
$ pip install git+https://github.com/alexhayes/django-pdfkit.git
Usage¶
Simply use the class based PDFView
which is a drop in replacement for TemplateView
.
# urls.py
from django_pdfkit import PDFView
...
url(r'^my-pdf/$', PDFView.as_view(template_name='my-pdf.html'), name='my-pdf'),
...
Then in your browser goto http://localhost:8000/my-pdf/
and it will magically
render as a PDF.
By default the PDF filename will be the same as the template file name however with
a .pdf
file extension.
Query Parameter Support¶
The following query parameters can adjust the views behaviour.
inline
- don’t set theCONTENT-DISPOSITION
header, causing the PDF to be displayed inline if the browser supports it.download
- set theCONTENT-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 whensettings.DEBUG
isTrue
.
For example, http://localhost:8000/my-pdf/?inline
- will cause the PDF to be
displayed inline.
Properties¶
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.inline
- default to display PDF inline, can be overridden with thedownload
query string parameter.
Developer Documentation¶
Contributions¶
Contributions are more than welcome!
To get setup do the following;
mkvirtualenv --python=/usr/bin/python3.5 django-pdfkit
git clone https://github.com/alexhayes/django-pdfkit.git
cd django-pdfkit
pip install -r requirements/dev.txt
pip install Django
Running Tests¶
Once you’ve checked out you should be able to run the tests;
tox
Or run all environments at once using detox;
detox
Creating Documentation¶
cd docs
make clean html
Internal Module Reference¶
Release: | 0.3.1 |
---|---|
Date: | Oct 02, 2017 |
django_pdfkit package¶
Subpackages¶
Submodules¶
django_pdfkit.views module¶
django_pdfkit.views¶
Django view that converts HTML to PDF using webkit - via pdfkit and wkhtmltopdf.
-
class
django_pdfkit.views.
PDFView
(**kwargs)[source]¶ Bases:
django.views.generic.base.TemplateView
-
filename
= None¶ Set to change the filename of the PDF.
-
get
(request, *args, **kwargs)[source]¶ Return a HTTPResponse either of a PDF file or HTML.
Return type: HttpResponse
-
get_filename
()[source]¶ Return
self.filename
if set otherwise return the template basename with a.pdf
extension.Return type: str
-
get_pdfkit_options
()[source]¶ Returns
self.pdfkit_options
if set otherwise a default dict of options to supply to pdfkit.Return type: dict
-
inline
= False¶ Set to default the PDF display to inline.
-
pdfkit_options
= None¶ Set pdfkit options dict.
-
Module contents¶
Django view that converts HTML to PDF using webkit.
django_pdfkit¶
Author¶
Alex Hayes <alex@alution.com>