Open a PDF in a new window of the browser with angularjs

坚强是说给别人听的谎言 提交于 2019-11-27 02:45:24

问题


I'm new to angular js and I wish to open a PDF document in a new window of the browser after pressing a button.

I make a GET request with $http.get() at front end, at backend there is a Java rest service that respond to the GET and generates a PDF. I wish to open this PDF on the browser.

If is not possible to open the PDF in this way then at least open any PDF with AngularJs, how could 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 there is at backend to generate the response in the rest service.


回答1:


If you had something like this:

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

Do this instead:

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

If instead you have something like this:

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

Do this:

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



回答2:


Maybe this can help,in the case of 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 controller and do

$window.open(fileURL);


来源:https://stackoverflow.com/questions/30053212/open-a-pdf-in-a-new-window-of-the-browser-with-angularjs

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