我使用phantomjs将多个专用html页面呈现为PDF;由于这是一项需要扩展的服务,因此我试图尽可能减少开销。所有页面都是相同的结构,使用相同的CSS,并且在大多数方面都很相似,所以我决定重用相同的HTML页面,并调用JavaScript来替换内容。这样它不必重新加载CSS和Web字体等。如何检测phantomjs何时完成渲染?
为了让事情稍微复杂一点,我使用一个使用phantom.js的webserver界面创建的休息界面来控制node.js的渲染。 Web服务器的工作效果很好 - 如果我不重复使用该页面,则一切正常。
我遇到的问题是我无法知道什么时候可以向节点报告(通过http连接)文件已准备好使用。
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"});
});
我的问题是sendJSON在p.render完成之前被调用 - 它没有阻塞。老实说,我不希望它被阻止,因为这是JavaScript,但它似乎也不接受回调函数让我知道它什么时候结束。
任何人都发现这种类型的问题的解决方案?