XPages to PDF with iText

October 25, 2011 – 9:05 am

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(“http://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=’http://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 !

  1. 9 Responses to “XPages to PDF with iText”

  2. Great post!

    Cheers,
    Brian

    By Brian Moore on Oct 25, 2011

  3. 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.

    By JJTB Somhorst on Oct 25, 2011

  4. @JJTB Somhorst: Agree. The code posted here is only a sample …

    By Ulrich Krause on Oct 25, 2011

  5. I think you can change the package from com.itextpdf to com.lowagie and use one of the older versions. And by that use the old license that don’t require that you publish your source code if you add it into some of your projects.
    http://en.wikipedia.org/wiki/IText

    By Fredrik Norling on Oct 25, 2011

  6. 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.

    By Tommy Valand on Oct 25, 2011

  7. @Tommy : We are waiting for a blogpost showing how to use Flying Saucer ;-)

    By Fredrik Norling on Oct 25, 2011

  8. 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!!!!!!!!!!!!!!!!

    By John Turnbow on Oct 26, 2011

  9. @Fredrik: Google is everybodys friend.. :P

    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.

    By Tommy Valand on Oct 26, 2011

  10. 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

    By Fernando on Dec 22, 2011

Sorry, comments for this entry are closed at this time.