Advertisement
Guest User

Custom PhantomJS rasterize script

a guest
Jun 10th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. /**/
  2.  
  3. //"use strict";
  4. var page = require('webpage').create(),
  5. system = require('system'),
  6. address, output, size;
  7.  
  8. page.settings.userName = "info8000";
  9. page.settings.password = "Server2015";
  10.  
  11. if (system.args.length < 3 || system.args.length > 5) {
  12. console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
  13. console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
  14. console.log(' image (png/jpg output) examples: "1920px" entire page, window width 1920px');
  15. console.log(' "800px*600px" window, clipped to 800x600');
  16. phantom.exit(1);
  17. } else {
  18. address = system.args[1];
  19. output = system.args[2];
  20. page.viewportSize = { width: 600, height: 600 };
  21. if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
  22. size = system.args[3].split('*');
  23. page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
  24. : { format: system.args[3], orientation: 'portrait', margin: '1cm' };
  25. } else if (system.args.length > 3 && system.args[3].substr(-2) === "px") {
  26. size = system.args[3].split('*');
  27. if (size.length === 2) {
  28. pageWidth = parseInt(size[0], 10);
  29. pageHeight = parseInt(size[1], 10);
  30. page.viewportSize = { width: pageWidth, height: pageHeight };
  31. page.clipRect = { top: 0, left: 0, width: pageWidth, height: pageHeight };
  32. } else {
  33. console.log("size:", system.args[3]);
  34. pageWidth = parseInt(system.args[3], 10);
  35. pageHeight = parseInt(pageWidth * 3/4, 10); // it's as good an assumption as any
  36. console.log ("pageHeight:",pageHeight);
  37. page.viewportSize = { width: pageWidth, height: pageHeight };
  38. }
  39. }
  40. if (system.args.length > 4) {
  41. page.zoomFactor = system.args[4];
  42. }
  43. page.open(address, function (status) {
  44. if (status !== 'success') {
  45. console.log('Unable to load the address!');
  46. phantom.exit(1);
  47. } else {
  48. window.setTimeout(function () {
  49. page.render(output);
  50. phantom.exit();
  51. }, 200);
  52. }
  53. });
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement