jasper-reports

Export to pdf

Remarks#

To render fonts correctly in pdf font-extensions should always be used (in classpath)

With IDE (Integrated development environment)

JasperSoft Studio

In Preview, run report by clicking green arrow, if no errors the export menu will be enable, click the export button (disk image) and select “Export As Pdf” Export as pdf

With Java

To export a you need to fill the report to get the JasperPrint object.

Export single JasperPrint (single jrxml) to file

// 1. Create exporter instance
JRPdfExporter exporter = new JRPdfExporter();

// 2. Set exporter input document
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));

// 3. Set file path for exporter output
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("/path/filename.pdf"));

// 4. Create configuration instance
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();

// 5. Associate configuration with exporter
exporter.setConfiguration(configuration);

// 6. Fill export and write to file path
exporter.exportReport();

Export multiple JasperPrint’s (multiple jrxml) to single file

Only the first steps differ from the previous set:

List<JasperPrint> jasperPrintList = new ArrayList<>();
jasperPrintList.add(jasperPrint1);
jasperPrintList.add(jasperPrint2);

JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));

The remaining steps are the same:

exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("/path/filename.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();

See SimplePdfExporterConfiguration API for configuration details.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow