print-pdf.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * phantomjs script for printing presentations to PDF.
  3. *
  4. * Example:
  5. * phantomjs print-pdf.js "http://lab.hakim.se/reveal-js?print-pdf" reveal-demo.pdf
  6. *
  7. * By Manuel Bieh (https://github.com/manuelbieh)
  8. */
  9. // html2pdf.js
  10. var page = new WebPage();
  11. var system = require( 'system' );
  12. var slideWidth = system.args[3] ? system.args[3].split( 'x' )[0] : 960;
  13. var slideHeight = system.args[3] ? system.args[3].split( 'x' )[1] : 700;
  14. page.viewportSize = {
  15. width: slideWidth,
  16. height: slideHeight
  17. };
  18. // TODO
  19. // Something is wrong with these config values. An input
  20. // paper width of 1920px actually results in a 756px wide
  21. // PDF.
  22. page.paperSize = {
  23. width: Math.round( slideWidth * 2 ),
  24. height: Math.round( slideHeight * 2 ),
  25. border: 0
  26. };
  27. var inputFile = system.args[1] || 'index.html?print-pdf';
  28. var outputFile = system.args[2] || 'slides.pdf';
  29. if( outputFile.match( /\.pdf$/gi ) === null ) {
  30. outputFile += '.pdf';
  31. }
  32. console.log( 'Printing PDF (Paper size: '+ page.paperSize.width + 'x' + page.paperSize.height +')' );
  33. page.open( inputFile, function( status ) {
  34. window.setTimeout( function() {
  35. console.log( 'Printed successfully' );
  36. page.render( outputFile );
  37. phantom.exit();
  38. }, 1000 );
  39. } );