Blog
Generate PDF with Python Flask or Django
If you are building a web application in Python with either of the two most popular frameworks – Flask or Django, there’s a good chance you will need to support functionality for PDF generation.
Convert HTML to PDF with Flask or Django
To generate PDFs, we are going to use API2PDF. API2PDF is a REST API that supports PDF generation at massive scale. It can scale to millions of requests, has no rate limits or file size limits. Most importantly, it provides you the choice to generate PDFs with either Headless Chrome or wkhtmltopdf.
In the example below, we are going to use Headless Chrome. Start by installing the api2pdf client library with
pip install api2pdf
Then grab an api key at https://portal.api2pdf.com/register. It only takes a minute and it’s free to get started.
For convenience, I create some helper methods to abstract away from the library in case you want to swap out API2PDF with something else at a later time. See the sample code below. I tend to put this in its own file, such as pdf_manager.py.
"""Module for manipulating and generating PDFs""" | |
import requests | |
import json | |
from api2pdf import Api2Pdf | |
API2PDF_API_KEY = 'YOUR-API-KEY' # get key from portal.api2pdf.com/register | |
USERAGENT = { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} | |
def make_pdf_from_url(url, options=None): | |
"""Produces a pdf from a website's url. | |
Args: | |
url (str): A valid url | |
options (dict, optional): for specifying pdf parameters like landscape | |
mode and margins | |
Returns: | |
pdf of the website | |
""" | |
a2p_client = Api2Pdf(API2PDF_API_KEY) | |
pdf_response = a2p_client.HeadlessChrome.convert_from_url(url, options=options) | |
if pdf_response['success']: | |
download_response = requests.get(pdf_response['pdf'], headers=USERAGENT) | |
data = download_response.content | |
return data | |
else: | |
return None | |
def make_pdf_from_raw_html(html, options=None): | |
"""Produces a pdf from raw html. | |
Args: | |
html (str): Valid html | |
options (dict, optional): for specifying pdf parameters like landscape | |
mode and margins | |
Returns: | |
pdf of the supplied html | |
""" | |
a2p_client = Api2Pdf(API2PDF_API_KEY) | |
pdf_response = a2p_client.HeadlessChrome.convert_from_html(html, options=options) | |
if pdf_response['success']: | |
download_response = requests.get(pdf_response['pdf'], headers=USERAGENT) | |
data = download_response.content | |
return data | |
else: | |
return None |
Switch out “YOUR-API-KEY” with the api key you acquired from API2PDF. You should now be ready to generate PDFs! Give it a shot by calling the functions either with a url to an existing website or raw HTML. The functions will return a file-like object to the PDF that you can do with whatever you’d like. If you prefer to just grab the URL that was generated from API2PDF, you can return that instead.
That’s all!