How to attach a .pdf file to a generated Jasper report

4

How to attach a .pdf file to a generated Jasper report

Example: Say I have 2 jasper files, a.jasper and b.jasper. Now these 2 jasper files are exported as a pdf report

in one single pdf file. tht is Page 1 of the pdf will have a.jasper and page 2 will have b.jasper. Now this I have acheived this by using

JRExporter and passing a.jasper and b.jasper as a list. But there is

one more pdf file itself  say c.pdf ( not a jasper file, as this is provided by our customer and cannot be created as a japer).

Now I want this c.pdf as the 3rd page of the exported pdf report...

 

AnnMary.Thomas's picture
Joined: Jun 18 2014 - 4:41am
Last seen: 2 years 6 months ago

Would also like to see this feature

 

j.t.casperson - 3 years 3 months ago

Somebody knows how can it ?

 

Thanks.

dcdacd - 2 years 8 months ago

3 Answers:

2

This is how I did in Java. Hope this helps        

   ByteArrayInputStream bs = new ByteArrayInputStream(output.toByteArray()); // output refers to your Jasper report
            Document document = new Document();
            PdfCopy copy = new PdfCopy(document, output);
            document.open();

            PdfReader jasperReader; //Reader for Jasper Report

          PdfReader  nonJasperReader; // Reader for the Pdf to be attached (not .jasper file)
            jasperReader= new PdfReader(bs);
            attachmentReader = new PdfReader(params.get("att").toString());
            numberOfPages = jasperReader.getNumberOfPages();
            for (int page = 0; page < numberOfPages;) {
                copy.addPage(copy.getImportedPage(jasperReader, ++page));
            }
            numberOfPages = nonJasperReader.getNumberOfPages();
            for (int page = 0; page < numberOfPages;) {
                copy.addPage(copy.getImportedPage(nonJasperReader, ++page));
            }
            copy.freeReader(jasperReader);
            jasperReader.close();
            document.close();
            }

AnnMary.Thomas's picture
Joined: Jun 18 2014 - 4:41am
Last seen: 2 years 6 months ago
0

Thanks Thomas.

However when i see the code, i see only these lines that contribute to the output for my jasper report.

It creates a file path for the masterTemplate and then compiles it and fill and then finally export the jasperPrint object, so are you talking about the JasperPrint object ?

*****************************************************

String masterTemplate = ProductionUtils.createFilePath( config.getSystemProperty( SystemProperty.PATH_KEY ), JRXML_FILE );

jasperReport = JasperCompileManager.compileReport( masterTemplate );

jasperPrint = JasperFillManager.fillReport( jasperReport, parameters, new JREmptyDataSource() );

JasperExportManager.exportReportToPdfFile( jasperPrint, generateLocalFilePath() );

*****************************************************

rohitahuja123's picture
Joined: Aug 23 2016 - 9:32am
Last seen: 2 years 7 months ago
0

I called the

JasperExportManager.exportReportToPdf(jasperPrint) - method and it returned me byteArray.</pre>

1. But what is the output in PdfCopy instance  --> its  an outStream? :-

PdfCopy copy = new PdfCopy(document, output);    ?????????

2 . And how will i call my PdfReader for the attachmentReader as PdfReader accepts ByteArrayInputStream?

3. And after getting the 'copy', how we will pass the exportReportToFile method with a jasperPrint object?

 

Thanks.

rohitahuja123's picture
Joined: Aug 23 2016 - 9:32am
Last seen: 2 years 7 months ago
Feedback