Django PDF rendering, easily convert html responses to pdf responses
Developed by William Otieno.
This app makes rendering PDF files in Django really easy. It can be used to create invoices, bills and other documents from simple HTML markup and CSS styles. You can even embed images and use custom fonts.
The library provides both Class-Based View that is almost a drop-in
replacement for Django's TemplateView
as well as helper functions
to render PDFs in the backend outside the request scope
(i.e. using Celery workers).
Include
dj-easy-pdf
,xhtml2pdf
in yourrequirements.txt
file. If you are on Python 3 you need to install the latest version of Reportlab and the beta version of xhtml2pdf:$ pip install dj-easy-pdf
Add
easy_pdf
toINSTALLED_APPS
.Create HTML template for PDF document and add a view that will render it:
{% extends "easy_pdf/base.html" %} {% block content %} <div id="content"> <h1>Hi there!</h1> </div> {% endblock %}
from easy_pdf.views import PDFTemplateView class HelloPDFView(PDFTemplateView): template_name = 'hello.html'
You can also use a mixin to output PDF from Django generic views:
class PDFUserDetailView(PDFTemplateResponseMixin, DetailView): model = get_user_model() template_name = 'user_detail.html'
If you're using Function Based views then:
from easy_pdf.rendering import render_to_pdf_response def pdf_output(request): context = {} return render_to_pdf_response(request, "output.html", context)
The full documentation is at dj-easy-pdf.readthedocs.io.
dj-easy-pdf
depends on:
django>=3.2
xhtml2pdf
reportlab
dj-easy-pdf
is released under the MIT license.
- GitHub repository - https://github.com/WilliamOtieno/dj-easy-pdf
- PyPi Package site - https://pypi.python.org/pypi/dj-easy-pdf
- Docs - https://dj-easy-pdf.readthedocs.io/
This app and many other help us build better software and focus on delivering quality projects faster. We would love to help you with your next project so get in touch by dropping an email at jimmywilliamotieno@gmail.com