Reportes con ReportUtils.java

JmoordbUtils ofrece la clase ReportUtils que nos permite utilizar iText y OpenPDF de manera simplificada.

Metodo

Paragraph paragraph(String texto, Font fonFactory, Integer ALIGN)

PdfPCell PdfCell(String texto, Font fontFactory, Integer ALIGN)

PdfPCell PdfCell(String texto, Font fontFactory)

Boolean printPDF(ByteArrayOutputStream baos)

Formulario

<p:commandButton action="#{programacionVechicularController.imprimir()}" value="Reporte"
                        icon="icon-report" ajax="false">
</p:commandButton>

Texto con fuentes y alineación

       Paragraph p = new Paragraph("PROGRAMACION DE FLOTA VEHICULAR",
               FontFactory.getFont("arial", // fuente
                         12, // tamaño
                           Font.BOLD));
          p.setAlignment(Element.ALIGN_CENTER);
           document.add(p);

Se puede simplificar mediante el ReportUtils.paragraph()

  document.add(ReportUtils.paragraph("PROGRAMACION DE FLOTA VEHICULAR", FontFactory.getFont("arial", 12,Font.BOLD), Element.ALIGN_CENTER));

Fecha y Hora

Date currentDate = new Date();
String date = DateUtil.showDate(currentDate) + " " + DateUtil.showHour(currentDate);

document.add(ReportUtils.paragraph("Fecha: " + date, FontFactory.getFont("arial", 10, Font.BOLD), Element.ALIGN_RIGHT));

Agregar títulos a las columnas tabla

  table.addCell(ReportUtils.PdfCell("Partida", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));

Agregar valores de las columnas

String fechaPartida = showDate(pv.getFechahorasalida()) + " " + showHour(pv.getFechahorasalida());
String fechaRegreso = showDate(pv.getFechahoraregreso()) + " " + showHour(pv.getFechahoraregreso());
String fechaSolicitado = showDate(pv.getFechasolicitud()) + " " + showHour(pv.getFechasolicitud());

table.addCell(ReportUtils.PdfCell(fechaPartida, FontFactory.getFont("arial", 9, Font.NORMAL)));
table.addCell(ReportUtils.PdfCell(pv.getNombredia(), FontFactory.getFont("arial", 9, Font.NORMAL)));

Método imprimir

Imprimimos un pdf con dos textos como títulos uno centrado y el otro a la derecha.

Genera una tabla con varias columnas y con fechas formateadas.

  // <editor-fold defaultstate="collapsed" desc="void imprimir() ">
    public void imprimir() {

        com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4.rotate());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            PdfWriter.getInstance(document, baos);
            //METADATA

            document.open();
            document.add(ReportUtils.paragraph("PROGRAMACION DE FLOTA VEHICULAR", FontFactory.getFont("arial", 12, Font.BOLD), Element.ALIGN_CENTER));

            Date currentDate = new Date();
            String date = DateUtil.showDate(currentDate) + " " + DateUtil.showHour(currentDate);

            document.add(ReportUtils.paragraph("Fecha: " + date, FontFactory.getFont("arial", 10, Font.BOLD), Element.ALIGN_RIGHT));
            document.add(new Paragraph("\n"));

            //Numero de columnas
            PdfPTable table = new PdfPTable(8);

//Aqui indicamos el tamaño de cada columna
            table.setTotalWidth(new float[]{85, 65, 85, 80, 85, 220, 72, 105});

            table.setLockedWidth(true);

            table.addCell(ReportUtils.PdfCell("Partida", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
            table.addCell(ReportUtils.PdfCell("Dia", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
            table.addCell(ReportUtils.PdfCell("Regreso", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
            table.addCell(ReportUtils.PdfCell("Unidad", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
            table.addCell(ReportUtils.PdfCell("Solicitado", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
            table.addCell(ReportUtils.PdfCell("Mision", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
            table.addCell(ReportUtils.PdfCell("Conductor", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));
            table.addCell(ReportUtils.PdfCell("Vehiculo", FontFactory.getFont("arial", 11, Font.BOLD), Element.ALIGN_CENTER));

            for (ProgramacionVehicular pv : programacionVehicular) {

                String fechaPartida = DateUtil.showDate(pv.getFechahorasalida()) + " " + DateUtil.showHour(pv.getFechahorasalida());
                String fechaRegreso = DateUtil.showDate(pv.getFechahoraregreso()) + " " + DateUtil.showHour(pv.getFechahoraregreso());
                String fechaSolicitado = DateUtil.showDate(pv.getFechasolicitud()) + " " + DateUtil.showHour(pv.getFechasolicitud());

                table.addCell(ReportUtils.PdfCell(fechaPartida, FontFactory.getFont("arial", 9, Font.NORMAL)));

                table.addCell(ReportUtils.PdfCell(pv.getNombredia(), FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(ReportUtils.PdfCell(fechaRegreso, FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(ReportUtils.PdfCell(pv.getUnidad(), FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(ReportUtils.PdfCell(fechaSolicitado, FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(ReportUtils.PdfCell(pv.getMision(), FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(ReportUtils.PdfCell(pv.getConductor(), FontFactory.getFont("arial", 9, Font.NORMAL)));
                table.addCell(ReportUtils.PdfCell(pv.getMarca() + " " + pv.getModelo() + " PLACA:" + pv.getPlaca(), FontFactory.getFont("arial", 9, Font.NORMAL)));

            }
            document.add(table);
        } catch (Exception ex) {
            System.out.println("Error " + ex.getMessage());
        }
        document.close();

        ReportUtils.printPDF(baos);

    }
    // </editor-fold> 

Last updated