There is no direct way to do this that will work reliably. PDFs are not like HTML: they specify the positioning of text character-by-character. They may not even include the whole font used to render the text, just the characters needed to render the specific text in the document. No library I've found will do nice things like re-wrap paragraphs after updating the text. PDFs are for the most part a display-only format, so you'll be much better off using a tool that turns markup into a PDF than updating the PDF in-place.

If that's not an option, you can create a PDF form in something like Acrobat, then use a PDF manipulation library like iText (AGPL) or pdfbox, which has a nice clojure wrapper called pdfboxing that can handle some of that.

From my experience, Python's support for writing to PDFs is pretty limited. Java has, by far, the best language support. Also, you get what you pay for, so it would probably be worth paying for a iText license if you're using this for commercial purposes. I've had pretty good results writing python wrappers around PDF-manipulation CLI tools like pdfboxing and ghostscript. That will probably be much easier for your use case than trying to shoehorn this into Python's PDF ecosystem.

Answer from Lucas Wiman on Stack Overflow
🌐
IronPDF
ironpdf.com › ironpdf for python › ironpdf for python blog › using ironpdf for python › pdf from template
Generating a PDF from Template | IronPDF for Python
August 1, 2026 - This article will use IronPDF for Python to create simple PDF documents from templates. IronPDF is a powerful Python library that revolutionizes the way developers interact with PDF documents.
🌐
APITemplate.io
apitemplate.io › home › generate pdfs in python with 7 popular libraries in 2025
Generate PDFs in Python with 7 Popular Libraries in 2025 - APITemplate.io
December 17, 2024 - PDF Generation: The pisa.CreatePDF() method converts the HTML content into a PDF and saves it as output.pdf. This approach is especially useful if you’re working with existing HTML templates or web pages saved as HTML files.
Discussions

Generating PDF from some sort of template (jinja2) with headers, footers, images, not just a printed HTML document.
The basic ReportLab library is open source, the docs are here . Their layout language is called Platypus. It's unfortunately not very easy to get to grips with, though. More on reddit.com
🌐 r/learnpython
30
38
October 28, 2022
looking for an "low dependency" or pythonesque way to generate PDF's
Honestly all the good options I've found have required licenses, or are extremely low level, and a pain to use Eventually I always end up just outgrowing them and using a html -> pdf converter https://gotenberg.dev - this is what I use now, you can host your own and have all your clients use it via API calls (just post an html + CSS file and it returns a PDF) Alternatively just pop open a page in their default browser and have them print it to PDF More on reddit.com
🌐 r/Python
58
191
October 10, 2022
PDF generator, where should I start?
Hi, there are a lot of options for generating PDFs in Python. Here's a tutorial for you how you can create PDFs with pyfpdf . You might also want to look into generating a PDF from HTML using headless Chrome, which makes it much easier to design your template using HTML/CSS. Check out this pychromepdf library that might help: https://pypi.org/project/pychromepdf/ There's also a lot of hosted HTML => PDF APIs that can make this easier, because it can sometimes be complicated to get everything installed on your own server. Here's some examples: https://api2pdf.com https://browserless.io https://docraptor.com https://pdfshift.io I'm also working on a PDF generation tool called DocSpring , and we have a Python API client . DocSpring can fill out existing PDF forms with information, and we also have a HTML/CSS template creator that makes it easy to design and preview your PDF templates. (If you're a student or working on a hobby project then I'd be happy to set you up with a free developer account!) I hope that helps, and please let me know if you have any questions about PDFs! I've been working on DocSpring for the last few years, so I have a lot of experience with debugging weird PDF issues. More on reddit.com
🌐 r/learnpython
11
9
April 28, 2020
pdfme, the most powerful library in python to create PDF documents
What does this do better than existing PDF libraries? Eg: pdfminer, Py-pdf2, and others More on reddit.com
🌐 r/Python
98
534
July 15, 2021
People also ask

