3 Steps for Creating PDF files using Python

Sisay A. Chala
1 min readDec 22, 2023

--

If you are like me, you may have wanted to automate some of your tasks like creating report from excel to pdf using python. After exploring a few alternatives, I decided to use a tool or library that can convert HTML or text content into a PDF file. I found one popular library called pdfkit that is used to create PDF in Python. Below are three simple steps to generate PDF file.

1) Prepare requirements

First, you need to install the pdfkit library. Open a terminal and run:

pip install pdfkit

For pdfkit to work, you need to first install wkhtmltopdf on the system. Because pdfkit library relies on the wkhtmltopdf binary to convert HTML or Markdown to PDF. Follow these instructions to install wkhtmltopdf on your system:

Linux:

sudo apt-get install wkhtmltopdf

Windows: Download the installer from the wkhtmltopdf website[1] and follow the installation instructions.

2) Write the python script to create PDF file

#generat_pdf.py
import pdfkit
note_content = """
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
"""
pdfkit.from_string(note_content, 'pdf_file.pdf')

3) Run the script:

python generate_pdf.py

This will generate a PDF file named pdf_file.pdf.

If you find this article useful, I will write follow up articles to automate document generation from databases, excel, json other data sources.

[1] https://wkhtmltopdf.org/downloads.html

--

--

Sisay A. Chala
Sisay A. Chala

Written by Sisay A. Chala

Sisay — A software engineering and data analytics enthusiast. Reach me at: www.linkedin.com/in/sisayie or https://www.researchgate.net/profile/Sisay_Chala

No responses yet