gpt4 book ai didi

jquery - node.js html-pdf 转换问题,文件返回损坏

转载 作者:搜寻专家 更新时间:2023-10-31 22:24:36 27 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,它将采用一个使用 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'See the docs here.

关于jquery - node.js html-pdf 转换问题,文件返回损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42490993/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com