3

I successfully created a fillable form using PDF-lib (https://www.npmjs.com/package/pdf-lib).

But I am getting following error while flattening it.

Unhandled Rejection (Error): Failed to find page undefined for element mahesh_Signature_0

Here mahesh_Signature_0 is a text field name.

This is my simple code for flattening it.

const formPdfBytes = await fetch(file).then(res => res.arrayBuffer())
const pdfDoc = await PDFDocument.load(formPdfBytes)
const form = pdfDoc.getForm()
form.flatten()

How can I resolve this? Any alternate solution to flatten annotations of PDF in JavaScript?

stm
  • 662
  • 1
  • 6
  • 23
Blackhawk
  • 57
  • 1
  • 5

1 Answers1

0
const someVar = await form.save();
then download, view from the array

My guess from snippet of code seen.

import { PDFDocument } from 'pdf-lib'

// This should be a Uint8Array or ArrayBuffer
// This data can be obtained in a number of different ways
// If your running in a Node environment, you could use fs.readFile()
// In the browser, you could make a fetch() call and use res.arrayBuffer()
const formPdfBytes = ...

// Load a PDF with form fields
const pdfDoc = await PDFDocument.load(formPdfBytes)

// Get the form containing all the fields
const form = pdfDoc.getForm()

// Fill the form's fields
form.getTextField('Text1').setText('Some Text');

form.getRadioGroup('Group2').select('Choice1');
form.getRadioGroup('Group3').select('Choice3');
form.getRadioGroup('Group4').select('Choice1');

form.getCheckBox('Check Box3').check();
form.getCheckBox('Check Box4').uncheck();

form.getDropdown('Dropdown7').select('Infinity');

form.getOptionList('List Box6').select('Honda');

// Flatten the form's fields
form.flatten();

// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save()