Export JasperReport to PDF OutputStream?

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()); // nope, just chuck testa. //JasperExportManager.exportReportToPdfStream(report, new FileOutputStream(new File("/tmp/out.pdf"))); 
How can I get the PDF in an OutputStream ? 22.7k 19 19 gold badges 113 113 silver badges 242 242 bronze badges asked Dec 19, 2011 at 23:27 Naftuli Kay Naftuli Kay 90.7k 102 102 gold badges 278 278 silver badges 423 423 bronze badges

3 Answers 3

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()); // export it! File pdf = File.createTempFile("output.", ".pdf"); JasperExportManager.exportReportToPdfStream(print, new FileOutputStream(pdf)); 
answered Dec 20, 2011 at 1:48 Naftuli Kay Naftuli Kay 90.7k 102 102 gold badges 278 278 silver badges 423 423 bronze badges

If you're simply trying to output report into a file, you can use exportReportToPdfFile without handling outputstream by yourself

Commented Dec 20, 2011 at 1:56

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 :)

Commented Dec 20, 2011 at 2:27

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.