我正在创建一个从url下载pdf文件的应用程序,并在我的仪表板页面中以网格方式显示。
我正在使用带有快速框架的node.js.
exports.pdf = function(req, response) {
var url ="http://www.ieee.org/documents/ieeecopyrightform.pdf";
http.get(url, function(res) {
var chunks = [];
res.on('data', function(chunk) {
console.log('start');
chunks.push(chunk);
});
res.on("end", function() {
console.log('downloaded');
var jsfile = new Buffer.concat(chunks).toString('base64');
console.log('converted to base64');
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "X-Requested-With");
response.header('content-type', 'application/pdf');
response.send(jsfile);
});
}).on("error", function() {
console.log("error");
});
};
答案 0 :(得分:2)
我以前在node js中下载pdf的简单解决方案是 npm i node-downloader-helper ,只需添加下载网址:
const { DownloaderHelper } = require('node-downloader-helper');
const download = new DownloaderHelper('url', __dirname);
download.on('end', () => console.log('Download Completed'))
download.start();
答案 1 :(得分:1)
答案 2 :(得分:0)
对于那些希望下载PDF服务器端(与OP有点不同的用例)的人来说,这是我使用request npm模块进行的操作:
const fs = require("fs");
const request = require("request-promise-native");
async function downloadPDF(pdfURL, outputFilename) {
let pdfBuffer = await request.get({uri: pdfURL, encoding: null});
console.log("Writing downloaded PDF file to " + outputFilename + "...");
fs.writeFileSync(outputFilename, pdfBuffer);
}
downloadPDF("https://www.ieee.org/content/dam/ieee-org/ieee/web/org/pubs/ecf_faq.pdf", "c:/temp/somePDF.pdf");