public
Created

  • Download Gist
phantomjs-renderpdf.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
var page = require('webpage').create(),
system = require('system'),
address, output, size;
 
if (system.args.length < 3 || system.args.length > 6) {
console.log('Usage: rasterize.js URL filename waittime [paperwidth*paperheight|paperformat] [zoom]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
// should we wait before render ?
if (system.args.length > 3) {
waittime = system.args[3];
} else {
waittime = 200;
}
page.viewportSize = { width: 1280, height: 1024 };
if (system.args.length > 4 && system.args[2].substr(-4) === ".pdf") {
size = system.args[4].split('*');
page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
: { format: system.args[4], orientation: 'portrait', margin: '1cm' };
}
if (system.args.length > 4) {
page.zoomFactor = system.args[5];
}
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, waittime);
}
});
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.