繁体   English   中英

node.js html-pdf转换问题,文件重新损坏

[英]node.js html-pdf conversion issue, file is coming back corrupt

我正在尝试创建一个应用程序,它将使用jquery get请求填充html文件。 我正在制作一个http get请求来获取html,将字符串传递给pdf.create函数并使用生成的缓冲区通过电子邮件发送pdf文件。 这似乎处理并作为电子邮件附件发送文件,但是,当我尝试打开文件时,我收到一个错误,说文件已损坏。

code that convert html document to pdf:
                var options = {
                  host: 'localhost',
                  path: '/salesorder/' + orderid,
                  port: '3000'
                };

                http.request(options, function(response){
                  let buffer = '';
                  response.on('data', function (chunk) {
                      buffer += chunk;
                  });

                  response.on('end', function () {
                    pdf.create(buffer, {
                        directory: "tmp"
                    }).toBuffer(function(err, newbuffer){
                        if (err){
                            reject(err);
                        }

                        if (Buffer.isBuffer(newbuffer)){
                            resolve(newbuffer);
                        } else {
                            reject(new Error('The pdf file could not be generated at this time.  Please try again later.'));
                        }
                    });
                  });
                })
                .end();


code that sends the email:
    var transporter = nodemailer.createTransport({
        service: 'gmail',
        host: this.hostname,
        auth: {
            user: this.smtpusername,
            pass: this.smtppassword
        }
    });

    var mailOptions = {
        from: fromaddress, // sender address
        to: toaddresslist, // list of receivers
        subject: subject, // Subject line
        text: text
    };

    if (attachments){
        mailOptions.attachments = [
            {   // binary buffer as an attachment
                filename: 'invoice.pdf',
                content: new Buffer(attachments.toString(), 'base64'),
                contentType: 'application/pdf'
            }
        ];
    }

我正在使用html-pdf node.js包来执行转换。 我在这里错过了什么? 我正在从http.request方法获取html,如果我使用console.log将其打印出来,我可以看到生成的代码。 现在我注意到我没有看到文本字段和标签填充,所以它似乎不是jquery正在处理。

我对nodemailer没有经验,但在我看来你缺少encoding: 'base64'你的附件上的encoding: 'base64' 请参阅此处的文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM