Jump to content
Changes to the Jaspersoft community edition download ×

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


AnnMary.Thomas

Recommended Posts

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...

 

Link to comment
Share on other sites

  • 1 year later...
  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

  • 6 months later...

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();
            }

Link to comment
Share on other sites

  • 3 weeks later...

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() );

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...