I'm using phantomjs to render several specialized html pages to PDF; since this is a service that will need to scale, I'm trying to reduce as much overhead as possible. All pages are the same structure, use the same CSS, and are in most ways similar, so I decided to reuse the same html page and just call javascript to replace the content. This way it doesn't have to reload the CSS and web fonts and such.
To make things slightly more complicated, I'm controlling the render from node.js using a rest interface I created using phantom.js's webserver interface. The web server works great -- if I don't reuse the page, everything works perfectly.
The problem I'm having is that I have no way to know when I can report back to node (across the http connection) that the file is ready for use.
if (filename.substr(-4) == ".pdf") {
p.paperSize = {format: 'Letter', orientation: 'portrait', margin: '1cm'};
}
// loadPage evaluates javascript in the page to have it load the contents
// of the url into its body; the callback is triggered by a console.log
// inside the page that tells us when the ajax request has finished and
// the content is ready to be rendered.
loadPage(p, url, function() {
console.log("Loaded", url);
p.render(filename);
console.log("Rendered?", url);
// sendJSON returns the response to the node client
sendJSON(response, {filename: filename, status: "success"});
});
The problem I have is that sendJSON gets called before p.render finishes -- it's not blocking. Honestly, I wouldn't expect it to be blocking given that this is javascript, but it also doesn't seem to accept a callback function to let me know when it finishes.
Anyone found a solution for this type of problem?