1 2 Previous Next 16 Replies Latest reply on Jul 30, 2012 4:30 PM by Murat Arabacioglu

    JasperReports, iReport & PDFs OH MY!

    ginni machamer Newbie

      Newbie here... Don't hurt me please! :)


      I am trying to work with iReport (new to it) for my Seam/JSF web app. I have a page that displays a table of data supplied by a backing bean. The data is contained in a List and displayed in a RichTable.


      I need a link on the page to produce the same table of data into a PDF, nicely formatted.  SO, I am looking at JasperReports.


      In iReport, I don't know how to point to the backing bean List attribute as my data source.  Is that even possible?  Is there a baby-steps guide on how to link these things together?


      Thanks in advance!!


      Ginni


        • 1. Re: JasperReports, iReport & PDFs OH MY!
          Leo van den berg Master

          Hi,


          have a look at Primefaces. It includes a simple (but very handy) export function from a Datatable to PDF (and XML and Excel).


          Primefaces



          Leo


          P.S. The Primefaces page taks about JSF 2.0, but they also have libraries for the 1.X version.

          • 2. Re: JasperReports, iReport & PDFs OH MY!
            ginni machamer Newbie

            Thanks Leo.


            Unfortunately, this is an app that's already designed, using Richfaces. I don't have authority to redesign all the pages with Primefaces. I need to get JasperReports working for this feature.  The page with the data table is just a simple example, there are others required within the app.


            Ginni

            • 3. Re: JasperReports, iReport & PDFs OH MY!
              Leo van den berg Master

              Hi,


              well a pity because it's really fast for simple things.


              I've included an example of a PDF generator based on iText and a very handy Library Dynamicreport.




              public void createPDFReport(List<InfraWebLogentry> resultList){
                        
                        ServletContext x = (ServletContext)facesContext.getExternalContext().getContext();
                        String realpath = x.getRealPath("/");
                        
                        try {
                        FastReportBuilder drb = new FastReportBuilder();
                        
                      DynamicReport dr =      
                           drb.addColumn("ID", "logId", Long.class.getName(),15)
                                .addColumn("Description", "description", String.class.getName(),30)
                                .addColumn("Remarks", "remarks", String.class.getName(),55)
                                .setTitle("Found remarks")
                              .setSubtitle("This report was generated at " + new Date())
                              
                              .setPrintBackgroundOnOddRows(true)                      
                              .setUseFullPageWidth(true)
                              
                              .addImageBanner(realpath + "images/logo.jpg", new Integer(30), new Integer(30), ImageBanner.ALIGN_CENTER)
                              .addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT)
                              .build();  
                      
                      JRDataSource ds = new JRBeanCollectionDataSource(resultList);   
                      JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
              
                      // Now we can use Seam's DocumentStore
                      byte[] pdf = JasperExportManager.exportReportToPdf(jp);
                      DocumentData data = new ByteArrayDocumentData("report", new DocumentData.DocumentType("pdf", "application/pdf"), pdf);
                      DocumentStore documentStore = DocumentStore.instance();
                      
                      String docId =  documentStore.newId();
                      documentStore.saveData(docId, data);
                      
                      String documentUrl = documentStore.preferredUrlForContent(data.getBaseName(), data.getDocumentType().getExtension(), docId);
              
                      facesContext.getExternalContext().redirect(manager.encodeConversationId(documentUrl, null ));
                      // Don't process the reposne
                      facesContext.responseComplete();
                      
                        } catch (Exception e) {
                             e.printStackTrace();
                        } 
              
                   }




              Hopefully this helps you a bit further.


              Leo

              • 5. Re: JasperReports, iReport & PDFs OH MY!
                Aram Nigiyan Newbie

                Don't try jasperreport-jsf.
                I used it and isn't flexible and non developer-friendly :).
                Finally I came up to jasperreports.jar and smth like Leo van den Berg has posted.

                • 6. Re: JasperReports, iReport & PDFs OH MY!
                  Leo van den berg Master

                  Hi,


                  I came to the same conclusion. The jasperreports-jsf library needs a lot of additional work to make it useable. At the moment I am making a Seam component which reads and compiles a jasper report file made in iReport and send it to an email handler using an Asynchronous method on another bean. In this way I have a completely independent email handler for reports. My next step is using the DB to store the reports (via a upload page) which makes stuff more flexible. My approach is more generic so I think it should be possible to use it also for Birt reports.


                  If you're interested I can keep you informed.


                  Leo

                  • 7. Re: JasperReports, iReport & PDFs OH MY!
                    ginni machamer Newbie

                    I feel like a dunce, but all of this is new to me.
                    I tried using iReport, but for the life of me it will not read data from my Java bean.  I have a backing bean that contains the data for the page. It's called AuthorizationFormsAllLevelsManager wired into Seam with @ name authorizationFormsAllLevelsListManager.  The xhtml page references authorizationForms (List of AuthorizationFormsAllLevelsRowObject) in a rich table element from that bean.  That is my data source.  How do I expose it to iReport? Or, barring that, how do I just create a report in iReport and then edit it to put in the attributes from the bean List forcefully? ;)


                    Thanks,
                    Ginni


                    PS I tried JBoss Seam PDF, which will do the job, but requires too much duplicated code for our taste since it will not act on RichTable etc and requires its own p:table...


                    • 8. Re: JasperReports, iReport & PDFs OH MY!
                      ginni machamer Newbie

                      PPS Yes, please keep me informed. I can learn a lot from you! ginni at aero.org

                      • 9. Re: JasperReports, iReport & PDFs OH MY!
                        ginni machamer Newbie

                        public void createPDFReport(List<InfraWebLogentry> resultList){
                                  
                                  ServletContext x = (ServletContext)facesContext.getExternalContext().getContext();
                                  String realpath = x.getRealPath("/");
                                  
                                  try {
                                  FastReportBuilder drb = new FastReportBuilder();
                                  
                                DynamicReport dr =      
                                     drb.addColumn("ID", "logId", Long.class.getName(),15)
                                          .addColumn("Description", "description", String.class.getName(),30)
                                          .addColumn("Remarks", "remarks", String.class.getName(),55)
                                          .setTitle("Found remarks")
                                        .setSubtitle("This report was generated at " + new Date())
                                        
                                        .setPrintBackgroundOnOddRows(true)                      
                                        .setUseFullPageWidth(true)
                                        
                                        .addImageBanner(realpath + "images/logo.jpg", new Integer(30), new Integer(30), ImageBanner.ALIGN_CENTER)
                                        .addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_HEADER, AutoText.ALIGNMENT_LEFT)
                                        .build();  
                                
                                JRDataSource ds = new JRBeanCollectionDataSource(resultList);   
                                JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
                        
                                // Now we can use Seam's DocumentStore
                                byte[] pdf = JasperExportManager.exportReportToPdf(jp);
                                DocumentData data = new ByteArrayDocumentData("report", new DocumentData.DocumentType("pdf", "application/pdf"), pdf);
                                DocumentStore documentStore = DocumentStore.instance();
                                
                                String docId =  documentStore.newId();
                                documentStore.saveData(docId, data);
                                
                                String documentUrl = documentStore.preferredUrlForContent(data.getBaseName(), data.getDocumentType().getExtension(), docId);
                        
                                facesContext.getExternalContext().redirect(manager.encodeConversationId(documentUrl, null ));
                                // Don't process the reposne
                                facesContext.responseComplete();
                                
                                  } catch (Exception e) {
                                       e.printStackTrace();
                                  } 
                        
                             }




                        I see in this code that the parameter resultList isn't used, but assume it could be in the place of the drb.addColumn... with some iteration through the list items to do the same?  Am I reading that right?  I am still learning java, so I am just not sure.


                        Thanks for your help!!


                        Ginni

                        • 10. Re: JasperReports, iReport & PDFs OH MY!
                          ginni machamer Newbie

                          One more quick question - where does manager come from?

                          • 11. Re: JasperReports, iReport & PDFs OH MY!
                            ginni machamer Newbie

                            Okay, disregard, I see resultList is used.
                            If my objects in my authorizationForms list are not simple, as in they have attributes that are other objects... how do I refer to those attributes? Within my page, I do so like:


                            #{record.authorizationForm.formTitle}



                            I am guessing that in the line:


                            .addColumn("Description", "description", String.class.getName(),30)



                            description is an attribute of the InfraWebLogentry class.


                            So, would I put something like authorizationForm.formTitle there?
                            Thanks!

                            • 12. Re: JasperReports, iReport & PDFs OH MY!
                              Leo van den berg Master

                              Hi,


                              Manager is a Seam component:



                              org.jboss.seam.core.manager

                                  Internal component for Seam page and conversation context management. Always installed.

                                        org.jboss.seam.core.manager.conversationTimeout — the conversation context timeout in milliseconds.

                              Additionally I will try to finish a component this week to store JasperReport (jrxml) files in the DB for further processing. I prefer using iReport to make these files, because changing reports is very labourous. The program has some really funny behaviour, but with some practising you can work with it. Two impoprtant reminders:


                              - You must adapt the classpath of iReport to be able to use your entities (under properties),
                              - It additionally needs a kind of Helper class with a static method to access a Collection of your entities. This is needed if you use the Bean collection datasource. This class is NOT needed for further processing, only for design.


                              Hopefully this helps a bit with the Reproting struggle....


                              Leo

                              • 13. Re: JasperReports, iReport & PDFs OH MY!
                                Murat Arabacioglu Newbie

                                Hi Leo, have you finished the component you mentioned in your post? What is the current status of the question? Is there a step by step guide how to integrate Jasperrports to an existing (seam-gen generated) project?

                                • 14. Re: JasperReports, iReport & PDFs OH MY!
                                  Leo van den berg Master

                                  Hi Murat,

                                   

                                  Yes, I've made a Seam-2 component to load different types of reports (including JasperReport files). I also made a DB-loader which enables you to store the Report in the DB and retrieve it on the fly.

                                   

                                  To be honest, At the end I used the included Seam renderer for Templates and made and extended it with a DB-Resouice loader. The templates are not really "dummy-user-friendly", but with a bit of practice yoy can create beautiful multi-table reports which is really a lot of work with Jasper (you need subreports to do the trick). The JBoss tools/Eclipse xhtml editor gives you a nice WYSIWYG and helps you with the templates.

                                   

                                  Pity is the sudden move of the Seam dev.team to Seam-3 which let's you out in the cold with the "obsolete" Seam-2 stuff. If you are starting new development just use CDI.

                                   

                                  Leo

                                   

                                   

                                   

                                  .

                                  1 2 Previous Next