JJ Allaire | efa6ad4 | 2016-01-30 13:12:05 -0500 | [diff] [blame] | 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 | |
| 10 | // html2pdf.js |
| 11 | var page = new WebPage(); |
| 12 | var system = require( 'system' ); |
| 13 | |
| 14 | var slideWidth = system.args[3] ? system.args[3].split( 'x' )[0] : 960; |
| 15 | var slideHeight = system.args[3] ? system.args[3].split( 'x' )[1] : 700; |
| 16 | |
| 17 | page.viewportSize = { |
| 18 | width: slideWidth, |
| 19 | height: slideHeight |
| 20 | }; |
| 21 | |
| 22 | // TODO |
| 23 | // Something is wrong with these config values. An input |
| 24 | // paper width of 1920px actually results in a 756px wide |
| 25 | // PDF. |
| 26 | page.paperSize = { |
| 27 | width: Math.round( slideWidth * 2 ), |
| 28 | height: Math.round( slideHeight * 2 ), |
| 29 | border: 0 |
| 30 | }; |
| 31 | |
| 32 | var inputFile = system.args[1] || 'index.html?print-pdf'; |
| 33 | var outputFile = system.args[2] || 'slides.pdf'; |
| 34 | |
| 35 | if( outputFile.match( /\.pdf$/gi ) === null ) { |
| 36 | outputFile += '.pdf'; |
| 37 | } |
| 38 | |
| 39 | console.log( 'Printing PDF (Paper size: '+ page.paperSize.width + 'x' + page.paperSize.height +')' ); |
| 40 | |
| 41 | page.open( inputFile, function( status ) { |
| 42 | window.setTimeout( function() { |
Bruce's Thinkpad | 8b73dcf | 2016-07-14 00:12:43 +0800 | [diff] [blame] | 43 | console.log( 'Printed successfully' ); |
JJ Allaire | efa6ad4 | 2016-01-30 13:12:05 -0500 | [diff] [blame] | 44 | page.render( outputFile ); |
| 45 | phantom.exit(); |
| 46 | }, 1000 ); |
| 47 | } ); |
| 48 | |