![]() |
VOOZH | about |
Creating payment receipts is a common task, whether for an e-commerce website, a local store, or any business. In this article, we will learn how to generate transaction receipts as PDF files using Python.
We will use the reportlab library to create PDFs with tables, styles and headings.
Install "ReportLab" library using the following pip command:
pip install reportlab
1. Import Required Modules: Import all the Python libraries needed to create the PDF, tables, and styles.
2. Prepare Data: The data for the receipt is organized in a list of lists. The first row contains the table headers.
3. Create PDF Template: We create a base PDF document named receipt.pdf and set its page size to standard A4 dimensions.
4. Create Styles: Use ReportLab’s built-in sample style sheet and adjust the heading style.
Here, alignment = 1 centers the title on the page.
5. Create the Title: Create a title paragraph for the PDF.
6. Create Table Style: Define the style for the table, including borders, grid lines, header color, text alignment, and background color.
Explanation:
7. Create the Table: Create the table object and apply the style.
8. Build the PDF: Combine the title and table and build the PDF.
Output