print-pdf.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. * @author Manuel Riezebosch (https://github.com/riezebosch)
  10. */
  11. // html2pdf.js
  12. var system = require( 'system' );
  13. var probePage = new WebPage();
  14. var printPage = new WebPage();
  15. var inputFile = system.args[1] || 'index.html?print-pdf';
  16. var outputFile = system.args[2] || 'slides.pdf';
  17. if( outputFile.match( /\.pdf$/gi ) === null ) {
  18. outputFile += '.pdf';
  19. }
  20. console.log( 'Export PDF: Reading reveal.js config [1/4]' );
  21. probePage.open( inputFile, function( status ) {
  22. console.log( 'Export PDF: Preparing print layout [2/4]' );
  23. var config = probePage.evaluate( function() {
  24. return Reveal.getConfig();
  25. } );
  26. if( config ) {
  27. printPage.paperSize = {
  28. width: Math.floor( config.width * ( 1 + config.margin ) ),
  29. height: Math.floor( config.height * ( 1 + config.margin ) ),
  30. border: 0
  31. };
  32. printPage.open( inputFile, function( status ) {
  33. console.log( 'Export PDF: Preparing pdf [3/4]')
  34. printPage.evaluate(function() {
  35. Reveal.isReady() ? window.callPhantom() : Reveal.addEventListener( 'pdf-ready', window.callPhantom );
  36. });
  37. } );
  38. printPage.onCallback = function(data) {
  39. // For some reason we need to "jump the queue" for syntax highlighting to work.
  40. // See: http://stackoverflow.com/a/3580132/129269
  41. setTimeout(function() {
  42. console.log( 'Export PDF: Writing file [4/4]' );
  43. printPage.render( outputFile );
  44. console.log( 'Export PDF: Finished successfully!' );
  45. phantom.exit();
  46. }, 0);
  47. };
  48. }
  49. else {
  50. console.log( 'Export PDF: Unable to read reveal.js config. Make sure the input address points to a reveal.js page.' );
  51. phantom.exit(1);
  52. }
  53. } );