18

I am creating an application to download pdf files from url and show in my dashboard page as grid wise.

I am using node.js with express framework.

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");
   }); 
};
1
  • post the code you are using Commented Sep 20, 2014 at 5:57

5 Answers 5

17

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:

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");
Sign up to request clarification or add additional context in comments.

2 Comments

this is not working it is giving me plain text file with pdf extension but not opening
@James It's not supposed to open. It's just supposed to save the file as c:\temp\somePDF.pdf. If you want it to open in your PDF viewer, that's a different question entirely.
3

Simple solution I used to download pdf in node js is by npm i node-downloader-helper and just add downlaod url:

const { DownloaderHelper } = require('node-downloader-helper');

const download = new DownloaderHelper('url', __dirname);
download.on('end', () => console.log('Download Completed'))
download.start();

Comments

2

This will Help.

response.setHeader("Content-Type", "text/pdf");

This Link will help

Comments

1

As of node 18, you can do this with no third party dependencies, using the native fetch API and the native filesystem API:

const pdfRespone = await fetch("https://example.com/file.pdf");
const pdfBuffer = await pdfRespone.arrayBuffer();
const binaryPdf = Buffer.from(pdfBuffer);
fs.writeFileSync("/your/file/name.pdf", binaryPdf, 'binary');

If you're dealing with very large files or have limited memory, you could also do this with streams, which would allow you to avoid loading the whole file into memory at once.

Comments

0

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. Something like this:

async function downloadPDF(pdfURL, outputFilename, token) {
  const options = {
    url: pdfURL,
    headers: {
      'Content-Type': 'application/pdf',
      'Authorization': 'Bearer '+token,
    },
    encoding: null
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.