Best Approach to create PDF in Django Applications
If you have experience with Django, you may noticed that one the features that is not fully functional withing Django applications, is Exporting HTML pages into PDF. I know there are many solutions existing, But if you have charts, tables, CSS and many other stuff in your web page and you want to make a PDF report of it, it may not work well. In some scenarios you may need to write custom CSS codes for print version and somehow have a separate version of webpage just for PDF!
In this article I will introduce django-selenium-pdfmaker which is a complete Django Application to just install via pip and convert any HTML page that you want to PDF.
this package is compatible with django≥2.0 and only uses selenium package as an extra 3rd party package.
How it works!
This package uses selenium as it’s core feature to visit webpages and convert them into PDF. To complete procedure following steps will happen:
- setup selenium webdriver (using google-chrome) and config it to be used to crawl on webpages. This package has an embedded chromedriver in itself. so you don’t need to look for driver and download it.
- connect to webpage using selenium webdriver, convert it to PDF binary data.
- store PDF binary data in database using
ConvertedPDF
model in package for later access.
Usage
To use this application, simply just install it via pip
as follow:
pip install django-selenium-pdfmaker
and whenever u want to create a pdf from a local/remote url just use code section below:
from django_selenium_pdfmaker.modules import PDFMaker
pdfmaker = PDFMaker()
res = pdfmaker.get_pdf_from_html(path='https://google.com', filename='output', write=True)
This will load PDFMaker
main class and try to convert requested URL into PDF. for Example in provided code, request URL is https://google.com
and will store it as output.pdf
file.
and if the URL is reachable, res
object will contain below information:
{
"status": true,
"raw": "pdf in binary format",
"pdf": "ConvertedPDF instance if write flag is True.",
"message": ""
}
status
istrue
when converting to pdf is successful, else will befalse
. For example when url path is unreachablestatus
will befalse
.raw
is binary data of pdf before storing in file. Will hold data ifstatus == true
pdf
isConvertedPDF
object ifstatus
istrue
. so simple to access file url (which will be saved using package and no need for u to store it in file or database) just hitres['pdf'].file.url
.message
will hold reason whystatus
isfalse
.
For more performance you can also perform this action in background using celery
Hope this article was useful. peace :)