Skip to content

Commit a349ff4

Browse files
committed
new phantom pdf export script that works with 3.4.0 hakimel#1815
1 parent bc1ca1a commit a349ff4

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

plugin/print-pdf/print-pdf.js

+29-26
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,15 @@
44
* Example:
55
* phantomjs print-pdf.js "http://lab.hakim.se/reveal-js?print-pdf" reveal-demo.pdf
66
*
7-
* By Manuel Bieh (https://github.com/manuelbieh)
7+
* @author Manuel Bieh (https://github.com/manuelbieh)
8+
* @author Hakim El Hattab (https://github.com/hakimel)
89
*/
910

1011
// html2pdf.js
11-
var page = new WebPage();
1212
var system = require( 'system' );
1313

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-
};
14+
var probePage = new WebPage();
15+
var printPage = new WebPage();
3116

3217
var inputFile = system.args[1] || 'index.html?print-pdf';
3318
var outputFile = system.args[2] || 'slides.pdf';
@@ -36,13 +21,31 @@ if( outputFile.match( /\.pdf$/gi ) === null ) {
3621
outputFile += '.pdf';
3722
}
3823

39-
console.log( 'Printing PDF (Paper size: '+ page.paperSize.width + 'x' + page.paperSize.height +')' );
24+
console.log( 'Export PDF: Reading reveal.js config [1/3]' );
25+
26+
probePage.open( inputFile, function( status ) {
27+
28+
console.log( 'Export PDF: Preparing print layout [2/3]' );
29+
30+
var config = probePage.evaluate( function() {
31+
return Reveal.getConfig();
32+
} );
33+
34+
printPage.paperSize = {
35+
width: config.width * ( 1 + config.margin ),
36+
height: config.height * ( 1 + config.margin ),
37+
border: 0
38+
};
39+
40+
printPage.open( inputFile, function( status ) {
41+
window.setTimeout( function() {
42+
console.log( 'Export PDF: Writing file [3/3]' );
43+
printPage.render( outputFile );
44+
console.log( 'Export PDF: Finished successfully!' );
45+
phantom.exit();
46+
}, 1000 );
47+
} );
4048

41-
page.open( inputFile, function( status ) {
42-
window.setTimeout( function() {
43-
console.log( 'Printed successfully' );
44-
page.render( outputFile );
45-
phantom.exit();
46-
}, 1000 );
4749
} );
4850

51+

0 commit comments

Comments
 (0)