【发布时间】:2017-03-27 09:13:37
【问题描述】:
我有一个项目列表,每个项目都有一个说明列表。每个项目将被打印成 PDF 文件,每条指令占用一页并保存在本地。我现在拥有的是一个 for 循环,它查询数据库以获取每个项目的说明,并在填充 PDF 后将其输出到路径。问题是由于查询是异步的,只能打开最后一个通过管道传输的 PDF,其余的都已损坏。我该如何克服这个?我的代码如下所示。
var counter = 0
for (var o=0; o<itemList.length; ++o){
Steps.find( {itemid:itemid[o], sort: "sequence asc"} ).exec( function(err,steps){
doc[counter] = new PDFDocument();
if(!err){
doc[counter].pipe( fs.createWriteStream(path + "DocName" + counter + ".pdf") );
for (var x=0; x<steps.length; ++x){
doc[counter].text("Instructions: " + steps[x].instruction.font('Times-Roman', 13);
}
***/*if (counter == itemList.length-1){
doc[counter].end();
}*/***
//That is the problem, i shouldn't have done the check and end the doc immediately, that solved the issue.
doc[counter].end();
++counter;
}
}
}
【问题讨论】:
标签: node.js sails.js node-pdfkit