How to collate multiple jrxml jasper reports into a one single pdf output file

24,821

Solution 1

You can take advantage of exporting the whole jasperprint list:

List jpList = new ArrayList();
jpList.add(JRLoader.loadObjectFromFile("build/reports/Report1.jrprint")); 
...
JRExporter exporter = new JRPdfExporter(); 
exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, jpList); 
exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, stream); 
exporter.exportReport();

Solution 2

This answer is to help users with JASPER REPORT VERSION >5.6 (latest versions), hence remove the deprecated code.

Since jasper-report 5.6 JRPdfExporterParameter.JASPER_PRINT_LIST is deprecated the current code of Wojtek Owczarczyk answer is:

List<JasperPrint> jpList = new ArrayList<>();
//add your JasperPrint's from loading jrprint or more 
//commonly filling report with JasperFillManager.fillReport 

JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(jpList)); //Set as export input
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(stream)); //Set output stream
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
//set your configuration
exporter.setConfiguration(configuration);
exporter.exportReport();

Solution 3

Page numbers without itext...

private void drawPageNumbers(List<JasperPrint> listJasperPrint, int totalPages) throws JRException {

    int pageCount = 0;
    int posY = 0;
    int posX = 0;

    for (JasperPrint jasperPrint : listJasperPrint) {

        if (jasperPrint.getOrientation() == JRReport.ORIENTATION_PORTRAIT) {
            posY = 805;
            posX = 472;
        }

        if (jasperPrint.getOrientation() == JRReport.ORIENTATION_LANDSCAPE) {
            posY = 558;
            posX = 717;
        }

        for (Object obj : jasperPrint.getPages()) {

            pageCount++;
            JRPrintPage page = (JRPrintPage) obj;

            JRPrintText textTotalPages = new JRTemplatePrintText(new JRTemplateText(
                    jasperPrint.getOrigins()[0], jasperPrint.getDefaultStyleProvider()));
            textTotalPages.setX(posX + 54);
            textTotalPages.setY(posY);
            textTotalPages.setWidth(40);
            textTotalPages.setHeight(16);
            textTotalPages.setText(" " + totalPages);
            page.addElement(textTotalPages);

            JRPrintText textPageNumber = new JRTemplatePrintText(new JRTemplateText(
                    jasperPrint.getOrigins()[0], jasperPrint.getDefaultStyleProvider()));
            textPageNumber.setX(posX);
            textPageNumber.setY(posY);
            textPageNumber.setWidth(80);
            textPageNumber.setHeight(16);
            textPageNumber.setText("Página " + pageCount + " de");
            page.addElement(textPageNumber);
        }
    }

    return;
}
Share:
24,821

Related videos on Youtube

Vicky
Author by

Vicky

A software developer with zeal to learn new things all the time!!

Updated on April 03, 2020

Comments

  • Vicky
    Vicky about 4 years

    I have to prepare reports using five different sql queries. Each query will give out one report table.

    So I wrote 5 jrxml files each corresponding to one of the above query with their own headings, title settings, footers, pagenumbers, etc.

    Now, I am able to compile, print and export each of the above jrxmls into 5 different pdfs.

    However, client wants all the reports collated into one single pdf. That is in the final pdf, first four pages will be say report one, next five pages report two, then report three and so on and so forth.

    1) How to achieve this ?

    2) Each report has page number as 1/4, 2/4, 3/4 etc. Where the second part i.e. the complete page number is evaluated with evaluation time as report. So when I will collate all reports in single pdf (if possible), will it also be possible to re-number the pages in justification to final pdf ?

    Based on the answer below, I did the following in my java class and it works:

     try
                {
                JasperReport jreport1 = JasperCompileManager.compileReport(input1);
                JasperPrint jprint1 = JasperFillManager.fillReport(jreport1, new HashMap(), new JREmptyDataSource());
                //JasperExportManager.exportReportToPdfFile(jprint, "/home/ashutosh/Desktop/desktop/nikunj/JasperTestApp/output/mytest.pdf");
    
                JasperReport jreport2 = JasperCompileManager.compileReport(input2);
                JasperPrint jprint2 = JasperFillManager.fillReport(jreport2, new HashMap(), new JREmptyDataSource());
    
                JasperReport jreport3 = JasperCompileManager.compileReport(input3);
                JasperPrint jprint3 = JasperFillManager.fillReport(jreport3, new HashMap(), new JREmptyDataSource());
    
                List<JasperPrint> jprintlist = new ArrayList<JasperPrint>();
    
                jprintlist.add(jprint1);
                jprintlist.add(jprint2);
                jprintlist.add(jprint3);
    
                JRExporter exporter = new JRPdfExporter();
                exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, jprintlist);
    
                OutputStream output = new FileOutputStream(new File("/home/ashutosh/Desktop/desktop/nikunj/JasperTestApp/output/mytestbatch.pdf"));
    
                exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, output);
                exporter.exportReport();
    
                }catch(Exception e)
                {
                    e.printStackTrace();
                }
    

    Above: input1, input2, input3 are string paths to input jrxmls

    Where my JRXML files just print three messages: Hello World 1, Hello World 2, Hello World 3.

     <?xml version="1.0"?>
    <!DOCTYPE jasperReport
      PUBLIC "-//JasperReports//DTD Report Design//EN"
      "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
    
    <jasperReport name="Simple_Report">
     <detail>
        <band height="20">
          <staticText>
            <reportElement x="180" y="0" width="200" height="20"/>
            <text><![CDATA[Hello World One!]]></text>
          </staticText>
        </band>
      </detail>
    </jasperReport>
    

    Thanks for reading!

  • Alex K
    Alex K over 12 years
    @NikunjChauhan The sample is here. The folder demo\samples\batchexport from JasperReports distribution package.
  • Vicky
    Vicky over 12 years
    @Wojtek O. : How to define jpList ?? Is it a simple List of jprint objects ?
  • Vicky
    Vicky over 12 years
    @AlexK: Can you please re paste the link... its not there.. thanks!
  • Vicky
    Vicky over 12 years
    @AlexK: Please see my updates.. there is still something missing!
  • HybrisHelp
    HybrisHelp almost 11 years
    @Wojtek O. Ya , I can print all list of JasperReports in single pdf , But for each reports it go for new page in PDF So how can i prevent this .. I want to print this all report in single page of PDF bcz it not contain more data
  • Funky coder
    Funky coder almost 11 years
    @ankit337 then there is no easier way of doing that other than just adapting you report template to hold all the data necessary.
  • HybrisHelp
    HybrisHelp almost 11 years
    @Wojtek O. Thanks for responding ,But i have 5 or 6 table (Number of table is dynamic ) and i want to get data(2 to 4 row from each table ) from all table in one report .,, so can you suggest any way ?
  • Petter Friberg
    Petter Friberg almost 8 years
    The code in answer is deprecated since version 5.6, I have added an example of non deprecated code.
  • Topera
    Topera over 7 years
    Note: the export with best quality (JRGraphics2DExporter) does not work with many jasperPrints. Ref: source code of JRGraphics2DExporter
  • himanshu sharma
    himanshu sharma over 4 years
    setParameter is deprecated.

Related