1

I am currently using the following code inside a Spring controller which results in the PDF file being downloaded :

@RequestMapping(value = {"/testReport"}, method = RequestMethod.GET)
public void testReport(ModelMap model, HttpServletResponse response) throws IOException, JRException 
{
    Resource resource = new ClassPathResource("reports/testReport.jasper");
    InputStream jasperStream = resource.getInputStream();

    Map<String, Object> params = new HashMap<>();
    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

    response.setContentType("application/x-pdf");
    response.setHeader("Content-disposition", "inline; filename=testReport.pdf");

    final OutputStream outStream = response.getOutputStream();
    JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
}

I am looking for a way to not download the PDF file but to instead open it in a new browser tab. This is the HTML code that calls this method :

<a href="/reports/testReport" target="_blank">Test</a>

I'm pretty sure this has to do with the last 2 lines in my controller method but i cannot find any documentation on how to direct the PDF to a new tab instead of a file download using Jasper.

Any help would be appreciated!

Thanks!

******EDIT******

This is not a duplicate post, i found the solution and will post it in an answer.

Martin
  • 1,265
  • 2
  • 17
  • 48

0 Answers0