【问题标题】:How to convert HTML to pdf in angular2?如何在angular2中将HTML转换为pdf?
【发布时间】:2017-08-11 12:53:55
【问题描述】:

我需要将动态生成的 html 表格转换为 pdf 格式并且也可以打印出来。我需要它在 angular2 和 Typescript 中完成。

【问题讨论】:

  • 看看jsPDF。
  • 我已经检查了 JsPDF 它适用于 angular1 而不是 angular2。你能在 plunker 中给我看一些我们可以将 HTML 转换为 pdf 的东西吗?我需要将带有ID的HTML表格转换为pdf并打印出来。
  • 它在 Angular 2 中确实有效。当然,您需要为它获取 typings

标签: angular pdf typescript html-to-pdf


【解决方案1】:

JSPDF 适用于 Angular 2。您需要从 dt~ 下载定义。 将库导入为:

import * as jsPDF from "jspdf";
.
.
.

let doc = new jsPDF();

// Add a title to your PDF
doc.setFontSize(30); 
doc.text(12, 10, "Your Title");

// Create your table here (The dynamic table needs to be converted to canvas).
let element = <HTMLScriptElement>document.getElementsByClassName("pvtTable")[0];
html2canvas(element)
.then((canvas: any) => {
    doc.addImage(canvas.toDataURL("image/jpeg"), "JPEG", 0, 50, doc.internal.pageSize.width, element.offsetHeight / 5 );
    doc.save(`Report-${Date.now()}.pdf`);
})

在您的 system.js 中,在地图部分添加以下行:

"jspdf": "<myLibs>/jspdf.js",

【讨论】:

  • 你是我的英雄。
猜你喜欢
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多