34

I'm writing a pretty simple sample project for familiarizing myself with Jasper Reports. I'd like to export a report I've configured to a PDF OutputStream, but there's no factory method for it:

InputStream template = JasperReportsApplication.class
    .getResourceAsStream("/sampleReport.xml");
JasperReport report = JasperCompileManager.compileReport(template);
JasperFillManager.fillReport(report, new HashMap<String, String>());
// nope, just chuck testa.  
//JasperExportManager.exportReportToPdfStream(report, new FileOutputStream(new File("/tmp/out.pdf")));

How can I get the PDF in an OutputStream?

Alex K
  • 22,315
  • 19
  • 108
  • 236
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411

3 Answers3

44

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.

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • 4
    If you're simply trying to output report into a file, you can use `exportReportToPdfFile` without handling outputstream by yourself – Rangi Lin Dec 20 '11 at 01:56
  • 1
    Right, but this way gives me the most flexibility; I can write the output stream to a file or across the network or anywhere I want :) – Naftuli Kay Dec 20 '11 at 02:27
  • 4
    Note that compiling is *usually* a superfluous step. Instead, software should fill out the report using the `.jasper` file, rather than re-compiling the `.jrxml` file every time. – Dave Jarvis Oct 02 '16 at 08:50
18

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();
Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
17

You can use a JRExporter to export the filled report to different streams and formats.

JRExporter exporter = null;

exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
exporter.exportReport();

Also note that there are other exporters:

exporter = new JRRtfExporter();
exporter = new JRHtmlExporter();

You can find more exporters available here: http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRExporter.html

They should all accept an OUTPUT_STREAM parameter to control the destination of the report.

stevemac
  • 2,534
  • 5
  • 27
  • 36