我有一个 Angular 7 应用程序,我想用 jsPDF 从中生成一些 PDF。我可以毫无问题地生成基于文本的 PDF,但是当我尝试添加 Acroform 字段时,会生成 PDF,但缺少字段。以下是重现问题的步骤:
-
使用 Angular-CLI 创建一个新的 Angular 应用程序:
ng new jspdfTester
cd jspdfTester
-
安装 jspdf
npm install -s jspdf
-
将 jspdf 代码添加到 app.component.ts 文件
import { Component } from '@angular/core'; import * as jsPDF from 'jspdf'; declare global { const TextField: any; } @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponent { title = 'jspdfTester'; constructor() { const doc = new jsPDF(); const textField = new TextField(); textField.Rect = [50, 50, 30, 10]; doc.addField(textField); doc.text('test', 50, 40); doc.save('sample.pdf'); } }
-
运行应用,将下载包含测试文本但没有文本字段的 PDF。
关于如何解决这个问题的任何想法?
编辑:经过仔细调试,看起来正在调用来自zone.js的Object.getOwnPropertyDescriptor方法,而不是浏览器的Object.getOwnPropertyDescriptor方法。显然他们的工作方式不同,它正在破坏jsPDF。仍然没有解决方案,但我越来越近了。
我想出了那个区域.js覆盖了 Object.defineProperty 设置,并更改了 jsPDF 在定义 AcroFrom 原型时尝试设置的某些属性的配置值。我不确定是否可以在不更改两个库之一的情况下解决此问题。我建议对jsPDF进行更改,因此也许在下一个版本中他们会修复它。