2006 IR Open Dicussion Posted August 26, 2006 Share Posted August 26, 2006 By: Chris - qoviachris Exporting Multiple Jasper Print Objects (XML) 2006-02-21 14:28 I am trying to determine how to export multiple JRXML files into a single XML stream that can later be saved to PDF, etc.. Currently I am using: JasperExportManager.exportReportToXmlStream(print, baos); Is there a way to do this where "print" can be a List of JasperPrint Objects? Any help would be appreciated. Thanks in advance. By: Aegis Thorne - aegisthorne RE: Exporting Multiple Jasper Print Objects ( 2006-02-21 17:19 Do you want to merge the reports into one big composite report ? The following code creates a new empty report and copies all of the reports(JasperPrint objects) from the 'reports' List into it. public JasperPrint getCombinedReport(List reports) { JasperPrint combinedReport = new JasperPrint(); //temp variable JasperPrint print; for(int i = 0; i < reports.size(); i++) { print = (JasperPrint) reports.get(i); copyPages(combinedReport, print); copyFonts(combinedReport, print); } copyProperties(combinedReport, print); return combinedReport; } private void copyFonts(final JasperPrint combinedReport, JasperPrint filledReport) throws JRException { List fonts = filledReport.getFontsList(); if (fonts == null) { return; } for (Iterator iter = fonts.iterator(); iter.hasNext();) { JRReportFont font = (JRReportFont) iter.next(); if (!combinedReport.getFontsMap().containsKey(font.getName())) { combinedReport.addFont(font); } } } private void copyPages(final JasperPrint combinedReport, JasperPrint filledReport) { List pages = filledReport.getPages(); if (pages == null) { return; } for (Iterator iter = pages.iterator(); iter.hasNext();) { JRPrintPage page = (JRPrintPage) iter.next(); combinedReport.addPage(page); } } /** Set the properties of the target report to the properties of the source. You might want to change this up. * * @param source * @param target */ private void copyProperties(final JasperPrint target, JasperPrint source) { target.setDefaultFont(source.getDefaultFont()); target.setName(source.getName()); target.setOrientation(source.getOrientation()); target.setPageHeight(source.getPageHeight()); target.setPageWidth(source.getPageWidth()); } Hope this helps. AT By: Aegis Thorne - aegisthorne RE: Exporting Multiple Jasper Print Objects ( 2006-02-22 07:31 or you can export a list using: "The JRExporterParameter.JASPER_PRINT_LIST can then be set on the JRPdfExporter (or whatever other exporter). If you put your two reports in a java.util.List and pass them as the value of the JRExporterParameter.JASPER_PRINT_LIST you are done. " - russelldb123 This is from the post on printing two reports at once. I haven't tried this method personally but it sounds simple enough. By: Chris - qoviachris RE: Exporting Multiple Jasper Print Objects ( 2006-02-22 08:04 Can this be used in conjuntion with JasperExportManager.exportReportToXmlStream(print, baos); Currently I stream the reports to a byte array output stream and then we use them elsewhere and convert them as needed. By: Aegis Thorne - aegisthorne RE: Exporting Multiple Jasper Print Objects ( 2006-02-22 08:08 Well, the JRPrint creation method will definately work, since the print is one object. The other method will also work with a few more lines of code. JasperExportManager.exportReportToXmlStream(print, baos) is a convenience method for the following code: JRXmlExporter exporter = new JRXmlExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); exporter.exportReport(); So, set the JRExporterParameter.JASPER_PRINT_LIST instead of JASPER_PRINT and that should work. By: Chris - qoviachris RE: Exporting Multiple Jasper Print Objects ( 2006-02-22 08:10 Awesome, thanks so much I will look into using this. I guess I just didn't realize that the JasperExportManager was a convience method. By: Aegis Thorne - aegisthorne RE: Exporting Multiple Jasper Print Objects ( 2006-02-22 08:17 Good luck :) By: Chris - qoviachris RE: Exporting Multiple Jasper Print Objects ( 2006-02-23 13:15 Just curious if someone knows ... What I am trying to do is run the same JRXML file multiple times with the only difference being that the parameter map contains different values but the same keys. From what I can see so far, it appears that Jasper uses the 1st instance of a property it finds. I had been under the assumption that a JasperPrint object that had been filled could be merged with any number of JasperPrint Objects even if they contained identical data. Basically what I am looking for is a way to merge multiple Jasper Print Objects into the same report where currently a single JasperPrint object represents a single page. By: Aegis Thorne - aegisthorne RE: Exporting Multiple Jasper Print Objects ( 2006-02-23 13:24 I'm changing the datasource for each of my reports; the same keys are present. It works fine for me. Can you post some code ? By: Chris - qoviachris RE: Exporting Multiple Jasper Print Objects ( 2006-02-23 13:32 This is the current code I am using: private static InputStream createReport (final HashMap[] parameterMaps) { final ArrayList printList = new ArrayList (); ByteArrayInputStream bais = null; for (int mapIndex = 0; mapIndex < parameterMaps.length; mapIndex ++) { JasperReport report = null; JasperPrint print = null; final HashMap reportMap = parameterMaps[mapIndex]; try { final URL reportURL = ReportFactory.class.getResource((String) reportMap.get("reportURL")); final Object obj = JRLoader.loadObject(reportURL); if (obj instanceof JasperReport) { report = (JasperReport) obj; } } catch (JRException jreEx) {} if (report != null) { try { print = JasperFillManager.fillReport(report, reportMap, new JREmptyDataSource()); printList.add(print); } catch (JRException jreEx) {} } } if (!printList.isEmpty()) { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final JRXmlExporter exporter = new JRXmlExporter (); exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, printList); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); exporter.exportReport(); bais = new ByteArrayInputStream(baos.toByteArray()); } catch (JRException jreEx) {} } return bais; } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now