使用 Node.js 将 HTML 转为 PDF [英] HTML to PDF with Node.js

查看:43
本文介绍了使用 Node.js 将 HTML 转为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望为我的网站网页创建可打印的 pdf 版本.像 express.render() 之类的东西只会将页面呈现为 pdf

有谁知道这样做的节点模块吗?

如果没有,您将如何实施?我已经看到一些方法谈论使用像 phantom.js 这样的无头浏览器,但不确定流程是什么.

解决方案

扩展 Mustafa 的回答.

A) 安装

C) 这是一个渲染 pdf 的例子

var phantom = require('phantom');phantom.create().then(function(ph) {ph.createPage().then(function(page) {page.open("http://www.google.com").then(function(status) {page.render('google.pdf').then(function() {console.log('页面渲染');ph.exit();});});});});

PDF 输出:

无声打印那个 PDF

java -jar pdfbox-app-2.0.2.jar PrintPDF -silentPrint C:print_mypdf.pdf

I'm looking to create a printable pdf version of my website webpages. Something like express.render() only render the page as pdf

Does anyone know a node module that does that ?

If not, how would you go about implementing one ? I've seen some methods talk about using headless browser like phantom.js, but not sure whats the flow.

解决方案

Extending upon Mustafa's answer.

A) Install http://phantomjs.org/ and then

B) install the phantom node module https://github.com/amir20/phantomjs-node

C) Here is an example of rendering a pdf

var phantom = require('phantom');   

phantom.create().then(function(ph) {
    ph.createPage().then(function(page) {
        page.open("http://www.google.com").then(function(status) {
            page.render('google.pdf').then(function() {
                console.log('Page Rendered');
                ph.exit();
            });
        });
    });
});

Output of the PDF:

EDIT: Silent printing that PDF

java -jar pdfbox-app-2.0.2.jar PrintPDF -silentPrint C:print_mypdf.pdf

这篇关于使用 Node.js 将 HTML 转为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