Reputation: 21
How i can export to PDF in JasperReports 6.1?
I used this code with JasperReports API 5.2:
JasperPrint jasperPrint = JasperFillManager.fillReport(getServletContext().getRealPath(url), parametros, new JRBeanCollectionDataSource(listadoDatos));
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.exportReport();
But the code below is not working for JR API 6.1:
JasperPrint jasperPrint;
if (conConexion) {
jasperPrint = JasperFillManager.fillReport(getServletContext().getRealPath(url), parametros, conexion);
conexion.close();
} else {
jasperPrint = JasperFillManager.fillReport(getServletContext().getRealPath(url), parametros, new JRBeanCollectionDataSource(listaDatos));
}
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(nombreReporte+".pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
How I can rewrite this code?
Upvotes: 2
Views: 12221
Reputation: 22867
The answer by @LuisNeira
This is solution:
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setPermissions(PdfWriter.AllowCopy | PdfWriter.AllowPrinting);
exporter.setConfiguration(configuration);
exporter.exportReport();
Upvotes: 8