API Overview¶
Views¶
PDFTemplateResponseMixin¶
- class easy_pdf.views.PDFTemplateResponseMixin¶
Bases:
TemplateResponseMixinA 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_filenamevalue 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_kwargsby default.The kwargs are passed to
render_to_pdf_response()andxhtml2pdf.pisa.pisaDocument().- Return type
- get_pdf_response(context, **response_kwargs)¶
Renders PDF document and prepares response.
- Returns
Django HTTP response
- Return type
- render_to_response(context, **response_kwargs)¶
Return a response, using the response_class for this view, with a template rendered with the given context.
Pass response_kwargs to the constructor of the response class.
PDFTemplateView¶
- class easy_pdf.views.PDFTemplateView(**kwargs)¶
Bases:
PDFTemplateResponseMixin,ContextMixin,ViewConcrete 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
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,templateandcontext.If
filenameparam is specified then the responseContent-Dispositionheader will be set toattachmentmaking the browser display a “Save as..” dialog.- Parameters
request (
django.http.HttpRequest) – Django HTTP requesttemplate (str) – Path to Django template
context (dict) – Template context
using – Optional Django template engine
- Return type
Other lower-level helpers¶
- easy_pdf.rendering.html_to_pdf(content, dest, encoding='utf-8', link_callback=fetch_resources, **kwargs)¶
Converts html
contentinto PDF document.- Parameters
content (unicode) – html content
- Returns
PDF content
- Return type
- Raises
PDFRenderingError
- easy_pdf.rendering.fetch_resources(uri, rel)¶
Retrieves embeddable resource from given
uri.For now only local resources (images, fonts) are supported.
- easy_pdf.rendering.make_response(content, filename=None, content_type='application/pdf')¶
Wraps content into HTTP response.
If
filenameis specified thenContent-Disposition: attachmentheader is added to the response.Default
Content-Typeisapplication/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