代码之家  ›  专栏  ›  技术社区  ›  Hardik Shah

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

  •  0
  • Hardik Shah  · 技术社区  · 8 年前

    我的形象在 png

    下面是示例代码( ):

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

    用户.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)
    

    1 回复  |  直到 8 年前
        1
  •  0
  •   Hardik Shah    8 年前

    问题是, hummus 正在处理之前 fileStream 关闭png图像。所以,我需要照顾 fstream 先关闭活动,然后为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.'
          });
        });
       });
      });
    
    推荐文章