Skip to content

Instantly share code, notes, and snippets.

@viko16
Created July 18, 2016 17:33
Show Gist options
  • Save viko16/a35340991a029269e9a3c15dd5a2704b to your computer and use it in GitHub Desktop.
Save viko16/a35340991a029269e9a3c15dd5a2704b to your computer and use it in GitHub Desktop.
用 PhantomJS 网页截图
var page = require('webpage').create();
var url = 'http://ukn.me/#!/post/2016-07-13-Array%2520%25E9%259B%2586%25E5%2590%2588%25E6%2596%25B9%25E6%25B3%2595.md';
// 视窗大小
page.viewportSize = {
width: 1440,
height: 900
}
page.open(url, function(status) {
// 网页没设置背景色的时候,jpg 表示不出透明,图片会变黑
// 此处设置一个白色的背景色
page.evaluate(function() {
if (!document.body.style.backgroundColor) {
document.body.style.backgroundColor = 'white';
}
});
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
// 延时是怕单页没加载完
window.setTimeout(function() {
page.render('output.jpg', {
format: 'jpeg',
quality: '100'
});
phantom.exit();
}, 3000);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment