繁体   English   中英

如何使用nodemailer发送base64转换的pdf

[英]How to send base64 converted pdf using nodemailer

我的服务器上有pdf的base64版本,可以在console.log中查看其值。 该变量称为encodedpdf并且执行console.log(encodedpdf)显示pdf的base64版本。

Nodemailer允许我们发送bsae64文件作为附件,并使用缓冲区对其进行转换。 下面是代码

attachments: [{
            filename: 'main.pdf',
            content: new Buffer(encodedpdf,'base64')
          }]

但是,我不断收到此错误

TypeError:第一个参数必须是字符串,Buffer,ArrayBuffer,Array或类似数组的对象

EDIT1:

这是我的前端代码,我将pdf转换为base64并将其作为ajax发送

 var element = document.getElementById('element-to-print');
           html2pdf().from(element).outputPdf().then(function(pdf) {
            const newpdf = btoa(pdf);    //This line
            $.post('./setup', newpdf, function(result) {
                console.log('result', result);
            }); 

我在后端发布请求中收到大量随机字符串,因此我假设btoa(pdf)将我的pdf转换为base64。 这里可能有问题吗?

您正在使用的方法new Buffer(String,'base64')弃用

请使用以下方式将字符串编码为base64

Buffer.from("I am a tring").toString('base64') 

或使用nodemailer的内置函数发送base64编码的字符串pdf

filename: 'your.pdf',
content: 'encodedpdfstring', //EncodedString
encoding: 'base64'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM