Empty PDF when downloading using Spring Boot and Angularjs

Asked

Viewed 245 times

0

I am creating an application where I need to return a PDF through an API call developed in Spring Boot through Angularjs, my code is like the below:

Return of the API:

ResponseEntity.ok()
        .headers(result.getHttpHeaders())
        .body(new InputStreamResource(result.getInputStream()));

Where the header is application/pdf

Code Angularjs:

$http.get("path/pdf", 
    { params: params },
    { responseType: "arraybuffer" }
)
.then(function(response){
     console.log(response.data);
     var blob = new Blob([response.data], {type: 'application/pdf'});            
     FileSaver.saveAs(blob, 'arquivo.pdf');
});

As a result the PDF generated by Filesaver is empty, even the sponse.date., with the data.

When calling the API url directly from the browser the PDF is returned normally.

  • Ever tried to change the responseType for blob and call straight FileSaver.saveAs(response, 'arquivo.pdf') or FileSaver.saveAs(response.data, 'arquivo.pdf')?

  • Yes already tried, Filesaver.saveAs receives an instance of Blob when changing an error happens, and changing only responseType the pdf keeps coming blank.

1 answer

1

Problem solved by changing the method on Angularjs:

$http.post(mobioneEnvService.addPath("/operation/report/alerts/export"), 
           params, 
           {responseType:"arraybuffer"})
     .then(function(response){

            var blob = new Blob([response.data], {type: 'application/pdf'});

            FileSaver.saveAs(blob, 'report.pdf');
     });

The API should also accept calls via POST.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.