rest - AngularJS Display PDF (byte[]) received from Spring @RestController -


To display my requirement either (new tab) / download / embed / post

I do not want to return a unique identifier for the PDF generated from the server and instead to use a $ window service to open a new window, the url pointing to server-side endpoint which is PDF Based on the unique identifier Because I need to create a PDF on the fly (there is no storage in the file system).

There is only one question on this but it is not working for me.

My Controller

  angular.module ('EvaluationResultsModule'). Controller ('CA_EvaluationResultsCtrl', ['$ scope', 'EvaluationResultsService', '$ SCE', function ($ scope, EvaluationResultsService, $ SCE) {$ scope.showPDF = function () {$ scope.result = CA_EvaluationResultsService.getEvalataionResultPDF ( $ Scope.evaluationResults); $ scope.result $ promise.then (function (data) {var file = new blob ([data], {type: 'application / pdf'}); var fileURL = URL.createObjectURL (file ); $ Scope.pdfContent = $ sce.trustAsResourceUrl (fileURL);})}}}]); Factory ('EvaluationResultsService', Function ($ resource)  

My service

  angular.module ('EvaluationResultsModule'). {Return $ resource ('./ api / ca / ​​evaluationResults /: dest', {}, {getEvalataionResultPDF: {method: 'GET', Params: {dest: "getPDF"}, response type: 'arraybuffer',}});} ); Public Controller Method   
  @RequestMapping (value = "/ getPDF", method = RequestMethod.GET) public byte [] GetEvalataionResultPDF () {ByteAutputputBas = new byteOnoutStream ( ); // Jasper Map & lt; String, Object & gt; Model = new hashmop & lt; String, Object & gt; (); & Lt; Users & gt; UsersList = null; // population from the service layer; JRBeanCollectionDataSource beanColDataSource = New JRBeanCollectionDataSource (usersList); JasperPrint jasperPrint = jasperPrint = JasperFillManager.fillReport (.get.getClass () getClassLoader () getResourceAsStream ("A4.jasper"), model, beanColDataSource.); JasperExportManager.exportReportToPdfStream (jasperPrint, baos); Return baos.toByteArray (); } Aararaffr, position 200, header: function, Config: Object, statusText: "OK"} config: Objectdata: ArrayBufferbyteLength: (...) __ proto__: ArrayBufferbyteLength: [Exception: typos Method ArrayBuffer.prototype.byteLength But the incompatible receiver is called # from & lt; ArrayBuffer & gt;] byteLength meets function Baitleenda () ([native code]} Manufacturer: function Arebfr () {[native code}} piece function slice () {[native code]} __ Prototo ___: Objektheders: function ( Name) {resource: Resourcestatus: 200statusText: "OK" __Pooto__: object  

I Code, and it works for me:

Rest controller:

  @RequestMapping (value = "/ API / report / PDF", method = RequestMethod .GET) @Timed Public @ResponseBody By [] GetOpenedEventsInPdf (HttpServletResponse response) {Response.setHeader (the "Content Disipson", "in line; filename = file.pdf"); response.setContentType ( "application / pdf"); // backend your custom service byte Download file bytearray [] file = jasperReportsService.getOpenedEventsReport (ReportFormat.PDF); return file;}  

JS / Angular Controller;

  $ scope. GetPdf = function {$ http.get ('/ api / reports / pdf', {responseType: 'Arraybuffer'}) .success (data) {var file = new blob ([data], {type: ' Application / pdf '}); Var fileURL = URL.createObjectURL (file); Window.open (fileURL));}}}}  

HTML fragment:

  & lt; One ng-click = "getPdf ()" & gt; PDF image & lt; / A & gt;  

Comments