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