HTML-PDF转换

时间:2019-05-10 08:55:54

标签: node.js html-pdf node-html-pdf

我想在node.js中转换html-pdf转换。我尝试了phantomjs medthod,它在本地工作正常,但在服务器中却无法正常工作。我不明白为什么它不起作用。

下面是我用来转换html-pdf的处理程序

handlers.generatePDF = async function(data,callback){
    var str = data.payload.html;
    var options = { format: 'A4',
                    border: {"top": "1cm","right": "1cm","bottom": "1cm","left": "1cm"},
                    phantomPath: 'D:/review/phantomjs/bin/phantomjs.exe'
                    //base: 'file:///' + __dirname + '/public/images/'
                  };
    if(data.payload.type == 'UTS'){
      options.orientation = 'landscape';
      str = str.replace(str.substring(str.indexOf('<!--UTS_Head_start-->'), str.indexOf('<!--UTS_Head_end-->')), "");
      str = str.replace('<div class="fr-box fr-basic fr-top">', '<div class="fr-box fr-basic fr-top" style="display:none">');
      var utscondition = {'ticket_repo':data.payload.ticket_repo,'issue':data.payload.ticketId,'uts_revision_no':data.payload.revisionNo};
      var result = await _data.findOne(utsModel,utscondition);
      str = str.replace('Unit Test Results:','Unit Test Results: \n\n'+(result.toObject()).case_screenshots);
      var replaceStr = 'http://localhost:3001/public/images/';
      var putStr = 'file:///'+ __dirname + '/../public/images/';
      while(str.indexOf(replaceStr)>-1){
        str = str.replace(replaceStr,putStr);
      }
    }
    var fileName = data.payload.type+'_'+data.payload.ticket_repo+'_'+data.payload.ticketId+'_revision_'+data.payload.revisionNo+'.pdf';
    var filePath = process.env.DOWNLOAD_PATH+'\\'+fileName;
    pdf.create(str, options).toFile(filePath, function(err, res) {
      if (err)  console.log(err);
      callback(200,{'fileName':fileName});
    });
};

0 个答案:

没有答案