Wednesday, May 9, 2012

PDF file download dialog with JAX-RS

Below I will show how to display PDF file download dialog to the user using REST JAX-RS.

My implementation below will NOT create PDF file in user's directory until the user choose "save as" in the dialog box. To achieve this, I used ByteArrayInputStream class.

 

ByteArrayOutputStream baos = new ByteArrayOutputStream();

//This is just PDF generation API
JasperExportManager.exportReportToPdfStream(jasperPrint, baos);
                
byte[] entity = baos.toByteArray();
                
return Response.ok(entity)
        .header("Content-Disposition", "attachment; filename=MyAwesomeJasperReportDownload.pdf")
        .type("application/pdf")
        .build();

No comments:

Post a Comment