Open the pdf file in a new browser window using angularjs

I am new to angular js and I want to open the PDF in a new browser window after clicking a button.

I am making a GET request with $http.get() on the front panel, in the backend there is a Java service that responds with GET and generates a PDF. I want to open this PDF file in a browser.

If it is not possible to open a PDF in this way, then at least open any PDF using AngularJs, how can I do this?

 @GET @Path("/printPdf") public Response printService(){ //generates the pdf File reportFile = new File(filePath); String name = reportName + "." + "pdf"; ResponseBuilder response = Response.ok(new TemporaryFileInputStream(reportFile)); response.header("Content-Disposition", "attachment; filename=" + name); response.header("Content-Type", "application/pdf"); response.header("Access-Control-Expose-Headers", "x-filename"); response.header("x-filename", name); return response.build(); } 

this is what is on the backend to generate a response in the recreation service.

+8
angularjs browser get pdf
source share
2 answers

If you have something like this:

 var myPdfUrl = 'something' $http.get(myPdfUrl); 

Do this instead:

 var myPdfUrl = 'something' $window.open(myPdfUrl); 

If you have something like this:

 $http .get(generatePdfUrl) .then(function(data){ //data is link to pdf }); 

Do it:

 $http .get(generatePdfUrl) .then(function(data){ //data is link to pdf $window.open(data); }); 
+13
source share

Perhaps this may help if you have something like this:

 $http.get('generatePdfUrl') .then(function (data) { // data is your url var file = new Blob([data], {type: 'application/pdf'}); var fileURL = URL.createObjectURL(file); }); 

Then use your service in the controller and

 $window.open(fileURL); 
+12
source share

All Articles