Skip to content

Instantly share code, notes, and snippets.

@pkra
Created May 25, 2015 07:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkra/52391c345ae48613e5e0 to your computer and use it in GitHub Desktop.
Save pkra/52391c345ae48613e5e0 to your computer and use it in GitHub Desktop.
PhantomJS MathJax PDF example
// via https://github.com/mathjax/MathJax/issues/1029#issuecomment-72348069 and http://c.utz.cc/mathjax/render.js
var webpage = require('webpage');
var capture = function (page, pageUrl, callback) {
page.open(pageUrl, function (status) {
var interval, allDone;
if (status !== 'success') {
callback(new Error('Error rendering page'));
return;
}
allDone = page.evaluate(function () {
if (window.MathJax) {
MathJax.Hub.Register.StartupHook('End', function () {
window.allDone = 1;
});
return false;
} else {
return true;
}
});
if (allDone) {
callback();
return;
}
interval = setInterval(function () {
var allDone = page.evaluate(function () {
return window.allDone;
});
if (allDone) {
clearInterval(interval);
callback();
}
}, 100);
});
};
var page = webpage.create();
page.paperSize = {
format: 'Letter',
orientation: 'portrait',
margin: {left: '.39in',
right: '.38in',
top: '.39in',
bottom: '0in'}
};
var pageUrl = 'http://c.utz.cc/mathjax/index2.html';
capture(page, pageUrl, function (err) {
if (err) {
console.log(err);
} else {
page.render('test.pdf', {format: 'pdf'});
}
phantom.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment