Introduccion
Last updated
Was this helpful?
Last updated
Was this helpful?
Sitio principal
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.8</version>
</dependency>
<b:column medium-screen="25" >
<p:commandButton action="#{programacionVechicularController.imprimir()}" value="Reporte"
icon="icon-report" ajax="false">
</p:commandButton>
public void imprimir() {
com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.LETTER);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
PdfWriter.getInstance(document, baos);
//METADATA
document.open();
document.add(new Paragraph(" PROGRAMACION DE FLOTA VEHICULAR \n"));
DateFormat formatter= new SimpleDateFormat("dd/MM/yy '-' hh:mm:ss:");
Date currentDate = new Date();
String date = formatter.format(currentDate);
document.add(new Paragraph("Fecha Generado: "+date));
document.add(new Paragraph("\n"));
PdfPTable table = new PdfPTable(6);
table.setTotalWidth(new float[]{ 20,72, 110, 95, 170, 72 });
table.setLockedWidth(true);
PdfPCell cell = new PdfPCell(new Paragraph("Listado de ALumnos" ,
FontFactory.getFont("arial", // fuente
8, // tamaño
Font.BOLD)));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setColspan(6);
table.addCell(cell);
cell = new PdfPCell(new Paragraph ("ID", FontFactory.getFont("arial",8,Font.BOLD )));
table.addCell("conductor");
table.addCell("placa");
for (ProgramacionVehicular pv:programacionVehicular) {
table.addCell(pv.getIdviaje().toString());
table.addCell(pv.getConductor());
table.addCell(pv.getPlaca());
}
document.add(table);
} catch (Exception ex) {
System.out.println("Error " + ex.getMessage());
}
document.close();
FacesContext context = FacesContext.getCurrentInstance();
Object response = context.getExternalContext().getResponse();
if (response instanceof HttpServletResponse) {
HttpServletResponse hsr = (HttpServletResponse) response;
hsr.setContentType("application/pdf");
hsr.setHeader("Contentdisposition", "attachment;filename=report.pdf");
// hsr.setHeader("Content-disposition", "attachment");
hsr.setContentLength(baos.size());
try {
ServletOutputStream out = hsr.getOutputStream();
baos.writeTo(out);
out.flush();
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
}
context.responseComplete();
}
}