Export JasperReport to PDF OutputStream?

后端 未结 3 1584
萌比男神i
萌比男神i 2020-12-02 10:34

I\'m writing a pretty simple sample project for familiarizing myself with Jasper Reports. I\'d like to export a report I\'ve configured to a PDF OutputStream, b

相关标签:
3条回答
  • 2020-12-02 11:06

    Ok, so here's how it works; JasperFillManager actually returns a JasperPrint object, so:

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

    Enjoy.

    0 讨论(0)
  • 2020-12-02 11:09

    You can use a JRExporter to export the filled report to different streams and formats.

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

    Also note that there are other exporters:

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

    You can find more exporters available here: http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRExporter.html

    They should all accept an OUTPUT_STREAM parameter to control the destination of the report.

    0 讨论(0)
  • 2020-12-02 11:12

    JRExporterParameter is deprecated in latest versions, This is a non deprecated solution of @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();
    
    0 讨论(0)
提交回复
热议问题