print-pdf.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. * @author Manuel Bieh (https://github.com/manuelbieh)
  8. * @author Hakim El Hattab (https://github.com/hakimel)
  9. */
  10. // html2pdf.js
  11. var system = require( 'system' );
  12. var probePage = new WebPage();
  13. var printPage = new WebPage();
  14. var inputFile = system.args[1] || 'index.html?print-pdf';
  15. var outputFile = system.args[2] || 'slides.pdf';
  16. if( outputFile.match( /\.pdf$/gi ) === null ) {
  17. outputFile += '.pdf';
  18. }
  19. console.log( 'Export PDF: Reading reveal.js config [1/3]' );
  20. probePage.open( inputFile, function( status ) {
  21. console.log( 'Export PDF: Preparing print layout [2/3]' );
  22. var config = probePage.evaluate( function() {
  23. return Reveal.getConfig();
  24. } );
  25. if( config ) {
  26. printPage.paperSize = {
  27. width: Math.floor( config.width * ( 1 + config.margin ) ),
  28. height: Math.floor( config.height * ( 1 + config.margin ) ),
  29. border: 0
  30. };
  31. printPage.open( inputFile, function( status ) {
  32. window.setTimeout( function() {
  33. console.log( 'Export PDF: Writing file [3/3]' );
  34. printPage.render( outputFile );
  35. console.log( 'Export PDF: Finished successfully!' );
  36. phantom.exit();
  37. }, 1000 );
  38. } );
  39. }
  40. else {
  41. console.log( 'Export PDF: Unable to read reveal.js config. Make sure the input address points to a reveal.js page.' );
  42. phantom.exit(1);
  43. }
  44. } );