我正在尝试使用PDFkit动态创建pdf,并希望将其作为附件发送到电子邮件中。在此http://pdfkit.org/demo/browser.html示例和https://nodemailer.com/using-attachments/文档之后,我编写了以下代码:
var doc = new PDFDocument();
var stream = doc.pipe(blobStream());
doc.text("Howdy!!");
doc.on('end');
stream.on('finish', function() {
var htmlMailBody ='Hi'
var textMailBody = 'hi';
var mailOptions =
{
from: 'ASD', // sender address
to: '[email protected]', // list of receivers
subject: 'Invitation ', // Subject line
text: textMailBody, // plaintext body alt for html
html: htmlMailBody,
attachments:[
{
filename:"TEST1.pdf",
path:stream.toBlobURL('application/pdf')
}]
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
res.redirect('/');
});
});
但我收到以下错误:
TypeError: listener must be a function
at PDFDocument.addListener (events.js:197:11)
at PDFDocument.Readable.on (_stream_readable.js:665:33)
at exports.getSendReport (d:\projects\PDFChecker\server\controllers\pdf.js:159:6)
at Layer.handle [as handle_request] (d:\projects\PDFChecker\node_modules\express\lib\router\layer.js:95:5)
我应该如何解决?
最佳答案
pdfkit
实例是一个流,您可以将其简单地传递给nodemailer
:
const doc = new pdfkit();
transport.sendMail({
from: '...',
to: '...',
subject: '...',
text: '...',
attachments: [{
filename: 'attachment.pdf',
content: doc,
}],
});
关于node.js - 使用Node.js应用程序中的PDFKit发送动态创建为附件的PDF,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40275397/