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_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: Return type: Returns: Rendered PDF document
Additional
**render_kwargs
are passed tohtml_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
andcontext
.If
download_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
- 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: Returns: Django HTTP response
Additional
**render_kwargs
are passed tohtml_to_pdf()
.- request (
-
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: - template (str) – Path to Django template
- context (
dict
ordjango.template.Context
) – Template context - using – Optional Django template engine
Return type: Returns: Django content file
Additional
**render_kwargs
are passed tohtml_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 theurl_fetcher
is able to load the files.Resource URLs can use either external
http://
orhttps://
protocol or localfile://
protocol (for example when embedding images fromSTATIC_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: - content (str) – HTML content to render
- stylesheets (list of
weasyprint.CSS
orNone
) – Additionalweasyprint.CSS
stylesheets orNone
. See https://weasyprint.readthedocs.io/en/latest/tutorial.html#stylesheet-origins. - base_url – The base used to resolve relative URLs. See WeasyPrint docs.
- url_fetcher – A function or other callable with the same signature
as
weasyprint.default_url_fetcher()
called to fetch external resources such as stylesheets and images. See https://weasyprint.readthedocs.io/en/latest/tutorial.html#url-fetchers. - media_type – The media type to use for
@media
. Defaults to'print'
.
Return type: 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 thenContent-Disposition: attachment
header is added to the response.Default
Content-Type
is'application/pdf'
.Parameters: Return type: 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