Home > database >  How to fill a pdf with python
How to fill a pdf with python

Time:01-13

I don't have a concrete project yet, but in anticipation I would like to know if it is possible to fill a pdf with data stored in mysql?

It would be a question of a form with several lines and column history not to simplify the thing... If yes, what technology/language to use?

I found several tutorials which however start from a blank pdf. I have the constraint of having to place the data in certain specific places.

CodePudding user response:

Try using PyFPDF or ReportLab to create and manipulate PDF documents in Python.

CodePudding user response:

Using PyMuPDF makes it easy to place text at any desired position using any desired font.

To e.g. put some text on a page starting at position (x, y) do this:

import fitz  # import the package
doc = fitz.open("your.pdf")
page = doc[0]  # desired page, here: page 0
# insert starti 50 points from left, 36 points from top
page insert_text((50, 36), text)  # insert starts here

Or insert into a rectangle with automatic line breaks at word boundaries

rect = fitz.Rect(50, 36, 150, 236)  # define rectangle
page.insert_textbox(rect, text)

Rectangles are defined by their top-left (50,36) and bottom-right (150, 236) coordinates. So in the above case a 100 x 200 rectangle.

  • Related