Dynamic Jasper Report

Jasper Jar Download

package com.abcprocure.dynamicjasper.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.ReflectiveReportBuilder;

@WebServlet(name = "showReport", urlPatterns = {"/showReport"})
public class ShowReport extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        Session session = null;
        try {
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            session = sessionFactory.openSession();
            session.beginTransaction();

            List list = session.createQuery("from Employee").list();
            //List list = session.createQuery("select sm.studentStd, sm.studentRollno, sm.studentFirstname, sm.studentLastname, sd.studentGender, sd.studentPercentage, sd.studentContact, sd.studentAddress from StudentMaster sm, StudentDetail sd where sm.studentId = sd.studentMaster.studentId").list();
            //String [] argss = {"sm.studentStd","sm.studentRollno", "sm.studentFirstname", "sm.studentLastname", "sd.studentGender", "sd.studentPercentage", "sd.studentContact", "sd.studentAddress"};            
            //List list = session.createQuery("from StudentMaster").list();	
            session.getTransaction().commit();

            //DynamicReport dynamicReport = new ReflectiveReportBuilder (list,argss).build();
            DynamicReport dynamicReport = new ReflectiveReportBuilder (list).build();
            dynamicReport.setTitle("List of Employees");
            JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(dynamicReport, new ClassicLayoutManager(), list);
            //JasperViewer.viewReport(jasperPrint); //To View Report in Jasper pan
            //JasperExportManager.exportReportToPdfFile(jasperPrint, "C:\\TestDynamicJasper.pdf"); //To save pdf in hard drive
            resp.setContentType("application/octet-stream");
            resp.setHeader("Content-Disposition", "attachment;filename=\"NewPDF.pdf\"");
            JasperExportManager.exportReportToPdfStream(jasperPrint, resp.getOutputStream());
            //resp.getWriter().write("Welcome to Show Report");
            //resp.getWriter().flush();
            //resp.getWriter().close();
        } catch (Exception e) {
            System.out.println("Error : "+e);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}
Advertisements
This entry was posted in Generate PDF, Jasper Report, JSP and tagged . Bookmark the permalink.

One Response to Dynamic Jasper Report

  1. junaid ahmed says:

    hi how r u , i have a report its working fine but, i need to know how to save this report in harddrive……
    if (success.equalsIgnoreCase(“PRINT PPO”)) {
    try {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat(“dd/MM/yyyy”);
    InputStream reportStream = null;
    String fileName = “CF_”;
    if(pensionMaintenanaceDelegate.formTypeToPrint.equalsIgnoreCase(“printPpoForm”)) {
    fileName = fileName+”Ppo_”+pensionMaintenanaceDelegate.ppoFormData.get(0).getIpSerialNo();

    reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream(“/forms/ppo_form_maintenance.jasper”);

    }
    fileName=fileName+”_”+dateFormat.format(calendar.getTime())+”_”+pensionMaintenanaceDelegate.ppoFormData.get(0).getClaimNo()+”_”+pensionMaintenanaceDelegate.ppoFormData.get(0).getPpoNo()+”.pdf”;
    System.out.println(“************** File Name **************** “+fileName);
    HashMap parameterMap = new HashMap();
    parameterMap.put(“SUBREPORT_DIR”, “../../forms/”);
    System.out.println(“[PensionApprovalAction] execute: Form2Print=[“+pensionMaintenanaceDelegate.formTypeToPrint+”] Printing Report=[“+fileName+”] ReportStream=[“+reportStream+”]”);
    //JRDataSource dataSource = new JRBeanCollectionDataSource(pensionApplicationDelegate.claimFormIPData);
    JRDataSource dataSource = null;
    if(pensionMaintenanaceDelegate.formTypeToPrint.equalsIgnoreCase(“printPpoForm”)){
    System.out.println(“pensionApprovalDelegate.ppoFormData.get(0).getIpRegionFoCode =”+pensionMaintenanaceDelegate.ppoFormData.get(0).getIpRegionFoCode());
    System.out.println(“pensionApprovalDelegate.ppoFormData.get(0).getIpCheckDigit =”+pensionMaintenanaceDelegate.ppoFormData.get(0).getIpCheckDigit());

    dataSource = new JRBeanCollectionDataSource(pensionMaintenanaceDelegate.ppoFormData);
    System.out.println(“dataSource = “+dataSource);

    }

    response.reset();
    response.resetBuffer();
    servletOutputStream = response.getOutputStream();
    response.setContentType(“application/pdf”);
    response.setHeader(“Content-Disposition”,”inline; filename=”+fileName);

    JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parameterMap, dataSource);

    servletOutputStream.flush();
    servletOutputStream.close();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s