【发布时间】:2019-04-22 15:31:14
【问题描述】:
我想以角度 6 从 html 导出 pdf。所以,我正在使用 jspdf 库。但我不能给出像颜色和背景颜色这样的样式。我怎样才能做到这一点? (如果jspdf有其他免费库,我可以使用)你可以从下面的链接中看到演示。
.ts 文件
export class AppComponent {
@ViewChild('reportContent') reportContent: ElementRef;
downloadPdf() {
const doc = new jsPDF();
const specialElementHandlers = {
'#editor': function (element, renderer) {
return true;
}
};
const content = this.reportContent.nativeElement;
doc.fromHTML(content.innerHTML, 15, 15, {
'width': 190,
'elementHandlers': specialElementHandlers
});
doc.save('asdfghj' + '.pdf');
}
}
.html 文件
<div #reportContent class="p-col-8 p-offset-2">
<table>
<tr>
<td style="color: red;background-color: blue;border:solid;">1111
</td>
<td style="border:solid;">2222
</td>
</tr>
</table>
</div>
<button pButton type="button" label="Pdf" (click)="downloadPdf()">Export Pdf</button>
【问题讨论】:
-
您可以使用
doc.setFillColor(0, 0, 0)设置背景颜色和doc.setTextColor(255)设置颜色 -
我投了 +1 票,因为事实上这是一个很好的问题......但我自己没有任何答案。 Openslide 似乎可以做到,但我不知道它是如何工作的 github.com/OpenSlides/OpenSlides
-
感谢@BenjaminCaure。我不明白为什么有人反对这个问题。
-
但是,我想要来自 html。不是打字稿代码。因为在实际场景中会有很多细胞。
doc.addHtml与造型配合得很好。但是在doc.addHtml,pdf 质量很差。 @Bharathkumarkamal
标签: angular typescript angular6 jspdf