【问题标题】:JSPDF and html2canvas with Angular带有 Angular 的 JSPDF 和 html2canvas
【发布时间】:2019-07-29 07:56:13
【问题描述】:

我正在尝试使用 html2canvas 生成 PDF,但是在调用该函数时抛出错误,我的 TS 如下:

  @ViewChild('content') content:ElementRef;

  generarPDF() {
    html2canvas(document.getElementById('content'), {
      // Opciones
      allowTaint: true,
      useCORS: false,
      // Calidad del PDF
      scale: 1
    }).then(function(canvas) {
      var img = canvas.toDataURL("image/png");
      var doc = new jsPDF();
      doc.addImage(img,'PNG',7, 20, 195, 105);
      doc.save('postres.pdf');
    });
  }

我的html是:

div.cliente {
    border-bottom: 2px dotted;
    width: 70%;
    float: left;
}

div.fecha {
    border-bottom: 2px dotted;
    width: 28%;
    float: right;
}

ul.satisfacion {
    float: left;
    margin-top: 28px;
}
  <div #content id="content">
    <div class="contenido-pdf">
     
      <div class="cliente">
        <span>Cliente: </span>
        <div class="campo"></div>
      </div>
      <div class="fecha">
          <span>Fecha: </span>
          <div class="campo"></div>
        </div>
      <ul class="satisfacion">
        <li>El servicio es muy satisfactorio</li>
        <li>El servicio es satisfactorio</li>
        <li>El servico es normal</li>
        <li>El servicio no es satisfactorio</li>
      </ul>
    </div>
  </div>
  <button mat-raised-button (click)="generarPDF()" id="donwloadpdf">
  DESCARGAR CARTA DE CONFORMIDAD
</button>

我的错误

ERROR Error: "Uncaught (in promise): Error: Supplied Data is not a valid base64-String jsPDF.convertStringToImageData 
e.convertStringToImageData

当我要单击生成 pdf 时,它不会生成它或执行任何操作,它只会在我的控制台上抛出该错误,我已经尝试使用 jsPDF 生成它,但问题是它不保留样式

【问题讨论】:

  • 你用的是什么版本的jspdf和html2canvas?

标签: angular jspdf html2canvas


【解决方案1】:

您的 css 正在做一些奇怪的事情,使带有 id="content" 的 div 没有内容。

你可以看到这个,如果你把

#content {
  outline: 1px solid red;
}

我已经注释掉了你的 css 并生成了以下Working Stackblitz

取消注释 css 以查看问题。

重现 stackblitz 的步骤

angular.json

"scripts": [
  "node_modules/html2canvas/dist/html2canvas.min.js",
  "node_modules/jspdf/dist/jspdf.min.js"
]

依赖关系

  1. 我通过使用html2canvas@1.0.0-rc.1 避免了 html2canvas 的问题。看 - https://github.com/niklasvh/html2canvas/issues/1896

  2. 最新版本的 jspdf 无法解决文件保护程序的一些问题,所以我选择了我知道可以使用的版本 jspdf@1.4.1

  3. 包括类型@types/html2canvas@types/jspdf

  4. 出于某种原因,在 stackblitz 中您需要使用

  5. 导入这些
import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';

app.component.ts

import { Component, ViewChild, ElementRef } from '@angular/core';

import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';

 @ViewChild('content', { 'static': true }) content:ElementRef;

  generarPDF() {

    const div = document.getElementById('content');
    const options = {
      background: 'white',
      scale: 3
    };

    html2canvas(div, options).then((canvas) => {

      var img = canvas.toDataURL("image/PNG");
      var doc = new jsPDF('l', 'mm', 'a4', 1);

      // Add image Canvas to PDF
      const bufferX = 5;
      const bufferY = 5;
      const imgProps = (<any>doc).getImageProperties(img);
      const pdfWidth = doc.internal.pageSize.getWidth() - 2 * bufferX;
      const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
      doc.addImage(img, 'PNG', bufferX, bufferY, pdfWidth, pdfHeight, undefined, 'FAST');

      return doc;
    }).then((doc) => {
      doc.save('postres.pdf');  
    });
  }

}

如果有人遇到改进此答案的方法,请随意编辑它,我会接受任何此类编辑。

更新 - 改进使用 Pdf file size too big created using jspdf

【讨论】:

  • thanx @Andrew 但我需要为我的 PDF 提供样式
  • @MiguelÁngelMartín 没关系,没有什么能阻止你,除了不要提供使id=content 的 div 具有零区域的样式,而你目前正在这样做。您的 float css 似乎导致了这个 - 请参阅我更新的 stackblitz
  • @MiguelÁngelMartín 请注意,虚线/虚线边框是 html2canvas github.com/niklasvh/html2canvas/pull/1146987654324@的一个未解决问题
猜你喜欢
  • 2019-11-27
  • 2019-12-28
  • 2017-05-27
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多