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()
andxhtml2pdf.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: Raises:
-
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
andcontext
.If
filename
param is specified then the responseContent-Disposition
header will be set toattachment
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: - request (
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 thenContent-Disposition: attachment
header is added to the response.Default
Content-Type
isapplication/pdf
.Parameters: Return type:
-
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.
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.