Nodemailer 使用 wkhtmltopdf 附加 pdf 格式

问题描述 投票:0回答:1

我正在使用

https://github.com/devongovett/node-wkhtmltopd
来制作 pdf。问题是邮件已到达,但 pdf 格式为 0 字节。
out.pdf
中生成的pdf显示正确,所以问题出在附加的时候。我创建了一个变量
ReadWriteStream
,然后创建了
ReadStream
,因为只有该数据类型接受节点邮件程序的附件。


async main() {
    const pdf = wkhtmltopdf('<h1>Test</h1><p>Hello world</p>')
                     .pipe(fs.createWriteStream('out.pdf'));

   const content = fs.createReadStream('out.pdf');

   await mailer.sendMail({
       from: "[email protected]",
       to: "[email protected]",
       subject: 'Title',
       text: 'Email body',
       attachments: [{
           filename: 'out.pdf',
           content: content,
       }]
   })
}
javascript node.js nodemailer
1个回答
0
投票

ejs.renderFile( 位置模板, { 实体:发票, num: 发票.num, 项目, upiQr, 银行, 总计:

${currencySymbol} ${grandTotal.toFixed(2)}
, 总计:
${currencySymbol} ${invoice.total.toFixed(2)}
, SGST:
${currencySymbol} ${invoice.sgst.toFixed(2)}
, CGST:
${currencySymbol} ${invoice.cgst.toFixed(2)}
, 伊斯特:
${currencySymbol} ${invoice.igst.toFixed(2)}
, 标题:“发票”, billMetaHeading: "发票信息", partyMetaHeading:“账单至”, }, (错误,html)=> { 如果(错误)抛出错误; 常量缓冲区 = []; wkhtmltopdf( html, { 启用本地文件访问:true, 页面大小:“A4”, }, (错误,流)=> { 如果(错误)抛出错误; 流.on(“数据”,函数(数据){ bufs.push(数据); }); Stream.on("结束", 异步函数 () { const pdfBuffer = new Buffer.concat(bufs); 等待 Transporter.sendMail({ 至:“[电子邮件受保护]”, 来自:“输入代码[电子邮件受保护]”, 附件:[ { 文件名:“发票.pdf”, 内容:pdfBuffer, }, ], 文本:“这是您的发票”, }); return res.status(200).json({ message: "发票已发送" }); }); } ); } );

© www.soinside.com 2019 - 2024. All rights reserved.