【问题标题】:How to send base64 converted pdf using nodemailer如何使用nodemailer发送base64转换的pdf
【发布时间】:2018-09-04 05:42:40
【问题描述】:

我的服务器上有一个 pdf 的 base64 版本,我可以使用 console.log 并查看其值。该变量名为encodedpdf,执行console.log(encodedpdf) 会显示pdf 的base64 版本。

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

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

但是,我不断收到此错误

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

编辑1:

这是我的前端代码,我将我的 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。这里会不会有问题?

【问题讨论】:

    标签: javascript node.js buffer nodemailer


    【解决方案1】:

    你使用new Buffer(String,'base64')的方式是deprecated

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

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

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

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

    【讨论】:

    • 我收到此错误 TypeError: Invalid non-string/buffer chunk using nodemailer
    • 我对我的问题做了一些修改。请你看一下好吗?
    猜你喜欢
    • 1970-01-01
    • 2017-11-26
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2016-03-30
    相关资源
    最近更新 更多