How to generate PDF invoice from HTML template for HTML to PDF API in Python with Web API

Learn in simple ways: How to generate PDF invoice from HTML template for HTML to PDF API in Python

We regularly create and update our sample code library so you may quickly learn HTML to PDF API and the step-by-step process in Python. Web API helps with HTML to PDF API in Python. Web API is the Web API with a set of tools for documents manipulation, data conversion, data extraction, splitting and merging of documents. Includes image recognition, built-in OCR, barcode generation and barcode decoders to decode bar codes from scans, pictures and pdf.

The SDK samples displayed below below explain how to quickly make your application do HTML to PDF API in Python with the help of Web API. Follow the tutorial and copy – paste code for Python into your project’s code editor. This basic programming language sample code for Python will do the whole work for you in implementing HTML to PDF API in your app.

Our website provides free trial version of Web API that gives source code samples to assist with your Python project.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

import os import json import requests # pip install requests # The authentication key (API Key). # Get your own by registering at API_KEY = "***********************************************" # Base URL for Web API requests BASE_URL = "" # HTML template file_read = open(".\\invoice_template.html", mode='r') Template = file_read.close() # Data to fill the template file_read = open(".\\invoice_data.json", mode='r') TemplateData = json.dumps( file_read.close() # Destination PDF file name DestinationFile = ".\\result.pdf" def main(args = None): GeneratePDFFromTemplate(Template, TemplateData, DestinationFile) def GeneratePDFFromTemplate(template, templateData, destinationFile): """Converts HTML to PDF using Web API""" data = { 'templateData': templateData, 'html': template } # Prepare URL for 'HTML To PDF' API request url = "{}/pdf/convert/from/html?name={}".format( BASE_URL, os.path.basename(destinationFile) ) # Execute request and get response as JSON response =, data=data, headers={ "x-api-key": API_KEY }) if (response.status_code == 200): json = response.json() if json["error"] == False: # Get URL of result file resultFileUrl = json["url"] # Download result file r = requests.get(resultFileUrl, stream=True) if (r.status_code == 200): with open(destinationFile, 'wb') as file: for chunk in r: file.write(chunk) print(f"Result file saved as \"{destinationFile}\" file.") else: print(f"Request error: {response.status_code} {response.reason}") else: # Show service reported error print(json["message"]) else: print(f"Request error: {response.status_code} {response.reason}") if __name__ == '__main__': main()


{ "number": "1234567", "date": "April 30, 2016", "from": "Acme Inc., City, Street 3rd , +1 888 123-456,", "to": "Food Delivery Inc., New York, Some Street, 42", "lines": [{ "title": "Setting up new web-site", "quantity": 3, "price": 50 }, { "title": "Configuring mail server and mailboxes", "quantity": 5, "price": 50 }] }



Get 60 Day Free Trial

See also:


Get Your API Key

See also: