com.conecta.sat.utils.BuildBitacoraPDF.java Source code

Java tutorial

Introduction

Here is the source code for com.conecta.sat.utils.BuildBitacoraPDF.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.conecta.sat.utils;

import com.conecta.sat.exceptions.Excepciones;
import com.conecta.sat.utils.dto.BitacoraPDF;
import com.conecta.sat.utils.dto.PdfDTO;
import com.conecta.sat.hibernate.dto.Bitacora;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.draw.LineSeparator;
import java.awt.print.Book;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Color;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.ServletContextResource;
import org.springframework.web.servlet.view.document.AbstractPdfView;

/**
 *
 * @author LuisBeto
 */
public class BuildBitacoraPDF extends AbstractPdfView {

    String logoPath;
    String fontName;
    float[] columnas = {};
    float fontSize = 8;
    int widthLogo;
    int heightLogo;

    public String getLogoPath() {
        return logoPath;
    }

    public void setLogoPath(String logoPath) {
        this.logoPath = logoPath;
    }

    public float[] getColumnas() {
        return columnas;
    }

    public void setColumnas(float[] columnas) {
        this.columnas = columnas;
    }

    public float getFontSize() {
        return fontSize;
    }

    public void setFontSize(float fontSize) {
        this.fontSize = fontSize;
    }

    public int getWidthLogo() {
        return widthLogo;
    }

    public void setWidthLogo(int widthLogo) {
        this.widthLogo = widthLogo;
    }

    public int getHeightLogo() {
        return heightLogo;
    }

    public void setHeightLogo(int heightLogo) {
        this.heightLogo = heightLogo;
    }

    public String getFontName() {
        return fontName;
    }

    public void setFontName(String fontName) {
        this.fontName = fontName;
    }

    @Override
    protected void buildPdfDocument(Map<String, Object> map, Document document, PdfWriter writer,
            HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        //        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        hsr1.setContentType("application/pdf");
        DateFormat name = new SimpleDateFormat("ddMMyyyyhhmmss");
        hsr1.setHeader("Content-disposition", "attachment; filename=Bitacora" + name.format(new Date()) + ".pdf");
        List<BitacoraPDF> list = (List<BitacoraPDF>) map.get("listBitacora");
        String nombreUsuario = (String) map.get("nombreUsuario");

        PdfPTable table = new PdfPTable(columnas);
        table.setWidthPercentage(100);
        table.setSpacingBefore(10);

        Font font = FontFactory.getFont(FontFactory.HELVETICA);
        try {
            font.setFamily(fontName);
        } catch (Exception e) {
            font.setFamily("Verdana");
        }

        try {
            ServletContext servletContext = hsr.getSession().getServletContext();
            String relativeWebPath = logoPath;
            String absoluteDiskPath = servletContext.getRealPath(relativeWebPath);
            Image logo = Image.getInstance(absoluteDiskPath);
            //            Image logo = Image.getInstance("logo.png");
            System.out.println("La imagen se cargo correctamente");
            logo.scaleToFit(widthLogo, heightLogo);
            document.add(logo);
        } catch (Exception e) {
            System.err.println("ERROR" + Excepciones.getStackTrace(e));
            document.add(new Paragraph("Sin imagen " + logoPath));
        }
        font.setSize(fontSize + 3);
        Paragraph titulo = new Paragraph("Bitcora de Actividades", font);
        titulo.setAlignment(Element.ALIGN_RIGHT);
        document.add(titulo);

        Chunk CONNECT = new Chunk(new LineSeparator(0.5f, 100, java.awt.Color.BLACK, Element.ALIGN_CENTER, 3.5f));
        document.add(CONNECT);

        DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
        font.setSize(fontSize + 2);
        Paragraph fecha = new Paragraph(df.format(new Date()), font);
        fecha.setAlignment(Element.ALIGN_RIGHT);
        document.add(fecha);

        Paragraph pNombre = new Paragraph(nombreUsuario, font);
        pNombre.setAlignment(Element.ALIGN_RIGHT);
        document.add(pNombre);

        font.setSize(fontSize);
        font.setColor(java.awt.Color.white);

        // define table header cell
        PdfPCell cell = new PdfPCell();
        java.awt.Color color = java.awt.Color.LIGHT_GRAY;
        cell.setBackgroundColor(color);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);

        cell.setPadding(5);

        // write table header
        cell.setPhrase(new Phrase("Nombre de usuario", font));
        table.addCell(cell);

        cell.setPhrase(new Phrase("Bitcora", font));
        table.addCell(cell);

        cell.setPhrase(new Phrase("ltima fecha de Modificaciones", font));
        table.addCell(cell);

        Font font2 = FontFactory.getFont(FontFactory.HELVETICA);
        try {
            font2.setFamily(fontName);
        } catch (Exception e) {
            font2.setFamily("Verdana");
        }
        font2.setSize(fontSize);
        font2.setColor(java.awt.Color.black);
        // write table row data
        df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (BitacoraPDF bitacora : list) {
            cell = new PdfPCell();
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.addElement(new Phrase(bitacora.getNombre(), font2));
            table.addCell(cell);
            cell = new PdfPCell();
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.addElement(new Phrase(bitacora.getMensaje(), font2));
            table.addCell(cell);
            cell = new PdfPCell();
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.addElement(new Phrase(bitacora.getFecha(), font2));
            table.addCell(cell);
            cell = new PdfPCell();
        }

        document.add(table);
    }

}