how to download pdf using NodeJS and send it to the client?

不想你离开。 提交于 2020-01-10 10:03:44

问题


I am trying to download a PDF file using NodeJS then send its data to client to be embedded in the page. Here is how I download the PDF file:

exports.sendPdf = function(req, responce) {
    var donneRecu = req.body;
    var url = 'http://www.ieee.org/documents/ieeecopyrightform.pdf'//pdf link
    http.get(url, function(res) {
        var data = '';
        res.on('data', function(chunk) {
            console.log('downloading');
            data += chunk;
        });
        res.on("end", function() {
            console.log('downloaded');
            responce.header("Access-Control-Allow-Origin", "*");
            responce.header("Access-Control-Allow-Headers", "X-Requested-With");
            responce.header(200, {'content-type' : 'application/pdf'});
            responce.send(data);
        });
    }).on("error", function() {
        callback(null);
    });
}

How do I send the data received from NodeJS to the client side?

EDIT i found the solution :

exports.sendPdf = function(req, res) {

  var donneRecu = req.body;

  console.log(donneRecu['lien']);

  var url = donneRecu['lien']; //pdf link

  http.get(url, function(response) {

      var chunks = [];

      response.on('data', function(chunk) {

          console.log('downloading');

          chunks.push(chunk);

      });

      response.on("end", function() {
          console.log('downloaded');
          var jsfile = new Buffer.concat(chunks).toString('base64');
          console.log('converted to base64');
          res.header("Access-Control-Allow-Origin", "*");
          res.header("Access-Control-Allow-Headers", "X-Requested-With");
          res.header('content-type', 'application/pdf');
          res.send(jsfile);
      });
  }).on("error", function() {
      callback(null);
  }); 
}

next in my angular controller:

var pdf = $scope.base64ToUint8Array(data);    
PDFJS.getDocument(pdf).then(functiongetPdfHelloWorld(_pdfDoc) {
$scope.pdfDoc = _pdfDoc;$scope.renderPage($scope.pageNum); });

回答1:


i found the solution :

exports.sendPdf = function(req, res) {

  var donneRecu = req.body;

  console.log(donneRecu['lien']);

  var url = donneRecu['lien']; //pdf link

  http.get(url, function(response) {

      var chunks = [];

      response.on('data', function(chunk) {

          console.log('downloading');

          chunks.push(chunk);

      });

      response.on("end", function() {
          console.log('downloaded');
          var jsfile = new Buffer.concat(chunks).toString('base64');
          console.log('converted to base64');
          res.header("Access-Control-Allow-Origin", "*");
          res.header("Access-Control-Allow-Headers", "X-Requested-With");
          res.header('content-type', 'application/pdf');
          res.send(jsfile);
      });
  }).on("error", function() {
      callback(null);
  }); 
}

next in my angular controller:

var pdf = $scope.base64ToUint8Array(data);    
PDFJS.getDocument(pdf).then(functiongetPdfHelloWorld(_pdfDoc) {
$scope.pdfDoc = _pdfDoc;$scope.renderPage($scope.pageNum); });



回答2:


I too faced the same situation and the below code helped me.

var fs = require("fs");

var file = fs.createReadStream('./public/modules/datacollectors/output.pdf');
    var stat = fs.statSync('./public/modules/datacollectors/output.pdf');
    res.setHeader('Content-Length', stat.size);
    res.setHeader('Content-Type', 'application/pdf');
    res.setHeader('Content-Disposition', 'attachment; filename=quote.pdf');
    file.pipe(res);

Hope it helps.



来源:https://stackoverflow.com/questions/22171510/how-to-download-pdf-using-nodejs-and-send-it-to-the-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!