Angular2 - 使用jspdf从HTML生成pdf

Flo*_*sdG 9 pdf html2canvas jspdf typescript angular

对于我正在进行的项目,我需要能够生成用户当前所在页面的PDF,我将使用该页面jspdf.由于我HTML需要生成PDF,我需要这个addHTML()功能.关于这一点有很多话题,说

你需要使用html2canvasrasterizehtml.

我选择使用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');
        });
      }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

调用此函数时,出现控制台错误:

EXCEPTION:./AppComponent类中的错误AppComponent - 内联模板:3:4导致:您需要https://github.com/niklasvh/html2canvashttps://github.com/cburgmer/rasterizeHTML.js

这究竟是为什么?我给出一个canvas参数,它仍然说我需要使用html2canvas.

小智 18

我发现的工作是添加:

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
Run Code Online (Sandbox Code Playgroud)

到index.html文件(它可能在其他地方).

然后我用了:

const elementToPrint = document.getElementById('foo'); //The html element to become a pdf
const pdf = new jsPDF('p', 'pt', 'a4');
pdf.addHTML(elementToPrint, () => {
    doc.save('web.pdf');
});
Run Code Online (Sandbox Code Playgroud)

在代码中不再使用html2canvas.
然后,您可以删除以下导入:

import * as html2canvas from 'html2canvas';
Run Code Online (Sandbox Code Playgroud)

  • 除了导入之外,您在代码中的何处使用 html2canvas ?我收到错误您需要 https://github.com/niklasvh/html2canvas 或 https://github.com/cburgmer/rasterizeHTML.js 错误:您需要 https://github.com/niklasvh/html2canvas 或https://github.com/cburgmer/rasterizeHTML.js 在 Object.e.API.addHTML (5认同)

Ahm*_*ssy 8

如果有人不想使用cdn脚本并且想要使用更多(有角度)的方式,这在Angular 6中对我有用:

使用这种方式将在编辑器中为您提供更好的支持和自动完成功能,并将帮助您避免依赖cdn脚本(如果您想避免它们,像我一样)

基于这里优秀答案,因为我很难找到答案,我正在重新分享其中所说的内容并帮助我在Angular 6中使用jsPDF(所有功劳都归功于此答案的原作者)

你应该运行这些cmds:

npm install jspdf --save

typings install dt~jspdf --global --save

npm install @types/jspdf --save
Run Code Online (Sandbox Code Playgroud)

在angular-cli.json中添加以下内容:

"scripts": [ "../node_modules/jspdf/dist/jspdf.min.js" ]
Run Code Online (Sandbox Code Playgroud)

HTML:

<button (click)="download()">download </button>
Run Code Online (Sandbox Code Playgroud)

组件ts:

import { Component, OnInit, Inject } from '@angular/core';
import * as jsPDF from 'jspdf'
@Component({
  ...
  providers: [
    { provide: 'Window',  useValue: window }
  ]
})
export class GenratePdfComponent implements OnInit {

  constructor(
    @Inject('Window') private window: Window,
    ) { }

  download() {

        var doc = new jsPDF();
        doc.text(20, 20, 'Hello world!');
        doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
        doc.addPage();
        doc.text(20, 20, 'Do you like that?');

        // Save the PDF
        doc.save('Test.pdf');
    }
}
Run Code Online (Sandbox Code Playgroud)


Sad*_*Ali 7

任何仍在尝试将 Html div 转换为 pdf 的人都可以选择使用html2pdf,只需几行即可轻松完成所有操作。

var element = document.getElementById('element-to-print');
html2pdf(element);
Run Code Online (Sandbox Code Playgroud)