【问题标题】:How to FTP upload a buffer (pdf buffer) using NodeJS?如何使用 NodeJS FTP 上传缓冲区(pdf 缓冲区)?
【发布时间】:2021-11-02 15:25:09
【问题描述】:

我使用html-pdf-node 将 HTML 转换为 pdf,但我找不到使用 FTP 将此 PDF 存储在我的服务器中的方法。

我的代码:

const html_to_pdf = require('html-pdf-node');

const generatePDF = () => {
   // The test HTML file
   let content = `
      <html>
         <head>
            <title>Test Application</title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
            <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
         </head>
         <body>
            <div class="container">
               <h2>Just a Test</h2>
            </div>
         </body>
      </html>
   `;
    
    // generating the PDF 
    let options = { 
        format: 'letter',
        margin: {
            right: '40px',
            left: '40px'
        } 
    };
    let file = { content };
    html_to_pdf.generatePdf(file, options).then((pdfBuffer) => {
       console.log(pdfBuffer); // This is the pdfBuffer. It works because if I send this buffer to my email as an attachment, I can open the PDF
       // How can I create and store a test.pdf file inside my server using FTP connection?
    });
}

谢谢

【问题讨论】:

    标签: node.js pdf ftp


    【解决方案1】:

    使用ftp 应该可以做到 - https://www.npmjs.com/package/ftp

    安装:npm i ftp 和用法类似:

    const Client = require("ftp");
    
    // snip snip some code here
    
    html_to_pdf.generatePdf(file, options).then((pdfBuffer) => {
      const c = new Client();
      c.on("ready", function () {
        c.put(pdfBuffer, "foo.pdf", function (err) {
          if (err) throw err;
          c.end();
        });
      });
    
      const secureOptions = {
          host: "localhost",
          port: 21,
          user: "TheGreatPotatoKingOfEurope",
          password: "5uper_5eekrit!"
      };
      c.connect(secureOptions);
    });
    

    【讨论】:

      猜你喜欢
      • 2023-03-02
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 2016-11-20
      • 2021-01-14
      • 2019-03-10
      • 2014-05-16
      • 1970-01-01
      相关资源
      最近更新 更多