Created
May 25, 2015 07:46
-
-
Save pkra/52391c345ae48613e5e0 to your computer and use it in GitHub Desktop.
PhantomJS MathJax PDF example
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
// 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