Luis Neira
Luis Neira

Reputation: 21

How can I export to PDF in JasperReports 6.1: alternate of using JRPdfExporter.setParameter method

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

Answers (1)

Alex K
Alex K

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

Related Questions