Path: blob/master/6-Selenium/phantomjs/examples/rasterize.js
164 views
var page = require('webpage').create(),1system = require('system'),2address, output, size;34if (system.args.length < 3 || system.args.length > 5) {5console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');6console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');7console.log(' image (png/jpg output) examples: "1920px" entire page, window width 1920px');8console.log(' "800px*600px" window, clipped to 800x600');9phantom.exit(1);10} else {11address = system.args[1];12output = system.args[2];13page.viewportSize = { width: 600, height: 600 };14if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {15size = system.args[3].split('*');16page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }17: { format: system.args[3], orientation: 'portrait', margin: '1cm' };18} else if (system.args.length > 3 && system.args[3].substr(-2) === "px") {19size = system.args[3].split('*');20if (size.length === 2) {21pageWidth = parseInt(size[0], 10);22pageHeight = parseInt(size[1], 10);23page.viewportSize = { width: pageWidth, height: pageHeight };24page.clipRect = { top: 0, left: 0, width: pageWidth, height: pageHeight };25} else {26console.log("size:", system.args[3]);27pageWidth = parseInt(system.args[3], 10);28pageHeight = parseInt(pageWidth * 3/4, 10); // it's as good an assumption as any29console.log ("pageHeight:",pageHeight);30page.viewportSize = { width: pageWidth, height: pageHeight };31}32}33if (system.args.length > 4) {34page.zoomFactor = system.args[4];35}36page.open(address, function (status) {37if (status !== 'success') {38console.log('Unable to load the address!');39phantom.exit(1);40} else {41window.setTimeout(function () {42page.render(output);43phantom.exit();44}, 200);45}46});47}484950