API Overview

Views

PDFTemplateResponseMixin

class easy_pdf.views.PDFTemplateResponseMixin

Bases: django.views.generic.base.TemplateResponseMixin

A mixin class that implements PDF rendering and Django response construction.

pdf_filename = None

Optional name of the PDF file for download. Leave blank for display in browser.

pdf_kwargs = None

Additional params passed to render_to_pdf_response()

get_pdf_filename()

Returns pdf_filename value by default.

If left blank the browser will display the PDF inline. Otherwise it will pop up the “Save as..” dialog.

Return type:str()
get_pdf_kwargs()

Returns pdf_kwargs by default.

The kwargs are passed to render_to_pdf_response() and xhtml2pdf.pisa.pisaDocument().

Return type:dict
get_pdf_response(context, **response_kwargs)

Renders PDF document and prepares response.

Returns:Django HTTP response
Return type:django.http.HttpResponse
render_to_response(context, **response_kwargs)

PDFTemplateView

class easy_pdf.views.PDFTemplateView(**kwargs)

Bases: easy_pdf.views.PDFTemplateResponseMixin, django.views.generic.base.ContextMixin, django.views.generic.base.View

Concrete view for serving PDF files.

class HelloPDFView(PDFTemplateView):
    template_name = "hello.html"
get(request, *args, **kwargs)

Handles GET request and returns HTTP response.

PDF rendering functions

easy_pdf.rendering.render_to_pdf(template, context, using=None, request=None, encoding='utf-8', **kwargs)

Create PDF document from Django html template.

Parameters:
  • template (str) – Path to Django template
  • context (dict) – Template context
  • using – Optional Django template engine
  • request (django.http.HttpRequest) – Django HTTP request
Returns:

rendered PDF

Return type:

bytes

Raises:

PDFRenderingError, UnsupportedMediaPathException

easy_pdf.rendering.render_to_pdf_response(request, template, context, using=None, filename=None, encoding='utf-8', **kwargs)

Renders a PDF response using given request, template and context.

If filename param is specified then the response Content-Disposition header will be set to attachment making the browser display a “Save as..” dialog.

Parameters:
  • request (django.http.HttpRequest) – Django HTTP request
  • template (str) – Path to Django template
  • context (dict) – Template context
  • using – Optional Django template engine
Return type:

django.http.HttpResponse

Other lower-level helpers

easy_pdf.rendering.html_to_pdf(content, dest, encoding="utf-8", link_callback=fetch_resources, **kwargs)

Converts html content into PDF document.

Parameters:content (unicode) – html content
Returns:PDF content
Return type:bytes
Raises:PDFRenderingError
easy_pdf.rendering.fetch_resources(uri, rel)

Retrieves embeddable resource from given uri.

For now only local resources (images, fonts) are supported.

Parameters:uri (str) – path or url to image or font resource
Returns:path to local resource file.
Return type:str
Raises:UnsupportedMediaPathException
easy_pdf.rendering.make_response(content, filename=None, content_type='application/pdf')

Wraps content into HTTP response.

If filename is specified then Content-Disposition: attachment header is added to the response.

Default Content-Type is application/pdf.

Parameters:
  • content (bytes) – response content
  • filename (str) – optional filename for file download
  • content_type (str) – response content type
Return type:

django.http.HttpResponse

easy_pdf.rendering.encode_filename(filename)

Encodes filename part for Content-Disposition: attachment.

>>> print(encode_filename("abc.pdf"))
filename=abc.pdf
>>> print(encode_filename("aa bb.pdf"))
filename*=UTF-8''aa%20bb.pdf
>>> print(encode_filename(u"zażółć.pdf"))
filename*=UTF-8''za%C5%BC%C3%B3%C5%82%C4%87.pdf

Exceptions

exception easy_pdf.exceptions.EasyPDFError

Bases: Exception

Base error class

exception easy_pdf.exceptions.UnsupportedMediaPathException

Bases: easy_pdf.exceptions.EasyPDFError

Resource not found or unavailable

exception easy_pdf.exceptions.PDFRenderingError(message, content, log, *args, **kwargs)

Bases: easy_pdf.exceptions.EasyPDFError

PDF Rendering error. Check HTML template for errors.