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

Form field content gets lost after flatten #86

Open
2 tasks done
equada opened this issue Jan 21, 2025 · 4 comments
Open
2 tasks done

Form field content gets lost after flatten #86

equada opened this issue Jan 21, 2025 · 4 comments
Labels
bug Something isn't working needs-triage

Comments

@equada
Copy link

equada commented Jan 21, 2025

What were you trying to do?

Flatten an uploaded pdf file. The key difference is how the form was filled beforehand.

How did you attempt to do it?

const doc= await PDFDocument.load(buffer);
const form = doc.getForm()
form.flatten()

What actually happened?

The "Name" field is empty after flatten

What did you expect to happen?

All fields should keep their value

How can we reproduce the issue?

https://jsfiddle.net/dwvs6tbg/1/

Steps to reproduce:

  • Open adobe acrobat pdf and see that the Name field is not empty
  • Open fiddle
  • Click on file upload
  • Select the adobe acrobat file below
  • flattened pdf should download
  • name field should be empty

This is the original pdf file
sample_pdf.pdf

This is the pdf file filled out using chrome browser --> "Name" field keeps it's value after flatten
sample_pdf_chrome.pdf

This is the pdf file filled out using Adobe Acrobat --> "Name" field is empty after flatten
sample_pdf_acrobat.pdf

Version

2.2.4

What environment are you running pdf-lib in?

Browser

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

No response

@equada equada added bug Something isn't working needs-triage labels Jan 21, 2025
@giox069
Copy link

giox069 commented Jan 21, 2025

I was just fighting against a very similar problem: I have a PDF with a form, and a field named "fieldname"

const doc= await PDFDocument.load(buffer);
const form = doc.getForm()
form.getTextField('fieldname').setText('Hello');
const pdfBytes = await pdfDoc.save();

works, and creates a new PDF with a form with the 'fieldname' field correctly dispalying Hello.

But when I add form->flatten(); just before save(), the "Hello" text disappears from the resulting PDF.

@Sharcoux
Copy link
Collaborator

What is flatten supposed to be doing exactly?

@equada
Copy link
Author

equada commented Jan 22, 2025

What is flatten supposed to be doing exactly?

https://pdf-lib.js.org/docs/api/classes/pdfform#flatten

flatten
▸ flatten(options: FlattenOptions): void

Defined in api/form/PDFForm.ts:537

Flatten all fields in this PDFForm.

Flattening a form field will take the current appearance for each of that field's widgets and make them part of their page's content stream. All form fields and annotations associated are then removed. Note that once a form has been flattened its fields can no longer be accessed or edited.

This operation is often used after filling form fields to ensure a consistent appearance across different PDF readers and/or printers. Another common use case is to copy a template document with form fields into another document. In this scenario you would load the template document, fill its fields, flatten it, and then copy its pages into the recipient document - the filled fields will be copied over.

@Sharcoux
Copy link
Collaborator

Then I wonder if it's not related to the same question raised here: #80

Where are the state data being stored before we call save()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage
Projects
None yet
Development

No branches or pull requests

3 participants