[hummus pageModifier]:无法结束 PDF(随机)

[hummus pageModifier]: Unable to end PDF (Randomly)

我的图片是 png 格式。问题是随机的。 10 次中有 5 次出错。

这里是示例代码(fileStream.js):

function generateSignedDoc(imageFilePath, signedDocFilePath, termCondFilePath, callback){
  try {
    var pdfWriter = hummus.createWriterToModify(termCondFilePath, {modifiedFilePath: signedDocFilePath});
    var pageModifier = new hummus.PDFPageModifier(pdfWriter,4,true);
    var ctx = pageModifier.startContext().getContext();
    ctx.drawImage(70,180, imageFilePath, {transformation:{width:150,height:100, proportional:true}});
    pageModifier.endContext().writePage();
    pdfWriter.end();
    if(fs.existsSync(signedDocFilePath)){
      callback(null, "successfully created PDF");
    } else {
      callback("Error while creating document.");
    }
  } catch(e){
  console.log("try....catch.....e.....", e);
  callback(e);
  }
}

控制器(user.js):

req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
    var agreementFile = "UserAgreement_" +  new Date().getTime();
    filename = agreementFile + '.' + (filename.substring(filename.lastIndexOf('.')+1)).toLowerCase();
    signedDocFileName = agreementFile + '.pdf';
    paths.imageFilePath = __dirname + '/../../utility' + Constant.templocalbucket + filename;
    paths.termCondFilePath = __dirname + '/../../resource/Term&Cond.pdf';
    paths.signedDocFilePath = __dirname + '/../..' + Constant.tempSignedDocbucket + signedDocFileName;

    fstream = fs.createWriteStream(paths.imageFilePath);
    file.pipe(fstream);
  });

req.busboy.on('finish', function() {
  Q.nfcall(FileStreamService.generateSignedDoc, paths.imageFilePath, paths.signedDocFilePath, paths.termCondFilePath)
  .then(function(){
    if(fs.existsSync(paths.signedDocFilePath)){
      return true;
    } else {
      console.log("In else..... file path not found....");
      throw {message : 'System file path not found'};
    }
  })
  .catch(function(err){
    console.log("catch block.........", err);
    Logger.info(JSON.stringify(err));
    return res.status(500).send({
      code: 500,
      message: 'Internal server error.'
    });
  });
});

错误:

try....catch.....e..... TypeError: Unable to end PDF
    at generateSignedDoc (/Users/hardik.shah/git/BE/fileStream/service/fileStream.js:38:15)
    at Promise.apply (/Users/hardik.shah/git/BE/node_modules/q/q.js:1185:26)
    at Promise.promise.promiseDispatch (/Users/hardik.shah/git/BE/node_modules/q/q.js:808:41)
    at /Users/hardik.shah/git/BE/node_modules/q/q.js:1411:14
    at runSingle (/Users/hardik.shah/git/BE/node_modules/q/q.js:137:13)
    at flush (/Users/hardik.shah/git/BE/node_modules/q/q.js:125:13)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)

我想知道,为什么这是随机发生的?

问题是,hummusfileStream 之前处理 png 图像关闭。所以,我需要先处理 fstream 关闭事件,然后将图像提供给鹰嘴豆泥。

req.busboy.on('finish', function() {
  fstream.on('close', function () { // This, I should take care about.
    Q.nfcall(FileStreamService.generateSignedDoc, paths.imageFilePath, paths.signedDocFilePath, paths.termCondFilePath)
    .then(function(){
      if(fs.existsSync(paths.signedDocFilePath)){
        return true;
      } else {
        throw {message : 'System file path not found'};
      }
    })
    .catch(function(err){
      return res.status(500).send({
        code: 500,
        message: 'Internal server error.'
      });
    });
   });
  });