Search code examples
struts2java-ee-6birt

call birt report from action class


I am developing a project in Java EE Struts 2 and Hibernate for airline reservations. Now my all work is done and I have to generate a ticket. Instead of generating a simple JSP or HTML ticket, I want to generate a downloadable report (like Crystal Reports in Java). I have my entire ticket info in a session that (as on internet) I can get on BIRT report using script.

I am totally new to BIRT and wanted to know how I can generate a BIRT report or maybe call its execution engine from one of my action classes. Any ready example will be a great help.


Solution

  • I guess you're trying to send PDF ticket to customers. Please create your template and pass parameters using these lines:

    ReportAdminServiceRemote  birtAdmService = (ReportAdminServiceRemote)MXServer.getMXServer().lookup(“BIRTREPORT”);
    byte[] abyte0 = birtAdmService .runReport(userInfo, reportName, appName, parameterData, filename, “pdf”);
    

    Once you have the bytes generated you can do this way:

    public String actionDownload() throws Exception{
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Content-Disposition","attachment; filename=\"" + example.pdf+ "\"");
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();   
        ByteArrayInputStream bis = new ByteArrayInputStream(abyte0);
        inputStream = bis;
        return SUCCESS;
    }
    

    All the credits goes to authors on these pages:

    http://www.maximonews.com/?p=65

    http://www.coderanch.com/t/432713/Struts/Struts-Files-DownLoad-Streaming-as