1

I'm trying to download a pdf file generated at nodejs server. When I send the data of pdf file then it is coming to angularjs frontend but I've tried blob object to convert into pdf but pdf is showing no data.

Already tried the {responseType: 'arraybuffer'} as mentioned in other answers but still my pdf is not showing any data.

Here is nodejs server which will serve the pdf data:

var pdff = require('html-pdf');
pdff.create(html, options).toFile('./report.pdf', function(err) {
if (err) return console.log(err);
  res.contentType("application/pdf");
  res.download("./report.pdf");

});

In the above code - report.pdf is generated at the server which is completely fine.

Here is the angularjs code:

   app.controller("ExampleCtrl",['FileSaver', 'Blob','$http', function(FileSaver, Blob,$http) {
  var vm = this;
        vm.consolidate=function(){
         $http.post('/mngmnt/consolidate_grv',{responseType: 'arraybuffer'}).then(function(response){
        //console.log(response.data);
        console.log(response);
        //var byteArray = new Uint8Array(response.data);
       var blob = new Blob([response.data], {  type: 'application/pdf'});
         console.log(blob);
          var fileURL = URL.createObjectURL(blob);
        //FileSaver.saveAs(blob, 'report.pdf');
        window.open(fileURL)

     });
   }
}]);

Everything is working properly but the generated pdf is not showing the same data as shown on the server side.

I'm expecting to generate the same pdf as generated at the server side

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76

1 Answers1

0
app.controller("ExampleCtrl", function(FileSaver, Blob,$http) {
     var vm = this;
     vm.consolidate=function(){
         $http.post('/mngmnt/consolidate_grv', {}, {responseType: 'blob'})
           .then(function(response){
             //console.log(response.data);
             console.log(response);
             //var byteArray = new Uint8Array(response.data);
             var blob = response.data;
             console.log(blob);
             var fileURL = URL.createObjectURL(blob);
             //FileSaver.saveAs(blob, 'report.pdf');
             window.open(fileURL)    
         });
     };
});

If you insist on using a POST request, the second argument to the $http.post method needs to be the data for the body of the request. The options object needs to be the third argument.

georgeawg
  • 48,608
  • 13
  • 72
  • 95