Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] API to Flatten PDF Forms #554

Closed
mattboutet opened this issue Aug 6, 2020 · 9 comments · Fixed by #716
Closed

[Feature Request] API to Flatten PDF Forms #554

mattboutet opened this issue Aug 6, 2020 · 9 comments · Fixed by #716

Comments

@mattboutet
Copy link

I 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?

@blocks-master
Copy link

Same problem. Any update ?

@mattboutet
Copy link
Author

I wound up moving away from PDF forms entirely because of this and other issues. Instead, I'm using the form field coordinates and placing my text directly on the PDF, bypassing the acroFields entirely.

@BNovikov
Copy link

+1 on this one.

I would add that flattening would allow to merge pdfs with fields having same name but different value, which is currently not possible using pdf-lib.
Would love to see any solution for this.

@Hopding
Copy link
Owner

Hopding commented Oct 1, 2020

Hello @mattboutet @BNovikov!

pdf-lib does not currently have any APIs for flattening forms. However, such an API could certainly be build. It should be relatively easy to create (at least compared to other APIs that have been requested). I personally do not have time to work on it right now. But if anybody is interested in working on a PR for this, I'd be happy to answer questions and help get you started!

@Hopding Hopding changed the title Flattening PDF Forms? [Feature Request] API to flatten PDF Forms Oct 1, 2020
@Hopding Hopding changed the title [Feature Request] API to flatten PDF Forms [Feature Request] API to Flatten PDF Forms Oct 1, 2020
@VivekRajagopal
Copy link

Hello @mattboutet @BNovikov!

pdf-lib does not currently have any APIs for flattening forms. However, such an API could certainly be build. It should be relatively easy to create (at least compared to other APIs that have been requested). I personally do not have time to work on it right now. But if anybody is interested in working on a PR for this, I'd be happy to answer questions and help get you started!

Hi @Hopding - I would like to have a go at this feature. Can you please give me some pointers to get started :)

@blocks-master
Copy link

i ended up using node-pdftk to fill and flatten the file at the same time. Just fyi maybe u can take a look at their code.

@Hopding
Copy link
Owner

Hopding commented Oct 8, 2020

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 pdfbox in 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 their PDAcroForm.flatten() method: https://github.com/apache/pdfbox/blob/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java#L226-L255

I hope this helps! Please let me know if you have any additional questions.

@patcon
Copy link

patcon commented Oct 16, 2020

FYI @BNovikov: @Hopding already gave a wonderful answer here (#151 (comment)) that offers an alternative approach to why you're seeking to flatten -- renaming the fields from copied/merged pages :)

@BNovikov
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants