【问题标题】:node.js html-pdf conversion issue, file is coming back corruptnode.js html-pdf 转换问题,文件回来损坏
【发布时间】:2017-07-18 09:01:12
【问题描述】:

我正在尝试创建一个应用程序,该应用程序将采用使用 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 似乎没有在处理。

【问题讨论】:

    标签: jquery node.js pdf pdf-generation


    【解决方案1】:

    我没有使用 nodemailer 的经验,但在我看来,您的附件中缺少encoding: 'base64'See the docs here.

    【讨论】:

    • 解决方案最终是结合编码和使用 attachments.toString() 而不仅仅是附件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    相关资源
    最近更新 更多