Java tutorial
/* * 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.PdfDTO; 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.EnumSet; import java.util.Enumeration; 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 BuildPDF 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=Reporte" + name.format(new Date()) + ".pdf"); List<PdfDTO> list = (List<PdfDTO>) map.get("list"); String nombreUsuario = (String) map.get("nombreUsuario"); PdfPTable table = new PdfPTable(9); 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("Reporte de Tokens", 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("Serial Token", font)); table.addCell(cell); // cell.setPhrase(new Phrase("Estatus Entrega", font)); cell.setPhrase(new Phrase("Estatus Entrega al Centro Financiero", font)); table.addCell(cell); cell.setPhrase(new Phrase("Estatus Entrega al Cliente", font)); table.addCell(cell); cell.setPhrase(new Phrase("Estatus Activacin", font)); table.addCell(cell); cell.setPhrase(new Phrase("Centro Financiero", font)); table.addCell(cell); cell.setPhrase(new Phrase("Tipo", font)); table.addCell(cell); cell.setPhrase(new Phrase("Cliente nico", font)); table.addCell(cell); cell.setPhrase(new Phrase("Folio Pivotal", font)); table.addCell(cell); // cell.setPhrase(new Phrase("ID nico", font)); // table.addCell(cell); cell.setPhrase(new Phrase("Fecha de ltima modificacin", 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 for (PdfDTO pdf : list) { cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getSerial(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getEntrega(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getEntrega(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getActivacion(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getCentro(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getTipo(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getCliente(), font2)); table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getFolioPivotal(), font2)); table.addCell(cell); // cell = new PdfPCell(); // cell.setHorizontalAlignment( Element.ALIGN_CENTER ); // cell.addElement( new Phrase(pdf.getIdUnico(),font2) ); // table.addCell(cell); cell = new PdfPCell(); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase(pdf.getLastUpdate(), font2)); table.addCell(cell); } document.add(table); } }