2011-12-19 46 views
21

Estoy escribiendo un proyecto de muestra bastante simple para familiarizarme con los informes de Jasper. Me gustaría exportar un informe He configurado a un PDF OutputStream, pero no hay método de fábrica para ello:Exportar Jasper Informar a PDF OutputStream?

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"))); 

¿Cómo puedo obtener el PDF en un OutputStream?

Respuesta

28

Ok, así es cómo funciona; JasperFillManager realidad devuelve un objeto JasperPrint, por lo que:

// 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)); 

disfrutar.

+2

Si usted está simplemente tratando de informe de salida en un archivo, se puede usar ' exportReportToPdfFile' sin manejar outputstream por usted mismo –

+0

Correcto, pero de esta manera me da la mayor flexibilidad; Puedo escribir el flujo de salida en un archivo o en la red o en cualquier lugar que desee :) –

+1

Tenga en cuenta que compilar es * generalmente * un paso superfluo. En cambio, el software debe completar el informe con el archivo '.jasper', en lugar de volver a compilar el archivo' .jrxml' cada vez. –

16

Puede usar un JRExporter para exportar el informe completo a diferentes flujos y formatos.

JRExporter exporter = null; 

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

También tenga en cuenta que hay otros exportadores:

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

Puede encontrar más exportadores disponible aquí: http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRExporter.html

todos ellos deben aceptar un parámetro OUTPUT_STREAM para controlar el destino del informe .

+0

Mejor porque polimórfico –

+1

Después de 5 años, el JRExportParameter ha quedado obsoleto, he agregado una [respuesta] no obsoleta (http://stackoverflow.com/a/35916470/5292302) –

6

JRExporterParameter está en desuso en las versiones más recientes, esta es una no desaprobado solución de @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();