print-pdf.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. printPage.paperSize = {
  26. width: config.width * ( 1 + config.margin ),
  27. height: config.height * ( 1 + config.margin ),
  28. border: 0
  29. };
  30. printPage.open( inputFile, function( status ) {
  31. window.setTimeout( function() {
  32. console.log( 'Export PDF: Writing file [3/3]' );
  33. printPage.render( outputFile );
  34. console.log( 'Export PDF: Finished successfully!' );
  35. phantom.exit();
  36. }, 1000 );
  37. } );
  38. } );