对于我正在处理的项目,我需要能够生成用户当前正在使用的页面的PDF,为此我将使用jspdf。因为我有一个HTML我需要生成一个PDF,我需要的addHTML()功能。这里有很多话题,说Angular2 - 使用jspdf从HTML生成pdf
You'll either need to use html2canvas or 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, {
onrendered: function(canvas: HTMLCanvasElement) {
var pdf = new jsPDF('p','pt','a4');
pdf.addHTML(canvas, function() {
pdf.save('web.pdf');
});
}
});
}
}
当这个函数被调用时,我得到一个控制台错误:
EXCEPTION: Error in ./AppComponent class AppComponent - inline template:3:4 caused by: You need either /niklasvh/html2canvas or /cburgmer/rasterizeHTML.js
到底为什么会这样?我给了一个canvas作为参数,它仍然说我需要使用html2canvas。
-04-07
FlorisdG
+0
你试过从html2canvas/dist/html2canvas.js中导入*作为html2canvas吗? –
+0
@SyedAliTaqi我想你的建议,但它抛出错误 –