How can I use JasperFillManager.fillReportToStream?

jasper-reports, java

Solution

Calling `fillReportToStream` still produces a `JasperPrint` object, but it writes it directly to an output stream instead of passing it back to you. The `JasperFillManager` cannot output the report in any other format, so the export step cannot be skipped. To generate a PDF, you would still need to use `JRPdfExporter` and read the filled report from wherever the output stream wrote it to.

I think the problem here is your file virtualizer. You mention that "it will spin for over a day, regardless of setting the file virtualizer" but the virtualizer will actually make the process take longer. It is a basic time/memory tradeoff to avoid out of memory errors, but will make the filling much slower. In a benchmark I've seen, adding a file virtualizer quadrupled the filling time!

If you really need the virtualiser, try increasing the maxSize parameter you are passing. 2 seems awfully low. You could also try switching to a `JRSwapFileVirtualizer` as I've heard they have much better performance.

Problem

I have a simple Java program that takes a .jrxml file, compiles it, then fills it. The data for the report is supplied in an XML file. After the fill is done, the data is exported to PDF. ``` // Parse input document Document document = JRXmlUtils.parse(new File(xmlFile)); // Set it as the data source in the parameters parameters.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document); // Create and set the virtualizer JRFileVirtualizer virtualizer = new JRFileVirtualizer(2, "/tmp"); virtualizer.setReadOnly(true); parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer); // Fill the report String jasperFile = designFile.replaceAll(".jrxml",".jasper"); print = JasperFillManager.fillReport(jasperFile, parameters); // Export the report to PDF ArrayList<JasperPrint> jasperPrints = new ArrayList<JasperPrint>(); jasperPrints.add(print); JRPdfExporter exp = new JRPdfExporter(); exp.setParameter (JRExporterParameter.JASPER_PRINT_LIST, jasperPrints); exp.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName ); exp.exportReport(); ``` My confusion is about the export That fill line above works great for small reports, but once I get an XML source file approaching 1/2 MB, it will spin for over a day, regardless of setting the file virtualizer (which I do). I see there is another method called `fillReportToSteam`. My confusion is that with `fillReport`, I have to do an extra step to export to PDF. What sort of stream is `fillReportToStream` writing to, and how do I specify that? Will `fillReportToStream` write to a PDF file? I haven't been able to find any examples. I was hoping I could take advantage of the stream so that I could measure the progress and get these PDFs to complete in a normal span of time.

Original source