Node.js HTML-PDF 转换问题,文件返回损坏

6
我尝试创建一个应用程序,它将获取使用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没有处理。

1个回答

4

我对nodemailer不是很熟悉,但我觉得你在附件上缺少了encoding: 'base64'这里查看文档。


1
最终的解决方案是将编码和使用attachments.toString()的组合,而不仅仅是attachments。 - user1790300

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接