微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Html to PDF

做企业项目,经常会需要保存或输出 pdf 格式。

比如 invoice, purchase order 等等。

pdf 是 90 年 adobe 公司设计的, 08 年开放. 

制作 pdf 不难,但是互联网时代,大多数文本都用 html 格式来写. 

要弄一份一模一样的 pdf 出来就 double work 了。

于是就有了 html to pdf 的需求。

市场上已经有好多公司做这个出来卖钱了。

比如: 

https://www.nrecosite.com/pdf_generator_net.aspx#

https://www.paperplane.app/pricing

价格都不便宜呢.

html 是 93 年问世的, 很长一段时间, 游览器解析引擎都是闭源的. 所以要做 html to pdf 是蛮累的,要解析 html css。

直到 04 年苹果开源了 safari 的引擎 webkit。 

就有人基于 webkit 做出了 webkit html to pdf

https://wkhtmltopdf.org/

只要安装 webkit 然后通过 commond 调用一下就可以把 html 变成 pdf 文件了,开源的哦。

.net https://github.com/rdvojmoc/DinkToPdf 就是基于 wkhtmltopdf 的封装

有了前车之鉴, 后来的 phantomjs 也有 html to pdf 的功能

jsreport 早年就是基于 phantomjs  实现的 to html  pdf 功能 

https://github.com/jsreport/jsreport 

https://github.com/jsreport/jsreport-dotnet (.net 版本)

一直到 2017 年, chromium 推出了 headless 版. 

chromium 是 google 基于 webkit 的 folk, 现在 chrome 用着的是 blink, chromuim 算是前身吧. 

这个 chromium headless 可厉害了, google 出品, phantomjs 作者随后就宣布不在维护了,让位给 chromium.

chromium 自然是可以实现 html to pdf 的了. jsreport 后来的版本也切换到 chromium 了. 

chromium 的底层接口不太友好,google 自己又推出了 Puppeteer 

这是 js 的库, 可以用 nodejs 运行, 它是 chromium 友好的 api. 几行代码就可以实现各做游览器行为. 当然就包括了 html to pdf。

.net core 和 nodejs 是好朋友, 要在 core 调用 nodejs 的 package 是很简单的. 

所以又有了 https://github.com/kblok/puppeteer-sharp

我就是使用 asp.net core node service + Puppeteer  来实现 html to pdf 的.

 

上面说的都是 server side 的做法

client side 也有一个做法.

用的是一个叫 jsPDF 的插件, 它是做 pdf 的插件, 而不是 html to pdf 

实现手法是通过打开 browser 使用 url data:application/pdf 的方式输出 base64 

游览器支持这种 data 直接打开 file 的功能,不只 pdf, image 也是可以这样打开.

如果要 html to pdf 可以配上一个 html to canvas 的插件 

把 html  转成 canvas 然后变成图片在输入进 pdf  ( 当然这样文字就变成不可以 search 了 )

目前这个方案问题还是比较多的, 比如 htmltocanvas 依然处于不稳定版本,而且维护也很弱. 

 

 

参考资源 : 

https://wkhtmltopdf.org/

https://www.nrecosite.com/pdf_generator_net.aspx#

https://github.com/wkhtmltopdf/wkhtmltopdf

https://github.com/rdvojmoc/DinkToPdf

https://github.com/jsreport/jsreport

https://github.com/jsreport/jsreport-dotnet

https://github.com/kblok/puppeteer-sharp

https://www.paperplane.app/blog/modern-html-to-pdf-conversion-2019?utm_source=medium&utm_campaign=redirect

https://www.paperplane.app/pricing

https://juejin.im/entry/5ac1e7c05188257ddb0fc853

https://blog.risingstack.com/pdf-from-html-node-js-puppeteer/

https://zhaoqize.github.io/puppeteer-api-zh_CN/#/

https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions

http://www.rotisedapsales.com/snr/cloud2/website/jsPDF-master/docs/jsPDF.html

https://github.com/niklasvh/html2canvas

https://dev.to/damcosset/generate-a-pdf-from-html-and-back-on-the-front-5ff5

https://developers.google.com/web/updates/2017/04/headless-chrome

https://zhuanlan.zhihu.com/p/33015883

https://www.jianshu.com/p/db1b230e3415

https://stackoverflow.com/questions/564650/convert-html-to-pdf-in-net

https://www.quora.com/What-is-the-best-HTML-to-PDF-converter-tool

https://www.reddit.com/r/dotnet/comments/7i6ljt/what_is_currently_the_best_way_to_convert_html_to/

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