jsPDF Acroforms 在 Angular 应用程序中不工作

jsPDF Acroforms not working in Angular app

我有一个 Angular 7 应用程序,我想使用它的 jsPDF 生成一些 PDF。我可以毫无问题地生成基于文本的 PDF,但是当我尝试添加 Acroform 字段时,生成了 PDF,但字段丢失了。以下是重现问题的步骤:

  1. 使用 Angular-CLI 创建一个新的 Angular 应用程序:

    ng new jspdfTester

    cd jspdfTester

  2. 安装jspdf

    npm install -s jspdf

  3. 将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');
      }
    }
    
  4. 运行 将下载包含测试文本但没有文本字段的应用程序和 PDF。

关于如何解决这个问题有什么想法吗?

编辑: 仔细调试后,看起来 zone.js 中的 Object.getOwnPropertyDescriptor 方法被调用,而不是浏览器的 Object.getOwnPropertyDescriptor 方法。显然他们的工作方式不同并且它破坏了 jsPDF。仍然没有解决方案,但我越来越接近了。

我发现 zone.js 覆盖了 Object.defineProperty 设置并更改了 jsPDF 在定义 AcroFrom 原型时尝试设置的一些属性的配置值。我不确定是否可以在不更改两个库之一的情况下解决此问题。我有 proposed a change to jsPDF 所以也许在下一个版本中他们会修复它。