Skip to content

Instantly share code, notes, and snippets.

@acgrid
Created May 24, 2018 15:47
Show Gist options
  • Save acgrid/f0e2bfb7c81b3e180ef843a9401eb425 to your computer and use it in GitHub Desktop.
Save acgrid/f0e2bfb7c81b3e180ef843a9401eb425 to your computer and use it in GitHub Desktop.
Get screenshot for every page generated by pdf2html using phantomjs
"use strict";
var page = require('webpage').create();
var system = require('system');
if (system.args.length !== 2) {
system.stderr.writeLine('Put target URL as argument');
} else {
var url = system.args[1];
}
page.viewportSize = { width: 1080, height: 1920 };
page.open(url, function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var rects = page.evaluate(function () {
var elements = document.querySelectorAll('div[data-page-no]');
if (!elements) return null;
return Array.prototype.slice.call(elements).map(function (element) {
var rect = element.getBoundingClientRect();
return {top: parseInt(rect.top), left: parseInt(rect.left), width: parseInt(rect.width), height: parseInt(rect.height)};
});
});
setTimeout(function(){
rects.forEach(function(rect, index){
setTimeout(function(){
console.log(index, JSON.stringify(rect));
page.evaluate(function(top){
document.getElementById("page-container").scrollTop = top;
}, rect.top);
}, index * 1000);
setTimeout(function(){
page.clipRect = {top: 0, left: rect.left, width: rect.width, height: rect.height};
page.render((index + 1) + '.png', {format: 'png', quality: '80'});
if(index === rects.length - 1) phantom.exit();
}, index * 1000 + 500);
});
}, 1000);
}
});
@acgrid
Copy link
Author

acgrid commented May 24, 2018

Known bug: scroll mistake when capture the last page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment