Forked from tongpu/phantomjs-revealjs-slide-capture.js
Last active
November 17, 2016 15:59
-
-
Save eni23/867c9d12879442702a66118b7b59e109 to your computer and use it in GitHub Desktop.
PhantomJS script to capture/render screenshots of the slides of a Reveal.js powered slideshow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* PhantomJS script to capture/render screenshots of the slides of a Reveal.js powered slideshow. | |
*/ | |
var page = require('webpage').create(); | |
var args = require('system').args; | |
// Get url to render from command line. | |
var url; | |
if (args.length < 2) { | |
url = 'http://lab.hakim.se/reveal-js'; | |
console.warn('No url specified. Falling back on ' + url); | |
} | |
else { | |
url = args[1]; | |
} | |
// (Optional) prefix for renders. | |
var prefix = args.length >= 3 ? args[2] : 'slide-'; | |
// Set render size. | |
page.papersize = { format: 'A4', orientation: 'landscape' }; | |
// Open the url and do your thing. | |
page.open(url, function (status) { | |
if (status !== 'success') { | |
console.error('Failed to open url ' + url); | |
phantom.exit(); | |
} | |
// Disable slide transitions so we don't lose time on that. | |
page.evaluate(function() { | |
// Apparently setting "transition" and "backgroundTransition" to "none" | |
// is enough to disable slide transitions, even if there are per-slide transitions. | |
Reveal.configure({ | |
'transition': 'none', | |
'backgroundTransition': 'none' | |
}); | |
}); | |
// Disable transitions on "fragmented views". | |
page.evaluate(function() { | |
var fragments = document.getElementsByClassName('fragment'); | |
for (var f = fragments.length - 1; f >= 0; f--) { | |
fragments[f].classList.remove('fragment'); | |
} | |
}); | |
// Render all slides. | |
var slideCounter = 1; | |
var isLastSlide = function() { | |
return page.evaluate(function() { | |
return Reveal.isLastSlide(); | |
}); | |
}; | |
var next = function() { | |
return page.evaluate(function() { | |
return Reveal.next(); | |
}); | |
}; | |
var lpad =function(n, width, z) { | |
z = z || '0'; | |
n = n + ''; | |
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; | |
}; | |
while (!isLastSlide()) { | |
// Capture. | |
var filename = prefix + lpad(slideCounter,3) + '.pdf'; | |
console.log('Rendering ' + filename); | |
page.render(filename); | |
// Go to next slide. | |
next(); | |
slideCounter++; | |
} | |
phantom.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment