【问题标题】:PDFKIT piping multiple PDF files in a loopPDFKIT 在一个循环中管道多个 PDF 文件
【发布时间】: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


【解决方案1】:

编辑了问题以显示有效的解决方案。一个粗心的错误,我只在最后一个文件写入后才执行 doc.end(),而不是在每个文件之后结束它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2020-10-06
    • 1970-01-01
    • 2020-04-04
    • 2014-10-27
    • 2018-04-29
    • 1970-01-01
    相关资源
    最近更新 更多