且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular 7:将HTML转换为PDF

更新时间:2023-02-16 20:30:21

这不是一件微不足道的任务.在服务器上,我尝试使用wkhtmltopdf,但最终寻找了更好的解决方案,因为有了它,我无法使用CSS的所有功能进行打印.我认为jsPDF等客户端工具也存在同样的问题,您将无法获得预期的一致结果.

This is not so trivial task. On the server I tried using wkhtmltopdf but end up looking for a better solution, because with it I can't use all the power of CSS for printing. I think it is the same problem with client-side tools like jsPDF, you will not get an expected and consistent result.

我现在选择Google Cloud中的无头铬.

Headless chrome in Google Cloud is my choice right now.

这里很好地概述了可用的工具和技术现代HTML到PDF的转换

Here is a good overview of available tools and technologies Modern HTML to PDF conversion

更新:

如果您需要创建PDF而不能在服务器上安装任何内容,则可以使用Headless Chrome和Google Cloud Function来完成.

If you need to create a PDF and can't install anything on your server, you can do so by using Headless Chrome and Google Cloud Function.

using-puppeteer-in-google-cloud-functions

puppeteering-in-firebase-google-cloud-功能

更新2:

顺便说一句,您始终可以在浏览器中选择打印为PDF".但是,我不确定它在Android平板电脑上如何工作,iOS的Safari是否可以导出为PDF.

And by the way, you always have an option to Print to PDF in browser. However, I'm not sure how it works on Android tablets, Safari for iOS can export to PDF.