Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There's an external webservice accessed directly by browser that takes the user to a web page which deals the request according to the parameters passed in the url and then returns a pdf file directly in the browser view.

I'm working on an application that aims to make a call to this webservice and return a generated .pdf file to the user.

What I have tried:

To get this I tried to use the request npm package as follows:
JavaScript
var request = Meteor.npmRequire ( 'request');

request.GET ({
              url: this_nfse.link_nfse, timeout: 1500 jar: true,
              encoding: 'binary'
              }
             , Meteor.bindEnvironment (function (error, response, body) {
                 if (!error && response)
                 {
                 // Handle result according to the application purpose
                 }
            }));


But the body object that I get is the html page that is reponsible in deal with the pdf file request, not the file or representation of itself.

I'm a beginner in nodejs/meteor, and any help will welcome.
Posted
Updated 18-Aug-16 1:22am
v2

1 solution

You can try. I have already worked for this. This example will help you.

JavaScript
var path = require('path');
var mime = require('mime');

app.get('/download', function(req, res){

  var file = __dirname + '/upload-folder/dramaticpenguin.MOV';

  var filename = path.basename(file);
  var mimetype = mime.lookup(file);

  res.setHeader('Content-disposition', 'attachment; filename=' + filename);
  res.setHeader('Content-type', mimetype);

  var filestream = fs.createReadStream(file);
  filestream.pipe(res);
});file);
  filestream.pipe(res);
});




If you want to send the file to front end for handle then you can try :

JavaScript
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);
  }); 
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900