0. Phantomjs 官网下载地址
// phantomjs 下载地址
https://phantomjs.org/download.html
1. html2png.js
"use strict";
var page = require('webpage').create(),
system = require('system'),
address, output, size;
// 定义参数
page.settings = {
XSSAuditingEnabled:false,
javascriptCanCloseWindows:true,
javascriptCanOpenWindows:true,
javascriptEnabled:true,
loadImages:true,
localToRemoteUrlAccessEnabled:false,
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
webSecurityEnabled:true
};
// 固定参数个数 不可以超出
if (system.args.length < 4 || system.args.length > 4) {
console.log('Usage: phantomjs.exe html2png.js http://www.baidu.com E:\\data\\phantomjs\\bin\\002.png 1366*768');
phantom.exit(1);
} else {
system.args.forEach(function (arg, i) {
console.log(i + ': ' + arg);
});
address = system.args[1];
// 将 address 中的含有单引号或者双引号进行删除 入参处理
address = address.replace(/'/g,"").replace(/\"/g,"");
console.log("address : " + address);
output = system.args[2];
page.viewportSize = { width: 600, height: 600 };
size = system.args[3].split('*');
var pageWidth = parseInt(size[0], 10);
var pageHeight = parseInt(size[1], 10);
// 设置截屏长宽
page.viewportSize = { width: pageWidth, height: pageHeight };
page.clipRect = { top: 0, left: 0, width: pageWidth, height: 800 };
// 监听是否成功
page.onLoadFinished =function(status){
console.log('status='+status);
};
// 打开等待 是否成功 等待8秒写入生成退出
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit(1);
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 8888);
}
});
}
2. 执行入参
phantomjs.exe html2png.js http://www.baidu.com ./baidu.com.png 1366*768
微笑的java
欢迎关注转发评论点赞沟通,让编码不在孤单。