Introduction to ReportLab
ReportLab is a powerful and flexible library in Python used for generating PDFs. Whether you’re creating invoices, reports, or any document-based application, ReportLab offers a comprehensive set of tools to get the job done efficiently.
Getting Started with ReportLab
First, you need to install the ReportLab library using pip:
pip install reportlab
Creating a Basic PDF Document
The following code demonstrates how to create a basic PDF document using ReportLab:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
def create_pdf(filename):
c = canvas.Canvas(filename, pagesize=letter)
c.drawString(100, 750, "Welcome to ReportLab!")
c.save()
create_pdf("example_basic.pdf")
Using Different Fonts
ReportLab supports a variety of fonts. Here’s how you can change the font in your PDF:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
def create_pdf_with_fonts(filename):
c = canvas.Canvas(filename, pagesize=letter)
pdfmetrics.registerFont(TTFont('Arial', 'Arial.ttf'))
c.setFont('Arial', 12)
c.drawString(100, 750, "This is Arial font.")
c.save()
create_pdf_with_fonts("example_fonts.pdf")
Adding Images to Your PDF
You can easily add images to your PDFs with ReportLab:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
def create_pdf_with_image(filename):
c = canvas.Canvas(filename, pagesize=letter)
c.drawImage("example_image.jpg", 100, 600, width=200, height=150)
c.save()
create_pdf_with_image("example_image.pdf")
Creating Tables
Tables are a great way to organize data in PDFs:
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.lib import colors
def create_pdf_with_table(filename):
doc = SimpleDocTemplate(filename, pagesize=letter)
data = [['Name', 'Age', 'City'],
['Alice', '30', 'New York'],
['Bob', '25', 'Los Angeles'],
['Charlie', '35', 'Chicago']]
table = Table(data)
style = TableStyle([('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.beige)])
table.setStyle(style)
elements = [table]
doc.build(elements)
create_pdf_with_table("example_table.pdf")
Comprehensive App Example
Here is an example of a complete application that generates a report containing text, images, and a table:
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.lib import colors
def create_full_report(filename):
doc = SimpleDocTemplate(filename, pagesize=letter)
styles = getSampleStyleSheet()
story = []
# Title
story.append(Paragraph("Monthly Sales Report", styles['Title']))
# Spacer
story.append(Spacer(1, 0.2 * inch))
# Image
story.append(Image("example_image.jpg", 2 * inch, 2 * inch))
# Spacer
story.append(Spacer(1, 0.2 * inch))
# Table
data = [['Product', 'Quantity', 'Price'],
['Widget A', '100', '$1500'],
['Widget B', '50', '$3000'],
['Widget C', '200', '$2000']]
table = Table(data)
style = TableStyle([('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.beige)])
table.setStyle(style)
story.append(table)
doc.build(story)
create_full_report("full_report.pdf")
With these examples, you should have a strong foundation for using ReportLab to generate a variety of PDF documents tailored to your needs.
Hash: d96efdce458f2a4bb2d62785b8dc9ee776da89f83246513c6a8c0c53ae81abb7