当需要将页面原样输出为图片或PDF时,可以使用PhantomJS进行抓取
(官宣PhantomJS开发暂停,最新稳定版本phantomjs-2.1.1)
下载文件
官网 Download phantomjs-2.1.1-linux-x86_64.tar.bz2(22.3 MB) and extract the content.
上传至Linux服务器
修改screenshot.js
"use strict";
var page = require('webpage').create();
var system = require('system');
var address = system.args[1];
var output = system.args[2];
page.viewportSize = {
width: 2400,
height: 20000
};
page.zoomFactor = 1;
page.settings.loadImages = true;
page.settings.resourceTimeout = 30000;
page.settings.userAgent = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36';
page.open(address, function(status) {
function checkReadyState() {
setTimeout(function () {
var readyState = page.evaluate(function () {
return document.readyState;
});
if ("complete" === readyState) {
page.paperSize = {
format: 'A4',
orientation: 'portrait',
margin: '0.8cm'
};
page.render(output);
console.log("生成成功");
console.log("$"+output+"$");
phantom.exit();
} else {
checkReadyState();
}
},1000);
}
checkReadyState();
});
修改文件权限
执行Linux指令
/** chmod能改变权限,-R是目录下所有文件,777就是高权限(读、写、执行) */
chmod -R 777 /usr/local/phantomjs-2.1.1-forpdf
Java代码
public String phantomjsPdf(String orderNo,HttpServletRequest request){
/*phantomjs在服务器的路径*/
String phantomjs = "/usr/local/phantomjs-2.1.1-forpdf/bin/phantomjs";
/*截图js在服务器的路径*/
String screenshot = "/usr/local/phantomjs-2.1.1-forpdf/bin/screenshot.js";
/*要生成图片的链接*/
String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+"/product/getPdfPage.do?orderNo="+orderNo;
/*生成的图片路径*/
String time = new Date().getTime()+"";
String imgPath = request.getSession().getServletContext().getRealPath("/")+"/images/upload/image/"+"product_agreement/"+time+".pdf";
/*linux命令*/
String shell = phantomjs+" "+screenshot+" "+url+" "+imgPath;
MyUtil.shell(shell);
return "/images/upload/image/product_agreement/"+time+".pdf";
}
注:1. url中参数传多个时,shell中的url必须使用''包裹,否则会造成参数丢失
2. url参数中包含中文时,需将参数进行转码,否则会发生乱码
/*MyUtil.shell() 执行shell命令*/
public static void shell(String cmd) {
String[] cmds = { "/bin/sh", "-c", cmd };
Process process;
try {
process = Runtime.getRuntime().exec(cmds);
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "Error");
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "Output");
errorGobbler.start();
outputGobbler.start();
try {
process.waitFor();
}
catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}