我是角度js的新手,我希望在按下按钮后在浏览器的新窗口中打开PDF文档。
我在前端使用$http.get()
发出GET请求,在后端有一个Java休息服务,它响应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();
}
这就是后端在休息服务中生成响应的内容。
答案 0 :(得分:16)
如果你有这样的事情:
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);
});
答案 1 :(得分:16)
也许这会有所帮助,如果你有这样的事情:
$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);