In questo esempio vi mostrerò come creare un report scaricabile in formato PDF usando Liferay+iText.
Come prima cosa bisogna importare nel Built Path del plugin i seguenti JAR:
- core-renderer.jar
- dom4j-1.6.1.jar
- itext-pdfa-5.4.4.jar
- itext-xtra-5.4.4.jar
- itextpdf-5.4.4.jar
- xmlworker-5.4.5.jar
Poi si può passare all'implementazione:
1. creare un link "Download PDF" all'interno della portlet interessata:
<portlet:resourceURL var="submitFormDetailsResourceURL" escapeXml="false"/>
<p style="text-align:right;">
<a style="font-size:16px;" href="<%=submitFormDetailsResourceURL%>"
target="_blank">
Download <strong>PDF</strong>
</a>
</p>
<br/>
2. spostarsi nella classe che estende MVCPortlet della portlet/jsp in cui è stato inserito il link e scrivere un codice tipo questo:
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
Font fontSize18 = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD, BaseColor.RED);
Font fontSize9 = new Font(Font.FontFamily.TIMES_ROMAN, 9);
Font fontSize10 = new Font(Font.FontFamily.TIMES_ROMAN, 10);
Font fontSizeHeaderTable = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD,BaseColor.RED);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream portletOutputStream=null;
Document document=null;
try {
document = new Document();
PdfWriter.getInstance(document, baos);
document.open();
Paragraph p1 = new Paragraph("Books in My Library", fontSize18);
p1.setAlignment(Element.ALIGN_CENTER);
document.add(p1);
Paragraph p2 = new Paragraph("Created by: Pasturenzi Francesco", fontSize10);
p2.setAlignment(Element.ALIGN_RIGHT);
document.add(p2);
document.add( Chunk.NEWLINE );
PdfPTable table = new PdfPTable(6);
PdfPCell cell_titolo = new PdfPCell();
cell_titolo.addElement(new Paragraph("Titolo", fontSizeHeaderTable));
cell_titolo.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell_titolo);
PdfPCell cell_descr = new PdfPCell();
cell_descr.addElement(new Paragraph("Descrizione", fontSizeHeaderTable));
cell_descr.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell_descr);
PdfPCell cell_autori = new PdfPCell();
cell_autori.addElement(new Paragraph("Autori", fontSizeHeaderTable));
cell_autori.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell_autori);
PdfPCell cell_stato = new PdfPCell();
cell_stato.addElement(new Paragraph("Stato", fontSizeHeaderTable));
cell_stato.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell_stato);
PdfPCell cell_link = new PdfPCell();
cell_link.addElement(new Paragraph("Link", fontSizeHeaderTable));
cell_link.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell_stato);
PdfPCell cell_note = new PdfPCell();
cell_note.addElement(new Paragraph("Note", fontSizeHeaderTable));
cell_note.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell_note);
//recupero dati dal db
try {
List<MyLibrary> lista_books = MyLibraryLocalServiceUtil.getMyLibraries(0,
MyLibraryLocalServiceUtil.getMyLibrariesCount());
for(MyLibrary item: lista_books){
table.addCell(new Paragraph(item.getTitolo(), fontSize9));
table.addCell(new Paragraph(item.getDescrizione(), fontSize9));
table.addCell(new Paragraph(item.getAutori(), fontSize9));
table.addCell(new Paragraph(item.getStato(), fontSize9));
table.addCell(new Paragraph(item.getLink_more_info(), fontSize9));
table.addCell(new Paragraph(item.getNote(), fontSize9));
}
} catch (SystemException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
document.add(table);
document.close();
response.setContentType("application/pdf");
response.setProperty(HttpHeaders.CONTENT_DISPOSITION,"attachement;filename=Pasturenzi_Francesco_library");
response.addProperty(HttpHeaders.CACHE_CONTROL,"max-age=3600, must-revalidate");
response.setContentLength(baos.size());
System.out.println("baos.size(): "+baos.size());
portletOutputStream = (OutputStream) response.getPortletOutputStream();
baos.writeTo(portletOutputStream);
portletOutputStream.flush();
baos.close();
portletOutputStream.close();
} catch (Exception e) {
System.out.println( e.getMessage());
}
}
Le parti più rilevanti sono evidenziate in rosso, tutto il resto è sintassi iText per la creazione di un report.
Quello che ottengo è la creazione automatica di un report con 2 frasi in testa e una tabella con i libri della mi biblioteca.
Se volete vedere il risultato finale, cliccate su Download PDF a questa pagina: MyLibrary Pasturenzi Francesco.