/examples/rasterize.js

https://github.com/luiseduardohd/phantomjs · JavaScript · 32 lines · 31 code · 1 blank · 0 comment · 8 complexity · 4ae27f6a4e4294b832b4005b067b5934 MD5 · raw file

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