繁体   English   中英

用jspdf创建PDF,通过nodemailer Node.js发送

[英]Create PDF with jspdf and send via nodemailer Node.js

在我的 Node.js 服务器中,我正在创建一个 PDF 文件,(但我没有将它保存在我的磁盘上),我的想法是创建 PDF 文件,然后通过 nodemailer 发送它,这是我的代码:

应用程序.js

const { jsPDF }   = require("jspdf");

const doc = new jsPDF();
doc.text("Hello world!", 10, 10);

email_send.send_pdf(doc);

email_send.js

exports.send_pdf = function(doc) {
  var transporter = helpers.transporter();
  var mailOptions = {
  from:    'xxx@email.com',
  to:      'xxx@email.com',
  subject: "PDF",
  html:
  '<!DOCTYPE html>'+
  '<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">'+
    '<head>'+
      '<meta charset="utf-8">'+
      '<title>THIS IS AN EMAIL</title>'+
      '<meta name="viewport" content="width=device-width,initial-scale=1" />'+
      '<meta name="x-apple-disable-message-reformatting" />'+
      '<style>'+
        'table,'+
        'td,'+
        'div,'+
        'h1,'+
        'p {'+
          'font-family: Arial, sans-serif;'+
        '}'+
      '</style>'+
    '</head>'+
    '<body style="margin: 0; padding: 0; padding-top: 20px">'+
      '<p style="margin: 0 0 12px 0; font-size: 10px; line-height: 24px; font-family: Arial, sans-serif">'+
        '<b>Your email with pdf file is:</b> '+
      '</p>'+
    '</body>'+
  '</html>',
  attachments: [
    {
      filename: "mypdf.pdf",
      content: doc.output('arraybuffer')
    }
  ]
  };
  transporter.sendMail(mailOptions)
  .then(function(info) {
    console.log('success');
  })
  .catch(function(error) {
    console.log("----ERROR------");
  }); 
}

我看到一些博客说:把像这样的东西doc.output('arraybuffer') or doc.output('datauristring')但是我总是有以下错误:

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object )

那么,我该如何解决呢,都是在Node.js服务器上开发的。

看起来nodemailer想要一个“真正的”附件Buffer ,您可以ArrayBuffer创建它:

Buffer.from(doc.output('arraybuffer'))

暂无
暂无

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

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