将jsPDF生成的PDF发送到服务器

卢卡·迪里洛(Luca Di Liello)

我正在用jsPDF生成PDF客户端。我需要使用Axios将其发送到Express服务器。最后,我需要使用Nodemailer通过电子邮件发送它。我哪里错了?

客户端代码

//doc creation ....
var res = doc.output('datauristring');   //this line!!!!
axios.post('/mailsender', res).then((res) => {
    if(res.status === 'ok') console.log("Yeah!");
    else console.log(":(");
});

服务器端代码

...

api_router.post('/mailsender', (req, res) => {
    mail.send(req.body, (res) => {
        res.status(200).json({"status": res ? 'ok' : 'error' });
    });
});

mail.js是

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    host: 'smtp.mail.yahoo.com',
    port: 465,
    secure: true,
    auth: {
        user: '[email protected]',
        pass: 'password'
    }
});


exports.send = function (data, callback) {
    let mailOptions = {
        from: '"My application" <[email protected]>',
        to: "receiverAddress",
        subject: "Attachment experiment",
        text: "My <3",
        attachments: [
            {
                filename: 'attachment.pdf',
                content: data,
                contentType: 'application/pdf',
                encoding: 'base64'    //this line!!!!
            }
        ]
    };

    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
            callback(false);
        }
        callback(true);
    });
}

一切工作正常,除了如果我尝试打开收到的邮件中的附件,预览说文件已损坏。如果我尝试使用谷歌浏览器或其他PDF阅读器打开它,也是如此。可能必须更改带有注释的两行。感谢您的关注!

卢卡·迪里洛(Luca Di Liello)

这很简单:我只需要以这种方式更改附件部分:

attachments: [
    {
        path: data
    }
]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章