Skip to content

Commit

Permalink
Update PhantomJS script to 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Oct 9, 2011
1 parent 72248d6 commit 94204d1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
74 changes: 48 additions & 26 deletions bin/phantom-slippy-to-pdf.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,58 @@
var current, slides, viewport, output, delay;
var page, current, slides, viewport, output, delay;

// settings
delay = 500;
viewport = { width: 1024, height: 768 };

// init
if (phantom.state.length === 0) {
if (phantom.args.length !== 2) {
console.log('Usage: phantom-pdf.js URL dirname');
if (phantom.version.major == 1 && phantom.version.minor < 3) {
console.log('This script requires PhantomJS v1.3.0+, you have v'+phantom.version.major+'.'+phantom.version.minor+'.'+phantom.version.patch);
phantom.exit(-1);
}

if (phantom.args.length !== 2) {
console.log('Usage: phantom-pdf.js URL dirname');
phantom.exit(-1);
}

output = phantom.args[1];
current = 1;
page = new WebPage();
page.viewportSize = { width: viewport.width, height: viewport.height };
page.paperSize = { width: viewport.width * 1.5, height: viewport.height * 1.5 + 30 };

console.log('Loading ...');
page.open(phantom.args[0], function (status) {
if (status !== 'success') {
console.log('Unable to access network');
phantom.exit();
} else {
phantom.state = 'rasterize';
console.log('opening page');
phantom.open(phantom.args[0]);
slides = page.evaluate(function () {
return $('.slideContent').length;
});

setTimeout(initPage, 1000);
}
} else {
// run
current = 1;
slides = $('.slideContent').length;
phantom.viewportSize = { width: viewport.width, height: viewport.height };
phantom.paperSize = { width: viewport.width * 1.5, height: viewport.height * 1.5 + 30 };
output = phantom.args[1];

phantom.sleep(1000);
$('.incremental').css('opacity', '1').removeClass('incremental');

for (;current<=slides;current++) {
console.log('rendering slide '+current);
phantom.sleep(delay);
phantom.render(output+'slide'+"000".substring(current.toString().length)+current+'.pdf');
$(document).slippy().nextSlide();
});

function initPage() {
console.log('Rendering ...');
page.evaluate(function () {
$('.incremental').css('opacity', '1').removeClass('incremental');
});
setTimeout(renderNextSlide, delay);
}

function renderNextSlide() {
if (current <= slides) {
console.log('... slide '+current);
page.render(output+'slide'+"000".substring(current.toString().length)+current+'.pdf');
page.evaluate(function () {
$(document).slippy().nextSlide();
});
current++;
setTimeout(renderNextSlide, delay);
} else {
console.log('Done.');
phantom.exit(0);
}
console.log('done');
phantom.exit();
}
6 changes: 6 additions & 0 deletions bin/slippy-pdf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if [ "" = "$2" ]; then
echo "Usage: ./phantom-pdf.sh <slides URL> <PDF filename>"
exit 1
fi

mkdir -p tmp-pdf/
rm -f tmp-pdf/*.png
rm -f tmp-pdf/*.pdf
Expand Down Expand Up @@ -36,6 +37,11 @@ if [ "" = "$pdftk" ]; then
fi

$phantom $bin/phantom-slippy-to-pdf.js $1 tmp-pdf/
if [ "$?" != "0" ]
then
echo 'PhantomJS error, aborting.'
exit
fi

$pdftk tmp-pdf/*.pdf cat output $2
rm -r tmp-pdf/

0 comments on commit 94204d1

Please sign in to comment.