问题描述
我使用 PDF-lib (https://www.npmjs.com/package/pdf-lib) 成功创建了一个可填写的表单。
但是我在展平它时遇到以下错误。
未处理的拒绝(错误):未能找到元素 mahesh_Signature_0 的未定义页面
此处mahesh_Signature_0是文本字段名称。
这是我用来展平它的简单代码。
const formPdfBytes = await fetch(file).then(res => res.arrayBuffer())
const pdfDoc = await PDFDocument.load(formPdfBytes)
const form = pdfDoc.getForm()
form.flatten()
我该如何解决这个问题?在 JavaScript 中展平 PDF 注释的任何替代解决方案?
解决方法
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()