How can I generate a PDF from an HTML template using IronPDF?
You can generate a PDF from an HTML template by setting up an HTML structure with placeholders, rendering it with Python's jinja2 library, and using IronPDF's ChromePdfRenderer to convert the rendered HTML into a PDF.
🌐
ironpdf.com
ironpdf.com › ironpdf for python › ironpdf for python blog › using ironpdf for python › pdf from template
Generating a PDF from Template | IronPDF for Python
How do I install IronPDF for Python?
You can install IronPDF for Python by using pip, Python's package manager. Open your command-line interface and run the command `pip install ironpdf`.
🌐
ironpdf.com
ironpdf.com › ironpdf for python › ironpdf for python blog › using ironpdf for python › pdf from template
Generating a PDF from Template | IronPDF for Python
Can I use IronPDF to merge PDF documents in Python?
Yes, IronPDF offers a comprehensive set of tools and APIs that allow you to merge, split, and edit PDF documents in Python.
🌐
ironpdf.com
ironpdf.com › ironpdf for python › ironpdf for python blog › using ironpdf for python › pdf from template
Generating a PDF from Template | IronPDF for Python
Top answer
1 of 4
15

There is no direct way to do this that will work reliably. PDFs are not like HTML: they specify the positioning of text character-by-character. They may not even include the whole font used to render the text, just the characters needed to render the specific text in the document. No library I've found will do nice things like re-wrap paragraphs after updating the text. PDFs are for the most part a display-only format, so you'll be much better off using a tool that turns markup into a PDF than updating the PDF in-place.

If that's not an option, you can create a PDF form in something like Acrobat, then use a PDF manipulation library like iText (AGPL) or pdfbox, which has a nice clojure wrapper called pdfboxing that can handle some of that.

From my experience, Python's support for writing to PDFs is pretty limited. Java has, by far, the best language support. Also, you get what you pay for, so it would probably be worth paying for a iText license if you're using this for commercial purposes. I've had pretty good results writing python wrappers around PDF-manipulation CLI tools like pdfboxing and ghostscript. That will probably be much easier for your use case than trying to shoehorn this into Python's PDF ecosystem.

2 of 4
9

There is no definite solution but I found 2 solutions that works most of the time.

In python https://github.com/JoshData/pdf-redactor gives good results. Here is the example code:

# Redact things that look like social security numbers, replacing the
# text with X's.
options.content_filters = [
        # First convert all dash-like characters to dashes.
        (
                re.compile(u"Tom Xavier"),
                lambda m : "XXXXXXX"
        ),

        # Then do an actual SSL regex.
        # See https://github.com/opendata/SSN-Redaction for why this regex is complicated.
        (
                re.compile(r"(?<!\d)(?!666|000|9\d{2})([OoIli0-9]{3})([\s-]?)(?!00)([OoIli0-9]{2})\2(?!0{4})([OoIli0-9]{4})(?!\d)"),
                lambda m : "XXX-XX-XXXX"
        ),
]

# Perform the redaction using PDF on standard input and writing to standard output.
pdf_redactor.redactor(options)

Full Example can be found here

In ruby https://github.com/gettalong/hexapdf works for black out text. Example code:

require 'hexapdf'

class ShowTextProcessor < HexaPDF::Content::Processor

  def initialize(page, to_hide_arr)
    super()
    @canvas = page.canvas(type: :overlay)
    @to_hide_arr = to_hide_arr
  end

  def show_text(str)
    boxes = decode_text_with_positioning(str)
    return if boxes.string.empty?
    if @to_hide_arr.include? boxes.string
        @canvas.stroke_color(0, 0 , 0)

        boxes.each do |box|
          x, y = *box.lower_left
          tx, ty = *box.upper_right
          @canvas.rectangle(x, y, tx - x, ty - y).fill
        end
    end

  end
  alias :show_text_with_positioning :show_text

end

file_name = ARGV[0]
strings_to_black = ARGV[1].split("|")

doc = HexaPDF::Document.open(file_name)
puts "Blacken strings [#{strings_to_black}], inside [#{file_name}]."
doc.pages.each.with_index do |page, index|
  processor = ShowTextProcessor.new(page, strings_to_black)
  page.process_contents(processor)
end

new_file_name = "#{file_name.split('.').first}_updated.pdf"
doc.write(new_file_name, optimize: true)

puts "Writing updated file [#{new_file_name}]."

In this you can black out text on select text will be visible.

