[Feature Request] API to Flatten PDF Forms
See original GitHub issueI haven’t been able to find any way to flatten a PDF form, and I’m integrating with an API (Lob) that can’t / won’t handle non-flattened forms (it won’t render the content of any acroFields). I’ve tried making the fields read-only, and I’ve set NeedAppearances to true but that doesn’t cut it for this one use-case.
Is there a way, even if complicated/hacky to flatten a PDF form with pdf-lib?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Filling and Flattening PDF Forms in Go - UniDoc
Fill a PDF form programmatically via input data in JSON format; Flatten a filled PDF and create an uneditable output PDF with no...
Read more >Fill, Flatten, and Email PDF Forms - Gnostice
PDFtoolkit VCL allows you to fill and flatten PDF form fields. It also supports automatic e-mailing of PDF documents. In this article, you...
Read more >Flatten That PDF with Telerik Document Processing
In this post, I will show you how to fill the values of a predefined template with interactive forms and how to flatten...
Read more >How to Flatten PDF Documents and Forms (Step-by-Step Guide)
Whether you want to flatten your PDF forms so the content within gets ... be required to pay a fee to subscribe for...
Read more >Flatten PDF API - PSPDFKit
Reduce development time with a Flatten PDF API that lets you flatten single or multiple PDFs in your iOS, Android, Web, and Desktop...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@patcon , thank you, very much appreciated. I must admit I am still trying to understand the magic of this. But in my case I am trying to put static text instead of the field (would have been very easy if pdf-lib could delete fields), not to have different values in same ids, so that approach would not work. @Hopding, thanks for clarification, I think I’ll try the approach you suggested as well.
Hello @VivekRajagopal!
Flattening a PDF form just means flattening each field in a PDF’s form. pdf-lib already has APIs to get a list of all fields in a form.
To flatten an individual form field, you need to first ensure that it has an appearance stream. Most software (including pdf-lib) used to fill form fields generate appearance streams. However, pdf-lib does have APIs to generate appearance streams for filled fields that do not already have them.
Once you’ve verified the form field has an appearance stream, you need to take the FormXObject containing the appearance stream and draw it at the end of the page’s content stream. pdf-lib has APIs to get the FormXObject of an appearance stream and APIs to draw a FormXObject on a page’s content stream.
Be sure that you draw the FormXObject in the correct location on the page’s content stream with the correct rotation.
There are a number of libraries written in other languages that you can use as reference implementations of form flattening. I would recommend looking at
pdfboxin particular. I used it as a reference when building the form creation/filling APIs for pdf-lib. Here’s a link to the source for theirPDAcroForm.flatten()method: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java#L226-L255I hope this helps! Please let me know if you have any additional questions.