我正在做的项目需要能够生成当前用户所在页面的PDF,我将使用jspdf
实现。由于我需要生成一个带有HTML
的PDF,因此我需要使用addHTML()
函数。有很多关于此主题的文章,都说:
你需要使用
html2canvas
或rasterizehtml
。
我选择使用html2canvas
。目前我的代码如下:
import { Injectable, ElementRef, ViewChild } from '@angular/core';
import * as jsPDF from 'jspdf';
import * as d3 from 'd3';
import * as html2canvas from 'html2canvas';
@Injectable ()
export class pdfGeneratorService {
@ViewChild('to-pdf') element: ElementRef;
GeneratePDF () {
html2canvas(this.element.nativeElement, <Html2Canvas.Html2CanvasOptions>{
onrendered: function(canvas: HTMLCanvasElement) {
var pdf = new jsPDF('p','pt','a4');
pdf.addHTML(canvas, function() {
pdf.save('web.pdf');
});
}
});
}
}
当调用此函数时,我会收到控制台错误:
为什么会出现这种情况呢?虽然我已经将canvas作为参数传递,但它仍然说我需要使用html2canvas。EXCEPTION: Error in ./AppComponent class AppComponent - inline template:3:4 caused by: You need either https://github.com/niklasvh/html2canvas or https://github.com/cburgmer/rasterizeHTML.js
import * as html2canvas from 'html2canvas/dist/html2canvas.js';
吗? - Syed Ali Taqi