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