使用 angularjs 在新的 window 浏览器中打开 PDF

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

我是 angular js 的新手,我希望在按下按钮后在新的 window 浏览器中打开 PDF 文档。

我在前端使用 $http.get() 发出 GET 请求,在后端有一个 Java rest 服务响应 GET 并生成 PDF。我想在浏览器上打开此 PDF。

如果无法以这种方式打开 PDF,那么至少可以使用 AngularJs 打开任何 PDF,我该怎么做?

@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();
}

这是后端在其余服务中生成响应的内容。

如果你有这样的经历:

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

改为这样做:

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

如果你有这样的东西:

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

这样做:

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

如果您有这样的事情,也许这会有所帮助:

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

然后在控制器中使用您的服务并执行

$window.open(fileURL);