node.js html-pdf 转换问题,文件返回损坏
node.js html-pdf conversion issue, file is coming back corrupt
我正在尝试创建一个应用程序,该应用程序将使用 html 文件,该文件使用 jquery get 请求填充。我正在发出 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.
我正在尝试创建一个应用程序,该应用程序将使用 html 文件,该文件使用 jquery get 请求填充。我正在发出 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.