🌐
Apryse
docs.apryse.com › core › guides › generate-via-template › python
Generate PDF from a Template on Server/Desktop | Server SDK | Apryse
Launch Online Demo · The generation framework requires minimal code to get up and running. Simply initiate a normal Office conversion, but with the optional TemplateParamsJson parameter set: C# C++ Go · Java · JavaScript · Kotlin · Obj-C · Swift · PHP · Python · Ruby · VB · AskCopy · // Create a TemplateDocument object from an input office file.
🌐
Nutrient
nutrient.io › blog › sdk › how to generate pdf reports from html in python
Python HTML to PDF (2026): WeasyPrint setup, Django, and a high-volume API
August 10, 2026 - For the wider pipeline — extraction, validation, review, and downstream actions — see the AI document automation architecture guide. WeasyPrint is a solid open source option for basic HTML-to-PDF needs in Python. If you’re generating a ...
🌐
ConvertAPI
convertapi.com › template-to-pdf › python
Dynamic PDF Python SDK - Generate PDFs using Word templates and JSON
Dynamic PDF Python library is a tool that allows you to dynamically generate PDF documents based on a MS Word (DOCX) template by injecting custom properties using a JSON object that contains your data.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › generating pdf from some sort of template (jinja2) with headers, footers, images, not just a printed html document.
r/learnpython on Reddit: Generating PDF from some sort of template (jinja2) with headers, footers, images, not just a printed HTML document.
October 28, 2022 -

Hi!

I'm kinda losing my mind. This might be misplaced but it feels like I'm the only one with this problem.

I have to generate a PDF from some sort of template with user data. Our initial solution that made sense was Word form letters. The user, or our staff, can create a word document, get a CSV from a Django Rest Framework serializer, use that as a field source for the form letter, put those in and we fill that in with a python library and generate a pdf with libre office headless.

But now the requirements changed (or were different but product management didn't think that was an issue so it wasn't clearly communicated) and we need to have more logic in there than the form letters allow us to do. Like, "if this field is False just throw out those other fields". Basic Jinja conditionals.

But I literally can't find anything like this. There is ReportLab but they have "contact sales" prices for the version with some sort of template file we could use. And the project is not really warranting "contact sales" prices.

There's of course Latex but I'm the only person in the company that has any experience with Latex and the document should preferably look like a letter from our users so we need to implement some design elements that require a lot of fiddling with Latex and I'd have to do it but paying for an engineer to do the job of an intern is not a good idea.

All I find is basically printing HTML generated from a Jinja template but I just don't find anything that supports headers and footers. We need stuff like page numbers and disclaimers in the footer, company logo in the header and it looks like Latex is the only option.

Even Pandoc seems to require you to put some Latex in the metadata it will use to generate a header.

Am I just missing an obvious solution or is this really a very unsolved problem?

