Search code examples
node.jshummus.js

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


My Image is in png format. The issue is random. Out of 10, 5 times it gives an error.

Here is sample code (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);
  }
}

Controller (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.'
    });
  });
});

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)

I am wondering, why this is randomly happening?


Solution

  • The issue was, hummus is processing before fileStream for png image get closed. So, I need to take care of fstream close event first and then serve an image to hummus.

    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.'
          });
        });
       });
      });