Prompt
How do I fill out PDF forms with data from my database?
Latest observation
Oct 12, 2026
OpenAI APIWeb search: off
You typically do this in one of two ways:
1) Fill a PDF form template programmatically
If the PDF already has form fields (AcroForm), you can load it, map database values to field names, and save a filled copy.
Common tools
- Python:
pypdf,pdfrw,PyMuPDF - Java: iText, PDFBox
- Node.js:
pdf-lib - .NET: iText, PDFsharp, Syncfusion, Telerik
Basic workflow
- Get the record from your database.
- Open the PDF form template.
- Set each field value by field name.
- Save the filled PDF.
- Optionally flatten the form so it can’t be edited.
Example in Python with pypdf
from pypdf import PdfReader, PdfWriter
# Example database record
data = {
"first_name": "Ada",
"last_name": "Lovelace",
"email": "ada@example.com"
}
reader = PdfReader("template.pdf")
writer = PdfWriter()
writer.clone_document_from_reader(reader)
# Fill form fields
writer.update_page_form_field_values(writer.pages[0], {
"FirstName": data["first_name"],
"LastName": data["last_name"],
"Email": data["email"]
})
# Write output
with open("filled.pdf", "wb") as f:
writer.write(f)
2) Generate the PDF from scratch
If your PDF is not an interactive form, you can create a new PDF and place text at fixed positions.
Common tools
- Python: ReportLab
- Java: PDFBox, iText
- Node.js: pdfkit, puppeteer
- .NET: QuestPDF, iText
This is better when:
- You control the layout
- The document isn’t an existing form
- You want full automation and consistency
Important considerations
Field names
PDF form field names must match exactly. You can inspect them with tools like:
pdftk template.pdf dump_data_fieldspdfinfo/qpdf- script-based field enumeration
Flattening
If you want the values to be non-editable:
- Flatten after filling, or
- Save as a static PDF
Fonts and appearance
Sometimes filled fields look blank in some PDF viewers unless appearance streams are updated. Many libraries have options like:
- regenerate appearances
- auto-appearance
- flatten form
Special field types
- Checkboxes/radio buttons need the correct on-value
- Dropdowns must use valid option values
- Multi-line text fields may need wrapping or font sizing
Typical architecture
- User requests a document.
- Backend fetches DB record.
- Backend fills PDF template.
- Return PDF as download or store it in S3/blob storage.
If you tell me:
- your language/framework
- whether the PDF is an existing form or a custom document
- your database type
…I can give you a ready-to-run example.