尝试使用 pdf-lib 来填写 pdf 表单字段。扁平化和保存表单时遇到问题

SSc*_*tti 5 javascript pdf json pdf-form

我正在研究使用 pdf-lib 库表单来操作 pdf 表单。该包位于此处: https: //github.com/Hopding/pdf-lib

再次编辑这个问题(它已经存在几个月了,没有人接受 2 个 100 的赏金),只包含一个代码片段,该代码片段在获得正确的脚本标签库后至少可以部分工作。

我遇到的问题是,在编辑并完成预填充表单后,我希望能够在进行任何在线编辑后压平并保存表单。

这是我在 pdf-lib 存储库中作为问题在 Github 上发布的一些示例https://github.com/Hopding/pdf-lib/issues/965

$("#flatten").on("click", function(e) {

async function Flatten() {

 pdfDoc = await PDFLib.PDFDocument.load(document.getElementById('pdf').src)
 form = pdfDoc.getForm()
 form.updateFieldAppearances();
  checkboxes.forEach(function (item, index) {
    checkBox = form.getCheckBox(item);
    checkBox.defaultUpdateAppearances()
  });
 form.flatten()
 pdfBytes = await pdfDoc.saveAsBase64({ dataUri: true })
 document.getElementById('pdf').src = pdfBytes
}

Flatten();
});
    
    
$("#pdf").on("load", function(e) {
  autoResize($(this));
});
    
function autoResize(iframe) {
    
  // iframe.height($(iframe).contents().find('html').height());
  iframe.height("1650px");
  iframe.width("1275px");
  iframe.show();
}
    
        
$("#saveform").on("click", function(e) {
    download(document.getElementById('pdf').src, SaveAs+'.pdf', "application/pdf");
});

$("#sendform").on("click", function(e) {
    alert(document.getElementById('pdf').src);
    //download(document.getElementById('pdf').src, SaveAs+'.pdf', "application/pdf");
});
Run Code Online (Sandbox Code Playgroud)

GitHub 问题帖子中附有一个 archive.zip。如果您有兴趣,我建议您将这两个文件放在服务器上以查看问题所在。 如果您将 test.html 文件和 PDF 放在网络服务器上的同一目录(服务器的根目录)中,然后在浏览器中打开它,那么它就可以工作。如果您在字段中输入内容,然后尝试拼合,系统会要求您留在页面上或离开。这就是我想尝试解决的问题。