🌐
Codementor
codementor.io › community › create pdf files from templates with python and google scripts
Create PDF files from templates with Python and Google Scripts | Codementor
June 6, 2019 - This tutorial shows you how to build your own free template generator that combines the flexibility of Google Docs and the power of Python. Spend half an hour to build your own PDF templates and populate them with whatever data you need.
🌐
PDF.co API
apidocs.pdf.co › 26-pdf-from-html-template-python-generate-pdf-invoice-from-html-template
Generate PDF Invoice From HTML Template - Python | PDF.co API
December 20, 2024 - Sample HTML templates can be found at "https://github.com/bytescout/pdf-co-api-samples/tree/master/PDF from HTML template/TEMPLATES-SAMPLES" # 3. Save this new template # 4. Copy it’s ID to clipboard # 5. Now set ID of the template into “templateId” parameter # HTML template using built-in template # see https://app.pdf.co/templates/html/2/edit template_id = 2 # Data to fill the template file_read = open(".\\invoice_data.json", mode='r') TemplateData = file_read.read() file_read.close() # Destination PDF file name DestinationFile = ".\\result.pdf" def main(args = None): GeneratePDFFromTe
🌐
CraftMyPDF
craftmypdf.com › home › how to generate pdfs with python, pdfkit and craftmypdf
How to Generate PDFs with Python, PDFKit and CraftMyPDF - CraftMyPDF.com
July 24, 2022 - The easiest way to create PDFs is to render PDFs from HTML templates. wkhtmltopdf is an open-source command-line tool that renders HTML into PDF with the Qt WebKit rendering engine.
🌐
Apryse
docs.apryse.com › documentation › python › guides › generate-via-template
PDF Generation using Template with Server/Desktop | Apryse documentation
Simply initiate a normal Office conversion, but with the optional TemplateParamsJson parameter set: C#C++GoJavaJavaScriptObj-CPHPPythonRubyVB · 1// Create a TemplateDocument object from an input office file.
🌐
Medium
medium.com › @frederic.henri › generate-pdf-invoice-from-html-using-python-and-jinja-08fb401a90e3
Generate PDF Invoice from HTML Template using Python and Jinja engine | by Frederic Henri | Medium
June 15, 2026 - The return is Chrome-quality rendering, no system binary to manage, no intermediate files, and the same library covering PDF merging, signing, form filling, and text extraction if those come up later. The Jinja2 templating layer — the more interesting half of this article — is identical either way. Try IronPDF free for 30 days 👉 ironpdf.com/python
🌐
Edocgen
edocgen.com › examples › python-pdf-generation
Template-Based PDF Document Generation in Python
Python is a programming language known for its flexibility and effectiveness in automating tasks. EDocGen allows users to generate PDF documents in Python using templates. With it, businesses can quickly and easily generate thousands of documents from various data sources such as JSON, XML, ...
🌐
Nutrient
nutrient.io › blog › sdk › top 10 ways to generate pdfs in python
Top 10 Python PDF generator libraries: Complete guide for developers (2026)
2 weeks ago - Python’s standard library doesn’t include a PDF generator. Use a third-party library such as fpdf2 for basic documents, ReportLab for reports, or WeasyPrint for HTML templates. Which Python PDF library should I use for simple PDFs?
🌐
GeeksforGeeks
geeksforgeeks.org › python › building-a-background-pdf-generation-app-using-python-celery-and-template-data
Building a Background PDF Generation App Using Python Celery and Template Data - GeeksforGeeks
July 23, 2025 - Define routes to trigger PDF generation tasks. ... from flask import Flask, render_template from tasks import generate_pdf app = Flask(__name__) # Load Celery configuration app.config.update( CELERY_BROKER_URL='redis://localhost:6379/0', CELERY_RESULT_BACKEND='redis://localhost:6379/0' ) # Initialize Celery from celery import Celery celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) @app.route('/') def index(): return render_template('index.html') @app.route('/generate-pdf') def trigger_generate_pdf(): result = generate_pdf.delay() return f'PDF generation task queued!
🌐
Python GUIs
pythonguis.com › examples › python pdf report generator
Generate Custom PDF Reports with Python using ReportLab & pdfrw
April 8, 2026 - Learn how to build a Python PDF report generator using reportlab and pdfrw. Create a desktop GUI with PyQt or PySide to fill PDF templates, overlay text on existing PDFs, and batch generate reports from CSV files.
🌐
DEV Community
dev.to › custodiaadmin › how-to-generate-pdf-reports-from-html-templates-in-python-3c05
How to Generate PDF Reports from HTML Templates in Python - DEV Community
March 25, 2026 - Generate PDF reports from Jinja2 templates in Python. Render HTML → API call → PDF saved. Works with Django, Flask, and any Python app.
🌐
Templated
templated.io › blog › generate-pdfs-in-python-with-libraries
How to Generate PDFs in Python: 8 Tools Compared (Updated for 2025)
September 16, 2024 - In this updated guide for 2025, we’ll explore how to generate PDFs in Python using popular libraries including FPDF2, ReportLab, Playwright, Pyppeteer, Python-Wkhtmltopdf, PDFKit, WeasyPrint, and Borb. We’ll highlight key differences, performance metrics, and recent updates to help you choose the best option for your specific needs. Note: If you’re specifically interested in generating PDFs from HTML, check out our detailed guide: Convert HTML to PDF with Python.
🌐
Real Python
realpython.com › creating-modifying-pdf
Create and Modify PDF Files in Python – Real Python
March 3, 2026 - Creating and modifying PDF files in Python is straightforward with libraries like pypdf and ReportLab. You can read, manipulate, and create PDF files using these tools. pypdf lets you extract text, split, merge, rotate, crop, encrypt, and decrypt ...