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.

download_filename = None

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

base_url = None

Base URL for referencing relative images, fonts and stylesheet resources.

response_class

Response class. Defaults to django.http.HttpResponse.

alias of HttpResponse

content_type = 'application/pdf'

Response content type. Default is 'application/pdf'.

get_download_filename()

Returns download_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 or None
get_base_url()

Returns base_url value by default.

Return type:str or None
get_render_kwargs()

The render kwargs are passed to html_to_pdf().

get_pdf_response(context)

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

A view that renders template to PDF document in a way similar to Django’s TemplateView

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, **render_kwargs)

Creates PDF document from Django HTML template.

Parameters:
  • template (str) – Path to Django template
  • context (dict) – Template context
  • using – Optional Django template engine
  • request – Optional Django Request
Return type:

bytes

Returns:

Rendered PDF document

Additional **render_kwargs are passed to html_to_pdf().

easy_pdf.rendering.render_to_pdf_response(request, template, context, using=None, download_filename=None, content_type='application/pdf', response_class=HttpResponse, **render_kwargs)

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

If download_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
  • download_filename (str) – Optional filename to use for file download
  • content_type (str) – Response content type
  • response_class – Default is django.http.HttpResponse
Return type:

django.http.HttpResponse

Returns:

Django HTTP response

Additional **render_kwargs are passed to html_to_pdf().

easy_pdf.rendering.render_to_content_file(template, context, using=None, **render_kwargs)

Example:

>>> content = render_to_content_file('doc.html')

Then save to Django storage:

>>> from django.core.files.storage import default_storage
>>> default_storage.save('file.pdf', content)

Or attach to a model instance:

>>> instance.attachment.save('file.pdf', content, save=True)
Parameters:
Return type:

django.core.files.base.ContentFile

Returns:

Django content file

Additional **render_kwargs are passed to html_to_pdf().

Other lower-level helpers

easy_pdf.rendering.html_to_pdf(content, stylesheets=None, base_url=None, url_fetcher=default_url_fetcher, media_type='print')

Converts HTML content into PDF document.

The HTML document can contain references to image, font and style resources provided as absolute or relative URLs. If resources are referenced by relative URLs the base_url param must also be specified so the url_fetcher is able to load the files.

Resource URLs can use either external http:// or https:// protocol or local file:// protocol (for example when embedding images from STATIC_ROOT directory).

Keep that in mind and always specify and validate URLs for linked resources in case of user generated content is rendered to PDF documents to avoid potential security issues.

Parameters:
Return type:

bytes

Returns:

PDF content

easy_pdf.rendering.make_response(content, download_filename=None, content_type='application/pdf', response_class=HttpResponse)

Wraps file 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
  • download_filename (str) – Optional filename for file download
  • content_type (str) – Response content type
  • response_class – Response class to instantiate
Return type:

django.http.HttpResponse

Returns:

Django response

easy_pdf.rendering.encode_filename(filename)

Encodes filename part for Content-Disposition: attachment.

Parameters:filename (str) – Filename to encode
Return type:str
Returns:Encoded filename for use in Content-Disposition header
>>> 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