Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.93k stars 664 forks source link

Cannot remove multiline form field #1168

Open jelizarovas opened 2 years ago

jelizarovas commented 2 years ago

What were you trying to do?

I am trying to merge pdf documents with form fields. It does not work with forms very well as they become very unstable in the merge document, or disappear. My workaround is to get all of the form data, strip all the form fields, merge all the documents and try to apply the form data back on the merged document. Multiline text fields fail on form.removeField(field)

How did you attempt to do it?

Running node v14.18.3 multiline.pdf

module.exports.removeFormFieldsPDF = async (filePath = './multiline.pdf') => {
  const pdfDoc = await PDFDocument.load(fs.readFileSync(filePath));
  const pages = pdfDoc.getPages();
  const form = pdfDoc.getForm();
  const formFields = form.getFields();
  try {
    for (const field of formFields) {
        form.removeField(field);
    }
  } catch (error) {
    console.log(error);
  }
  //SEEMS TO NOT BE ABLE TO REMOVE Multiline textfields
  fs.writeFileSync("../build/test.pdf", await pdfDoc.save());
};

What actually happened?

Error: Unexpected N type: undefined
    at PDFWidgetAnnotation.PDFAnnotation.getNormalAppearance (C:\dev\apps\pts-forms\node_modules\pdf-lib\cjs\core\annotation\PDFAnnotation.js:60:15)
    at PDFForm.findWidgetAppearanceRef (C:\dev\apps\pts-forms\node_modules\pdf-lib\cjs\api\form\PDFForm.js:608:32) 
    at PDFForm.removeField (C:\dev\apps\pts-forms\node_modules\pdf-lib\cjs\api\form\PDFForm.js:489:34)
    at module.exports.removeFormFieldsPDF (C:\dev\apps\pts-forms\utils\pdf.js:195:14)
(node:13664) UnhandledPromiseRejectionWarning: Error: Expected instance of PDFDict or PDFStream, but got instance of undefined

What did you expect to happen?

Remove multi line text field.

How can we reproduce the issue?

Run multiline.pdf With my function, and it will produce an error

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

No response

jelizarovas commented 2 years ago

image

StepanSS commented 2 years ago

i didn't solve the problem with remove multiline form field so i removed all forms with

const form = pdfDoc.getForm();
form flatten();

more details here https://pdf-lib.js.org/docs/api/classes/pdfform#flatten maybe it will be useful for someone

enoh-barbu commented 1 year ago

I found a solution which might work also for you

while (field.acroField.getWidgets().length) {
    field.acroField.removeWidget(0);
}
form.removeField(field);
masotrix commented 1 year ago

I found a solution which might work also for you

while (field.acroField.getWidgets().length) {
    field.acroField.removeWidget(0);
}
form.removeField(field);

Thanks This allowed me to remove all types of fields

JandenMa commented 9 months ago

I found a solution which might work also for you

while (field.acroField.getWidgets().length) {
    field.acroField.removeWidget(0);
}
form.removeField(field);

Excellent 👍 Thank you!!

S1lander commented 8 months ago

I found a solution which might work also for you

while (field.acroField.getWidgets().length) {
    field.acroField.removeWidget(0);
}
form.removeField(field);

OMG yes! I owe you.