如何在JasperReports 6.1中导出为PDF:使用JRPdfExporter.setParameter方法的替代方法

Lui*_*ira 1 java jasper-reports export-to-pdf

如何在JasperReports 6.1中导出为PDF?

我将此代码与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();
Run Code Online (Sandbox Code Playgroud)

但是以下代码不适用于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();
Run Code Online (Sandbox Code Playgroud)

我该如何重写此代码?

Ale*_*x K 6

@LuisNeira的答案

这是解决方案:

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();
Run Code Online (Sandbox Code Playgroud)