Python generate PDF
Generate PDF with Python
In our daily work, we often need to save or share content such as text, data, and charts in PDF format. There are many third-party libraries in Python that can help us generate PDF files, such as reportlab
, fpdf
, and PyPDF2
. This article will introduce how to use the reportlab
library to generate PDF files and demonstrate some basic operations.
Installing the reportlab Library
First, we need to install the reportlab
library. Execute the following command in the command line:
pip install reportlab
Once the installation is complete, we can begin using the reportlab
library to generate PDF files.
Generating a Simple PDF File
Here, we’ll demonstrate how to generate a simple PDF file containing a paragraph of text and a table.
from reportlab.pdfgen import canvas
# Create a PDF file
c = canvas.Canvas("example.pdf")
# Set the font and font size
c.setFont("Helvetica", 12)
# Write text
c.drawString(100, 700, "Hello, World!")
# Draw a table
c.grid(range(100, 600, 100), range(600, 100, -100))
# Save the PDF file
c.save()
In the above code, we first import the canvas
class, then create a PDF file and set the font and font size. Next, we use the drawString
method to write some text, and finally use the grid
method to draw a simple table. Finally, we call the save
method to save the PDF file.
Running the above code will generate a PDF file named example.pdf
in the current directory. Open the file to see the result.
Adding Images and Charts
In addition to text and tables, we can also add https://coder-cafe.com/wp-content/uploads/2025/09/images and charts to PDF files. The following example demonstrates how to add an image and a bar chart to a PDF file.
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics.shapes import Drawing
# Create a PDF file
c = canvas.Canvas("example2.pdf")
# Add an image
c.drawImage("example_https://coder-cafe.com/wp-content/uploads/2025/09/image.jpg", 100, 500, width=2*inch, height=2*inch)
# Create a bar chart
data = [(10, 20, 30, 40),]
drawing = Drawing(400, 200)
bc = VerticalBarChart()
bc.x = 50
bc.y = 50
bc.height = 125
bc.width = 300
bc.data = data
drawing.add(bc)
# Add the chart to the PDF file
drawing.saveState()
drawing.drawOn(c, 100, 300)
drawing.restoreState()
# Save the PDF file
c.save()
In the above code, we first use the drawImage
method to add an image to the PDF file. We then create a bar chart and add it to the PDF file. Finally, we save the PDF file.
After running the above code, a PDF file named example2.pdf
will be generated in the current directory. Open the file to see the added image and bar chart.
Adding Headers and Footers
Sometimes, we need to add headers and footers to PDF files to better organize the content. The following example demonstrates how to add headers and footers to a PDF file.
from reportlab.pdfgen import canvas
# Create a PDF file
c = canvas.Canvas("example3.pdf")
# Add a header
c.setFont("Helvetica", 10)
c.drawString(100, 800, "Page Header")
# Add a footer
c.setFont("Helvetica", 10)
c.drawString(100, 50, "Page Footer")
# Save the PDF file
c.save()
In the above code, we use the drawString
method to add a header and footer to the PDF file. Finally, we save the PDF file.
After running the above code, a PDF file named example3.pdf
will be generated in the current directory. Open the file to see the added header and footer.
Conclusion
By using the reportlab
library, we can easily generate PDF files containing text, https://coder-cafe.com/wp-content/uploads/2025/09/images, tables, charts, and other content. This article introduces some basic operations. Readers can further explore the reportlab
library’s capabilities based on their actual needs.