Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 696
Vicky Avatar asked Dec 19 '11 16:12

Vicky


People also ask

What is the difference between Jasper and Jrxml?

jrxml is a human readable XML file that contains the report template i.e. report structure and its formatting rules. . jasper is the compiled report template i.e. compiled . jrxml file.

How do I embed a PDF in Jasper?

Suggested approach. TIBCO JasperReports® library does not provide an option to embed an external PDF file into a report. The closest option available out of the box is to add a link to an external PDF file to your report.


2 Answers

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();
like image 197
Funky coder Avatar answered Sep 20 '22 20:09

Funky coder


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();
like image 20
Petter Friberg Avatar answered Sep 22 '22 20:09

Petter Friberg