Use ReportLab, python to generate PDF with one click!

Hello everyone, I am Payson sauce.

Today I received a request to export a PDF report from the application system. This report contains text paragraphs, dynamic tables, and pictures.

The backend of our system is using Java, so I implemented the functionality with itextpdf.

After I came back, I thought that there should be a similar package in python to implement it, so I did some exploration.

Today I will tell you how to use reportlab to generate a PDF with both pictures and text.

There are easter eggs at the end of the article!

Install ReportLab

First, we need to install ReportLab. You can use the pip command to install:

pip install reportlab

Create PDF documents

Before we start creating PDF documents, we need to import ReportLab's Canvas class:

from reportlab.pdfgen import canvas

Next, we can create a Canvas object, specifying the name and path of the PDF file:

pdf_name = "my_tech_article.pdf"
pdf = canvas.Canvas(pdf_name)

add text

We can add text to a PDF document using the drawString() method:

pdf.drawString(50, 750, "My Technical Article")

The first two parameters of this method are the x and y coordinates of the text, and the third parameter is the text content.

add image

We can add an image to a PDF document using the drawImage() method:

from reportlab.lib.pagesizes import letter

pdf.drawImage("my_image.png", 50, 650, width=letter[0], height=letter[1]/2)

The first three parameters of this method are the x and y coordinates of the image, the fourth parameter is the width of the image, and the fifth parameter is the height of the image.

add form

We can use the Table class to add tables. First, you need to import the Table class:

from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.lib.units import inch
from reportlab.platypus import Table

data = [    ["Name", "Age", "Gender"],
    ["John", "25", "Male"],
    ["Sarah", "30", "Female"],
    ["Tom", "35", "Male"],
]

table = Table(data)
table.setStyle(
    [
        ("BACKGROUND", (0, 0), (-1, 0), colors.grey),
        ("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
        ("ALIGN", (0, 0), (-1, 0), "CENTER"),
        ("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
        ("FONTSIZE", (0, 0), (-1, 0), 14),
        ("BOTTOMPADDING", (0, 0), (-1, 0), 12),
        ("BACKGROUND", (0, 1), (-1, -1), colors.beige),
        ("TEXTCOLOR", (0, 1), (-1, -1), colors.black),
        ("ALIGN", (0, 1), (-1, -1), "CENTER"),
        ("FONTNAME", (0, 1), (-1, -1), "Helvetica"),
        ("FONTSIZE", (0, 1), (-1, -1), 12),
        ("BOTTOMPADDING", (0, 1), (-1, -1), 6),
        ("GRID", (0, 0), (-1, -1), 1, colors.black),
    ]
)

Summarize

Secretly tell everyone, the above content is written by ChatGPT, isn’t it very good?

Exchange group

After a lapse of 2 months, the Moyu learning and exchange group is open again for a limited time.

b2902571e37ee1156a18849eeb152fd9.png

The Python technical exchange group (mainly technical exchange, fishing, free prostitution courses) is open from time to time. Interested friends can reply in the official account below: 666, you can enter, and we will plan for 100 days  together !

Old rules , do you still remember, click "Looking" in the lower right corner , if you think the content of the article is good, remember to share it in Moments to let more people know!

64e5d5943c7af9329bb19c143edfe7f7.gif

[ How to obtain the mysterious gift package ]

Identify the official account below, reply: 1024

37c68784dbb3d5b2b93d5b07030d5208.jpeg

Guess you like

Origin blog.csdn.net/weixin_48923393/article/details/129173640