sábado, 16 de mayo de 2015

Reporte con itext con servlet


Crear una nuevo servlet en el paquete abcsac.servlet y el nombre Reporte.java  a continuacion escribe el codigo


import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

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 abcsac.dao.ProductoDao;
import abcsac.excepcion.DAOExcepcion;
import abcsac.modelo.Producto;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


@WebServlet("/Reporte")
public class Reporte extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public Reporte() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/pdf");
        //response.setHeader("Content-disposition", "attachment");

        Document document = new Document();
        try{
        PdfWriter.getInstance(document, response.getOutputStream());
        document.open();
        document.add(new Paragraph(" Reporte General de Productos \n"
                + "\n"));
          Date date = Calendar.getInstance().getTime();
          DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy, hh:mm:ss a");
            String today = formatter.format(date);
            document.add(new Paragraph("Fecha Actual: "+ today));
           
        document.add(new Paragraph(" "));
        PdfPTable table = new PdfPTable(6);
        table.setTotalWidth(new float[]{ 20,110, 110, 95, 72, 72 });
        table.setLockedWidth(true);
       
        PdfPCell cell;
       
        cell = new PdfPCell(new Paragraph("Id"));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(cell);
       
       
        cell = new PdfPCell(new Paragraph("Descripcion"));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(cell);
       
        cell = new PdfPCell(new Paragraph("Calidad"));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(cell);
       
       
        cell = new PdfPCell(new Paragraph("Metrado"));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(cell);
       
       
       
        cell = new PdfPCell(new Paragraph("Stock Min"  ));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(cell);
       
        cell = new PdfPCell(new Paragraph("Stock Max"));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(cell);
       
       
       
       

         ProductoDao dao= new ProductoDao();
         Collection<Producto> pro = dao.listar();
         for (Iterator<Producto> iterator = pro.iterator(); iterator.hasNext();) {
            Producto producto = (Producto) iterator.next();
            table.addCell(String.valueOf(producto.getIdProducto()));
            table.addCell(producto.getDescripcion());
            table.addCell(producto.getCalidad());
            table.addCell(producto.getMetrado());
            table.addCell(String.valueOf(producto.getStock_minimo()));
            table.addCell(String.valueOf(producto.getStock_maximo()));
        }
       
       
        document.add(table);
        document.close();
        }catch(DocumentException e){
        e.printStackTrace();
        } catch (DAOExcepcion e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

    }

}
 

Probar el ejemplo

<a href="Reporte">Reporte general</a>

 tal como se muestra

 

No hay comentarios:

Publicar un comentario