如何使用浏览器(chrome/firefox)的 HTML/CSS/JS 渲染引擎生成 PDF? [英] How to use the browser's (chrome/firefox) HTML/CSS/JS rendering engine to produce PDF?
问题描述
有一些不错的项目可以从 html/css/js 文件生成 pdf
There are nice projects that generate pdf from html/css/js files
- http://wkhtmltopdf.org/(开源)
- https://code.google.com/p/flying-saucer/(开源)
- http://cssbox.sourceforge.net/(不一定是直接生成 pdf)
- http://phantomjs.org/(开源允许pdf输出)
- http://www.princexml.com/(商业版,但最好的一个)
- https://thepdfapi.com/ chrome 修改以从 的 html 中吐出 pdf
- http://wkhtmltopdf.org/ (open source)
- https://code.google.com/p/flying-saucer/ (open source)
- http://cssbox.sourceforge.net/ (not necessarily straight pdf generation)
- http://phantomjs.org/ (open source allows for pdf output)
- http://www.princexml.com/ (comercial but hands down the best one out there)
- https://thepdfapi.com/ a chrome modification to spit pdf from html from
我想以编程方式控制 chrome 或 firefox 浏览器(因为它们都是跨平台的),使它们加载网页、运行脚本和设置页面样式并生成用于打印的 pdf 文件.
I want to programatically control chrome or firefox browser (because they both are cross platform) to make them load a web page, run the scripts and style the page and generate a pdf file for printing.
但是我如何开始以自动方式控制浏览器,以便我可以执行类似的操作
But how do I start by controlling the browser in an automated way so that I can do something like
render-to-pdf file-to-render.html out.pdf
render-to-pdf file-to-render.html out.pdf
我可以通过浏览页面然后将其打印为 pdf 来轻松地手动完成这项工作,我会在 pdf 文件上获得准确的、100% 符合规范的渲染 html/css/js 页面.通过浏览器中的配置选项,甚至可以在 pdf 中省略 url 标题.但同样,我如何开始尝试自动化此过程?
I can easily make this job manually by browsing the page and then printing it to pdf and I get an accurate, 100% spec compliant rendered html/css/js page on a pdf file. Even the url headers can be omitted in the pdf through configuration options in the browser. But again, how do I start in trying to automate this process?
我想在服务器端自动化,打开浏览器,导航到一个页面,并使用浏览器呈现的页面生成 pdf.
I want to automate in the server side, the opening of the browser, navigating to a page, and generating the pdf using the browser rendered page.
我做了很多研究,只是不知道如何提出正确的问题.我想以编程方式控制浏览器,也许就像 selenium 那样,但我将网页导出为 PDF(因此使用浏览器的渲染功能来生成好的 pdf)
I have done a lot of research I just don't know how to make the right question. I want to programatically control the browser, maybe like selenium does but to the point where I export a webpage as PDF (hence using the rendering capabilities of the browser to produce good pdfs)
推荐答案
Firefox 有一个 API 方法:https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/saveAsPDF
Firefox has an API method for that: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/saveAsPDF
browser.tabs.saveAsPDF({})
.then((status) => {
console.log('PDF file status: ' + status);
});
但是,它似乎仅适用于 浏览器扩展,不能从网页调用.
However, it seems to be available only for Browser Extensions, not to be invoked from a web page.
我仍在为此寻找公共 API...
I'm still looking for a public API for that...
这篇关于如何使用浏览器(chrome/firefox)的 HTML/CSS/JS 渲染引擎生成 PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!