XPages to PDF with iText
Yesterday, David Leedy published a new extended edition of his great Notes in 9 video series. Brian Moore talked about how to create PDF from XPages and showed a simple example during the show.
I played a bit with the iText package and like to share some mor code that shows how to add tables and cells, anchors, chunks and phrases and even HTML to a PDF. Although this is very basic code, it should give you an idea, how to create other elements in PDF.
Here is the code. (Put it into a button on your XPage)
importPackage(com.itextpdf);
importPackage(java.io);
//Initialization
var con = facesContext.getExternalContext();
var response:com.ibm.xsp.webapp.XspHttpServletResponse = con.getResponse();
//setting response headers for browser to recognize data
response.setContentType(“application/pdf”);
response.setHeader(“Cache-Control”, “no-cache”);response.setDateHeader(“Expires”, -1);
response.setHeader( “Content-Disposition”, “attachment; filename=\”test.pdf\”” );
// Setup the PDF Output Stream
var newPDF:com.itextpdf.text.Document = new com.itextpdf.text.Document();
var writer = com.itextpdf.text.pdf.PdfWriter.getInstance(newPDF,response.getOutputStream());
var htmlWorker = new com.itextpdf.text.html.simpleparser.HTMLWorker(newPDF);
// Open the PDF and write the PDF header info
newPDF.open();
newPDF.addAuthor(“Ulrich Krause”);
newPDF.addCreationDate();
newPDF.addCreator(“IBM Lotus Domino V8.5.3 :iText Library”);
newPDF.addTitle(“PDF Demo”);
//add a Hello World text to the PDF
var helloWorld = new com.itextpdf.text.Paragraph(“Hello world1”)
newPDF.add(helloWorld);
//Create a table with 3 cells, width = 100%
var table = new com.itextpdf.text.pdf.PdfPTable(3); // 3 columns.
var cell1 = new com.itextpdf.text.pdf.PdfPCell(new com.itextpdf.text.Paragraph(“Table Cell 1”));
var cell2 = new com.itextpdf.text.pdf.PdfPCell(new com.itextpdf.text.Paragraph(“Table Cell 2”));
var cell3 = new com.itextpdf.text.pdf.PdfPCell(new com.itextpdf.text.Paragraph(“Table Cell 3”));table.setWidthPercentage(100);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
newPDF.add(table);
// Add a Chunk to the PDF; chunks do not add a CRLF when the leght exceeds page width. Better use phrases
newPDF.add(new com.itextpdf.text.Chunk(“This is a chunk. “));
// Add a Phrase to the PDF
newPDF.add(new com.itextpdf.text.Phrase(“This is a phrase. “));
// Add a ordered List to the PDF
var orderedList = new com.itextpdf.text.List(com.itextpdf.text.List.ORDERED);
orderedList.add(new com.itextpdf.text.ListItem(“Item 1”));
orderedList.add(new com.itextpdf.text.ListItem(“Item 2”));
orderedList.add(new com.itextpdf.text.ListItem(“Item 3”));
newPDF.add(orderedList)
// Add a link to the PDF
var anchor = new com.itextpdf.text.Anchor(“eknori.de”);
anchor.setReference(“https://www.eknori.de”);
newPDF.add(anchor);
// Add HTML to the PDF
var str = “<html><head><title>titlu</title></head><body><table><tr><td><br /><br /><a href=’https://www.eknori.de’>Link 2 eknori.de</a><br /><br /><br />Test</td></tr></table></body></html>”;
htmlWorker.parse(new java.io.StringReader(str));
// Finish
newPDF.close();
writer.close();facesContext.responseComplete();
Have fun !