让ApachePDFBox与Spring AngularJS应用程序一起工作
我正在开发一个web应用程序,它的后端是Spring,前端是AngularJS。我需要将ApachePDFBox包含到我的PDF报告应用程序中。通过使用RequestMapping,我可以从Spring部分使其完美工作,下面是代码:让ApachePDFBox与Spring AngularJS应用程序一起工作,angularjs,spring,pdfbox,Angularjs,Spring,Pdfbox,我正在开发一个web应用程序,它的后端是Spring,前端是AngularJS。我需要将ApachePDFBox包含到我的PDF报告应用程序中。通过使用RequestMapping,我可以从Spring部分使其完美工作,下面是代码: @RequestMapping(value = "app/rest/pdf/{id}", method = RequestMethod.GET) public void getPdf(@PathVariable Long id, HttpServle
@RequestMapping(value = "app/rest/pdf/{id}",
method = RequestMethod.GET)
public void getPdf(@PathVariable Long id, HttpServletResponse response) {
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
// the method call below creates the PDF using PDFBox.. details not shown
output = printPdf(id);
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "inline; filename=\"test.pdf\"");
OutputStream os = response.getOutputStream();
output.writeTo(os);
os.flush();
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
现在,当我转到RequestMapping链接(1234是id)时,PDF为我打开。一切都好。现在,我需要从前端AngularJS应用程序获取PDF,在那里,PDF应该通过单击按钮生成。我可以使用AngularJS服务将控件发送到后端,它进入上述方法,但我不确定应该如何在AngularJS控制器中设置它,以便获取PDF。我的努力如下:
第页:
感谢您的帮助/建议
<button type="submit" ng-click="printPdf()">Print PDF Via PDBox</button>
myApp.controller('PdfController', function ($scope, Pdf) {
$scope.printPdf = function(){
// Pdf is the service and this action calls the above backend method
Pdf.get({id: $scope.report.id});
// Not sure what to do here so that I can open the PDF
}