[英]How to download pdf file from url in node.js?
I am creating an application to download pdf files from url and show in my dashboard page as grid wise.我正在创建一个应用程序来从 url 下载 pdf 文件并在我的仪表板页面中以网格方式显示。
I am using node.js with express framework.我正在使用带有 express 框架的 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");
});
};
For those looking to download a PDF server side, which is a bit of a different use case than the OP, here's how I did it using the request npm module:对于那些希望下载 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");
Simple solution I used to download pdf in node js is by npm i node-downloader-helper and just add downlaod url:我曾经在 node js 中下载 pdf 的简单解决方案是通过npm i node-downloader-helper并添加下载 url:
const { DownloaderHelper } = require('node-downloader-helper');
const download = new DownloaderHelper('url', __dirname);
download.on('end', () => console.log('Download Completed'))
download.start();
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-04-01
* @modified
*
* @description Node.js pdf crawler
* @augments
* @example
* @link
*
*/
// 1. commonjs module using `require` keyword
const fs = require("fs");
const path = require("path");
const { exit } = require("process");
const request = require("request-promise-native");
const log = console.log;
// 2. custom download folder
const folder = path.resolve(__dirname, '../pdf');
// log('folder', folder);
// 3. check if the folder exists, if not create it
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
async function downloadPDF(url, filename) {
log('🚧 pdf downloading ...');
const pdfBuffer = await request.get({
uri: url,
encoding: null,
});
// 4. write file to local file system
fs.writeFileSync(filename, pdfBuffer);
log('✅ pdf download finished!');
// 5. exit the terminal after download finished
exit(0);
}
const url = 'https://cs193p.sites.stanford.edu/sites/g/files/sbiybj16636/files/media/file/l1.pdf';
const filename = folder + '/cs193p-2021-l1.pdf';
// log('filename =', filename);
downloadPDF(url, filename);
https://nodejs.org/api/process.html#exit-codes https://nodejs.org/api/process.html#exit-codes
If anyone is having issues (like I was) actually getting the PDF to open in a PDF viewer when using the request module, try setting the encoding in your call to null.如果有人在使用请求模块时遇到问题(就像我一样)实际上让 PDF 在 PDF 查看器中打开,请尝试将调用中的编码设置为 null。 Something like this:
像这样的东西:
async function downloadPDF(pdfURL, outputFilename, token) {
const options = {
url: pdfURL,
headers: {
'Content-Type': 'application/pdf',
'Authorization': 'Bearer '+token,
},
encoding: null
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.