nodejs html 生成图片,Nodejs中使用phantom将html转为pdf或图片格式的方法

最近在项目中遇到需要把html页面转换为pdf的需求,并且转换成的pdf文件要保留原有html的样式和图片。也就是说,html页面的图片、表格、样式等都需要完整的保存下来。

最初找到三种方法来实现这个需求,这三种方法都只是粗浅的看了使用方法,从而找出适合这个需求的方案:

html-pdf 模块

wkhtmltopdf 工具

phantom 模块

最终使用了phantom模块,也达到了预期效果。现在简单的记录三种方式的使用方法,以及三者之间主要的不同之处。

1.html-pdf

github:https://github.com/marcbachmann/node-html-pdf

npm:https://www.npmjs.com/package/html-pdf

安装:

npm install -g html-pdf

使用命令行:

html-pdf /test/index.html index.pdf

这样便可以把index.html页面转换为对应的index.pdf文件。

使用代码:

var express = require('express');

var router = express.router();

var pdf = require('html-pdf');

router.get('/url',function(req,res){

res.render('html',function(err,html){

html2pdf(html,'html.pdf');

//........

});

});

/**

* 这种方法没有渲染样式和图片

* @param url

* @param pdfname

*/

exports.html2pdf = function(html,pdfname){

var options = {format:true};

pdf.create(html,options).tofile(__dirname+'/'+pdfname,function(err,res){

if (err) return console.log(err);

console.log(res);

});

};

在测试过程中发现,生成的pdf文件中并没有支持样式渲染和图片加载,不能支持通过url直接加载html;但是在分页的支持上很好。

结果如下:

0d659f84479ce93bcf9bdf18ced8ae23.png

2、wkhtmltopdf

github:https://github.com/wkhtmltopdf/wkhtmltopdf

官方文档:https://wkhtmltopdf.org

npm:https://www.npmjs.com/package/wkhtmltopdf

wkhtmltopdf在效果上比较html-pdf要好很多,它支持样式渲染,图片加载,还可以通过url直接生成pdf文件。

但是安装上要麻烦得多。具体安装步骤参考

安装完毕之后,使用命令行:

wkhtmltopdf https://github.com github.pdf

即可生成对应的pdf文件。

代码使用:

var wkhtmltopdf = require('wkhtmltopdf');

var fs = require('fs');

// url 使用url生成对应的pdf

wkhtmltopdf('http://github.com', { pagesize: 'letter' })

.pipe(fs.createwritestream('out.pdf'));

除了可以通过url生成之外,还能通过html文件内容生成,就像html-pdf一样,只要有html格式的字符串就可以生成相应的pdf。

结果如下:

a6f55bb4e068a4c8abbb1383ac785bd1.png

3、phantom 模块

github:https://github.com/amir20/phantomjs-node

官方文档:http://amirraminfar.com/phantomjs-node/

npm:https://www.npmjs.com/package/phantom

phantomjs是基于webkit的无头浏览器,提供相关的javascript api,nodejs就相当于对phantomjs的模块化封装,使得它能够在nodejs中使用。

模块安装:

node版本6.x以上的:

npm install phantom –save

node版本5.x的:

npm install phantom@3 –save

node版本4.x及以下的:

npm install phantom@2 –save

以下的例子都是基于node 4.x

代码使用:

var phantom = require('phantom');

phantom.create().then(function(ph) {

ph.createpage().then(function(page) {

page.open("https://www.oracle.com/index.html").then(function(status) {

page.property('viewportsize',{width: 10000, height: 500});

page.render('/oracle10000.pdf').then(function(){

console.log('page rendered');

ph.exit();

});

});

});

});

代码中,phantom能够通过url转换为相应的pdf,而且能够通过 page.property('viewportsize',{width:width,height:height}) 来设置生成的pdf的宽度和高度。

此例phantom中并没有分页,它是以整个浏览器截图的形式,获取全文,转化为pdf格式。

选择phantom的主要原因就是便于设置pdf的宽度,更能兼容html的排版。

结果如下:

fe0e81f6a8c96734b11ebf1183cc38a9.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用`pdfkit`和`canvas`模块来实现图片合成 pdf。 首先需要安装这两个模块: ``` npm install pdfkit canvas ``` 然后编写代码: ```javascript const PDFDocument = require('pdfkit'); const { createCanvas, loadImage } = require('canvas'); const fs = require('fs'); // 创建一个 PDF 文档 const doc = new PDFDocument(); // 定义图片的宽度和高度 const width = 300; const height = 200; // 加载图片 loadImage('image1.jpg').then(image1 => { loadImage('image2.jpg').then(image2 => { // 获取图片数量 const imageCount = 2; // 设置文档页面数量 doc.info['Title'] = 'My Document'; doc.info['Author'] = 'John Doe'; doc.info['Subject'] = 'Testing PDF Kit'; doc.info['Keywords'] = 'PDFKit, JavaScript, PDF, generator'; doc.info['Producer'] = 'PDFKit'; doc.info['Creator'] = 'John Doe'; doc.registerFont('NotoSans', 'fonts/NotoSansCJKsc-Regular.otf'); doc.info['Title'] = 'My Document'; doc.pipe(fs.createWriteStream('output.pdf')); // 循环添加每张图片到文档 for (let i = 0; i < imageCount; i++) { const canvas = createCanvas(width, height); const ctx = canvas.getContext('2d'); ctx.drawImage(i === 0 ? image1 : image2, 0, 0, width, height); // 将 canvas 添加到 PDF 文档 doc.image(canvas.toBuffer(), 50, 50, { width: width, height: height }); doc.addPage(); } // 结束文档 doc.end(); }); }); ``` 在以上代码,我们使用`loadImage`方法从文件加载图片,并将它们添加到 PDF 文档。我们还需要使用`createCanvas`方法创建一个新的画布,并在画布上绘制图片。最后,我们使用`doc.image`方法将画布添加到 PDF 文档。 运行代码后,会在当前目录下生成一个名为`output.pdf`的 PDF 文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值