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 !

9 thoughts on “XPages to PDF with iText

  1. Instead of calling this code directly create a java library where you add your itext code in and use it by calling an agent or adding it as a bean. This way you can make your code as generic as possible.

  2. If you want to generate PDFs from HTML, I recommend Flying Saucer. It’s CSS 2.1 compatible, so you can create quite nice layouts. CSS 2.1 has selectors that let you set HTML elements as headers and footers, generate page numbers/etc.

    Flying Saucer:
    http://code.google.com/p/flying-saucer/

    CSS Generated Content for Paged Media:
    http://www.w3.org/TR/css3-gcpm/

    The only “downside” with Flying Saucer is that it requires some extra permissions in java.policy if you want to use it from NSF.

  3. Great post!! Thanks…!

    Can someone explain why this is NOT already built into Xpages by IBM or as a Control. I see this as something that should already be in the product.

    Thanks again for Filling in for IBM!!!!!!!!!!!!!!!!

  4. @Fredrik: Google is everybodys friend.. 😛

    If the policy and Flying Saucer are in agreement, and the html is valid XHTML, it’s more or less plug and play 🙂

    I don’t have extensive knowledge about Flying Saucer, so I’m probably not the right person to blog about it. I used it in an app that was moved to a 64bit server. The old server used an older version of DominoPDF that was incompatible with 64bit Domino. Flying Saucer is lightyears ahead of DominoPDF in terms of flexibility.

  5. Hi, I am having trouble implementing this code; it raises an error on:
    “var newPDF:com.itextpdf.text.Document = new com.itextpdf.text.Document();”
    The error is “File not found”. Do you have an idea of what the problem could be?
    Thanks and regards
    Fernando

Comments are closed.