簡體   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