Export JasperReport to PDF OutputStream?

Ok, so here's how it works; JasperFillManager actually returns a JasperPrint object, so:

// get the JRXML template as a stream
InputStream template = JasperReportsApplication.class
    .getResourceAsStream("/sampleReport.xml");
// compile the report from the stream
JasperReport report = JasperCompileManager.compileReport(template);
// fill out the report into a print object, ready for export. 
JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, String>());
// export it!
File pdf = File.createTempFile("output.", ".pdf");
JasperExportManager.exportReportToPdfStream(print, new FileOutputStream(pdf));

Enjoy.


JRExporterParameter is deprecated in latest versions, This is a non deprecated solution of @stevemac answer

JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setMetadataAuthor("Petter");  //why not set some config as we like
exporter.setConfiguration(configuration);
exporter.exportReport();