AssertionError:使用html-pdf生成pdf时

时间:2019-01-06 22:07:07

标签: node.js mongodb express html-pdf

我正在使用Nodejs,express和Mongodb来构建应用程序。

使用npm的html-pdf模块生成pdf文件时,出现如下所示的终端错误。该错误并非每次都发生,而是几次发生,然后应用程序崩溃。

我从mongodb调用要写入pdf文件的数据,然后将其转换为“ bill.html”文件中的元素。

什么会导致此错误?

assert.js:42
  throw new errors.AssertionError({
  ^

AssertionError [ERR_ASSERTION]: html-pdf: Can't create a pdf without an html string
at new PDF (/Users/myapp/node_modules/html-pdf/lib/pdf.js:39:3)
at Object.createPdf [as create] (/Users/myapp/node_modules/html-pdf/lib/index.js:10:14)
at /myapp/app/routes/api.js:2502:45
at FSReqWrap.oncomplete (fs.js:135:15)
[nodemon] app crashed - waiting for file changes before starting...

api.js

var path = require('path'); // Import path module
var fs = require('fs');
let pdf = require('html-pdf');  
router.get('/pdf/:id', function(req, res){
    var path_to_pdf = path.join(__dirname, '..', '/pdf/');

    var settingInfo = {};
    //defining the path for the new file to be temporarily saved and the name of the file
    var mergeFileRes  = path_to_pdf + 'bill_merge.html';

    //Where you make a copy of the template and store the content in the temporary file that you just made
    var copy =   fs.copyFileSync(path_to_pdf + 'bill.html', mergeFileRes);

    CompanySetting.findById({_id: req.body.id }, function(err, setting){
        if(err){
            console.log(err);
            res.json({ success: false, message: 'Something went wrong. Please contact to us. We apologize for this inconvenience.' });
        }else{
            Bill.findById({_id: req.params.id}).populate('customer').exec(function(err, bill){
                if(err){
                    console.log(err);
                    res.json({ success: false, message: 'Something went wrong. Please contact to us. We apologize for this inconvenience.' });
                }else{

                    //Set the header variables
                    res.set('Content-type', 'application/pdf');
                    res.set('Content-disposition', 'attachment; filename= bill.pdf');
                    res.set('accept-ranges', 'bytes');
                    res.set("Content-Transfer-Encoding", "binary"); 
                    settingInfo = setting;

                    var html = fs.readFileSync(path_to_pdf + 'bill.html', 'utf8');
                    fs.createReadStream(path_to_pdf + 'bill.html', 'utf8').pipe(fs.createWriteStream(mergeFileRes, 'utf8'));

                    fs.readFile(mergeFileRes, 'utf8', function(err, data){
                        if(err){
                            console.log(err);
                            res.json({ success: false, message: 'Something went wrong. Please contact to us. We apologize for this inconvenience.' });
                        }else{

                            var setter = {

                                "PHONE": setting.phone_2,
                                "MOBILE": setting.mobile_2,
                                "EMAIL": setting.email_2,
                                "WEBSITE": setting.website_2,

                                "BUSINESS_NAME": bill.customer.business_name,

                            }

                            Object.keys(setter).forEach(function(element, key, _array) {
                              var regex = new RegExp(element, "g");

                              data = data.replace(regex, setter[element])
                            })

                            var result = data;

                            fs.writeFile(mergeFileRes, result, 'utf8', function (err) {
                                if(err) { console.log(err); return;}
                                else {
                                    let html2 = fs.readFileSync(mergeFileRes, 'utf8');
                                    let options = { format: 'A4' , 
                                                    "directory" : "/tmp",
                                                };


                                    pdf.create(html2, options).toStream(function(err, stream2){
                                        console.log(err);
                                        stream2.pipe(res);

                                        stream2.on('end', function () {
                                            try{

                                                fs.unlink(mergeFileRes)
                                            }
                                            catch (err){
                                                console.log(3090, "Did not delete file");
                                            }
                                        });
                                    });


                                }
                            });
                        }
                    });//fs.readFile
                }
            })//Bill
        }
    }); //CompanySetting
});

0 个答案:

没有答案