Django PDFKit

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

Build Status Code Health Code Coverage Documentation Status Latest Version Supported Python versions Downloads

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 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.

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 the download query string parameter.

WKHTMLTOPDF_BIN Environment Variable

pdfkit will automatically look for the wkhtmltohtml binary on your path however if you’ve installed it outside your path you can set the environment variable WKHTMLTOPDF_BIN.

WKHTMLTOPDF_BIN=/path/to/wkhtmltopdf ./manage.py runserver

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
django_pdfkit.tests package
Submodules
django_pdfkit.tests.settings module
django_pdfkit.tests.settings

Django test settings.

django_pdfkit.tests.test_views module
django_pdfkit.tests.test_views.test_html(client)[source]

Test that the query string parameter ?html outputs HTML.

django_pdfkit.tests.test_views.test_pdf(client)[source]

Test that a PDF is downloaded.

django_pdfkit.tests.test_views.test_pdf_filename(client)[source]

Test setting a filename works.

django_pdfkit.tests.test_views.test_pdf_inline(client)[source]

Test that a PDF is inline.

django_pdfkit.tests.urls module
Module contents
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.

render_html(*args, **kwargs)[source]

Renders the template.

Return type:str
render_pdf(*args, **kwargs)[source]

Render the PDF and returns as bytes.

Return type:bytes
Module contents

Django view that converts HTML to PDF using webkit.

class django_pdfkit.VersionInfo(major, minor, micro, releaselevel, serial)

Bases: tuple

major

Alias for field number 0

micro

Alias for field number 2

minor

Alias for field number 1

releaselevel

Alias for field number 3

serial

Alias for field number 4

django_pdfkit

License

This software is licensed under the MIT License. See the LICENSE.

Author

Alex Hayes <alex@alution.com>