生成pdf文件存储
page.render() 能够把当前页面渲染成图片并输出到指定文件中。输出的文件格式由传入的文件扩展名决定,目前支持 PNG 、 JPEG 、 GIF 、 PDF 。
InternetHospitalRealModel.getPagePdf = async function(id, gsdm, cb){
const phantom = require('phantom');
const fs = require('fs');
const path = require('path');
let instance = await phantom.create();
let page = await instance.createPage();
let url = ''
if(gsdm == 'SJMY' || gsdm == 'YKYD') {
url = `http://10.1.6.73/zhyw/tjbg/tjbgmx2.aspx?bh=${id}&gdm=${gsdm}&temp=${Math.random()}`
}
let status = await page.open(url);
if(status == 'success') {
await page.property('viewportSize', {width: 1000, height: 600});
let data = await page.render('a.pdf');
await instance.exit();
}else{
cb(null)
}
await page.close();
}
生成图片base64
InternetHospitalRealModel.getPagePdf = async function(id, gsdm, cb){
const phantom = require('phantom');
const fs = require('fs');
const path = require('path');
let instance = await phantom.create();
let page = await instance.createPage();
let url = ''
if(gsdm == 'SJMY' || gsdm == 'YKYD') {
url = `http://10.1.6.73/zhyw/tjbg/tjbgmx2.aspx?bh=${id}&gdm=${gsdm}&temp=${Math.random()}`
}
let status = await page.open(url);
if(status == 'success') {
await page.property('viewportSize', {width: 1000, height: 600});
let data = await page.renderBase64();
data = 'data:image/png;base64,'+ data;
cb(data);
}else{
cb(null);
}
await instance.exit();
}
学习参考文档:
https://segmentfault.com/q/1010000008331900
http://amirraminfar.com/phantomjs-node/#/install?id=node-v5x
https://www.npmjs.com/package/phantom
https://www.jianshu.com/p/15cba843bb55
https://javascript.ruanyifeng.com/tool/phantomjs.html#toc13