使用jspdf将angular2中的angular template.html转换为pdf
我是angular 2的新手,需要使用jspdf将angular 2中的html组件导出为pdf的功能。我需要使用jspdf将动态生成的HTML表格格式转换为pdf格式。示例代码和plunker链接如下所示:使用jspdf将angular2中的angular template.html转换为pdf,angular,typescript,jspdf,html-to-pdf,Angular,Typescript,Jspdf,Html To Pdf,我是angular 2的新手,需要使用jspdf将angular 2中的html组件导出为pdf的功能。我需要使用jspdf将动态生成的HTML表格格式转换为pdf格式。示例代码和plunker链接如下所示: 从“angular2/core”导入{Component,ElementRef,ViewChild}; 声明让jsPDF; @组成部分({ 选择器:“我的应用程序”, 模板:` 下载至PDF 名称 {{hero.name} `, 风格:[ ` .我的班级{ 背景颜色:黄色; } ` ]
从“angular2/core”导入{Component,ElementRef,ViewChild};
声明让jsPDF;
@组成部分({
选择器:“我的应用程序”,
模板:`
下载至PDF
名称
{{hero.name}
`,
风格:[
`
.我的班级{
背景颜色:黄色;
}
`
]
})
导出类应用程序{
@ViewChild(“测试”)el:ElementRef;
isClassVisible:正确;
英雄=[
{id:1,名字:'Superman'},
{id:2,姓名:'Batman'},
{id:5,姓名:'BatGirl'},
{id:3,名字:'Robin'},
{id:4,名称:'Flash'},
{id:1,名字:'Superman'},
{id:2,姓名:'Batman'},
{id:5,姓名:'BatGirl'},
{id:3,名字:'Robin'},
{id:4,名称:'Flash'}
];
构造函数(){
}
公开下载(){
var doc=new jsPDF();
doc.text(20,20,“你好,世界!”);
doc.save('Test1.pdf');
}
公共pdfHtml(){
警报(this.el.nativeElement.innerHTML);
设pdf=newjspdf();
让选项={
pagesplit:对
};
addHTML(this.el.nativeElement,0,0,options,()=>{
pdf.save(“test1.pdf”);
});
}
}
您可以使用自动表格:
import autoTable from 'jspdf-autotable'
在方法pdfHtml()中,将其改为:
public pdfHtml() {
alert(this.el.nativeElement.innerHTML);
let pdf = new jsPDF();
let options = {
pagesplit: true
};
//Add this
autoTable(pdf, {
tableWidth: 'auto',
// head: [['id', 'Name']],
head: [['id', 'Name']],
// body: [ [1, 'Superman', ],[2, 'Batman', ],[3, 'Batgirl', ] ],
body: heroes ,
},);
pdf.addHTML(this.el.nativeElement, 0, 0, options, () => {
pdf.save("test1.pdf");
});
}
祝你好运