我正在尝试触发在DOM中呈现的D3生成元素的wkhtmltopdf
- 化服务器端,然后将最终的PDF传递回响应中的客户端。
简而言之,这就是我正在做的事情:
Meteor.methods({
downloadPdf: function(data, node) {
var filename = data.slug + "-" + data.size,
headers = {
'Content-Type': 'application/pdf',
'Content-Disposition': "attachment; filename=" + filename + ".pdf"
};
this.response.writeHead(200, headers);
var r = wkhtmltopdf(node).pipe(this.response);
return r;
}
});
我在客户端上这样称呼它:
Template.getPDF.events({
'click .pdf-export-button': function(e) {
var data = Router.current() && Router.current().data();
if (data) {
var node = d3.select(".pdf-export-preview").node().innerHTML;
Meteor.call("downloadPdf", data, node, function(err, result) {
// response goes here?
});
}
}
});
问题是,上面的代码在Meteor.call
回调中返回500错误,我的终端告诉我它是:Exception while invoking method 'downloadPdf' TypeError: Cannot call method 'writeHead' of undefined
。似乎表明响应还没有准备好,或某种类似的东西。不确定如何解决这个问题。任何帮助将不胜感激!
P.S。我找到的一个解决方案是use Iron Router to trigger the PDF download,但这是一个问题,因为我需要能够将渲染的D3 SVG传递给wkhtmltopdf(以及PDF的一些配置内容)。