angular将html导出为pdf,Angular2 - 使用jspdf从HTML生成pdf

对于我正在处理的项目,我需要能够生成用户当前正在使用的页面的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 https://github.com/niklasvh/html2canvas or https://github.com/cburgmer/rasterizeHTML.js

到底为什么会这样?我给了一个canvas作为参数,它仍然说我需要使用html2canvas。

2017-04-07

FlorisdG

+0

你试过从html2canvas/dist/html2canvas.js中导入*作为html2canvas吗? –

+0

@SyedAliTaqi我想你的建议,但它抛出错误 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值