node.js - 如何使用 NodeJS FTP 上传缓冲区(pdf 缓冲区)?
问题描述
我使用 将 HTML 转换为 pdf html-pdf-node
,但我找不到使用 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?
});
}
谢谢
解决方案
使用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);
});
推荐阅读
- c++ - std::async 不产生线程
- c - 将文件名传递给函数
- python - w7 64_bit 上的 pyCharm
- android - 如何在 Android 上将“top”命令与交互式选项 1(对于许多内核)一起使用?
- java - (Java) 为什么我的代码没有打印出完整的数字?
- python - 类中的装饰器:有没有更好的方法
- google-cloud-firestore - 在 Firebase 中使用 DispatchQueue 更新 UI - SwiftUI
- javascript - easyAutocomplete 功能停止我的 keyup 处理程序
- r - 如何根据 R Shiny 中的传单地图的输入过滤数据表?
- python - Compare row data with previous row's data, if equal, state TRUE (Using Python)