Dynamic PDF creation using XPages and the iTextPDF Java Libary

Recently I was tasked with adding an ‘Export To PDF’ function to one of our internal applications so I had a quick look around at possible solutions and I found that some others in the Lotus community had been successful in using the iTextPDF library with their applications but only with client based apps and classic Domino web apps. I couldn’t find any information of using iTextPDF with XPages so I set about seeing if I could get it to work.

The first thing you will need to do is download the iTextPDF JAR file and add it into your NSF. I won’t explain how to do that here as a quick google search shows that a number of people have blogged about how to do that already.

Once you have the JAR file in your NSF you can now generate your PDF files using SSJS. For my purposes I added a ‘link’ control to a page and then in the server side onClick even I used the following code…

———————————————————————

// Load the java packages
importPackage(com.itextpdf);

//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=”export.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());

// Open the PDF and write the PDF header info
newPDF.open();
newPDF.addAuthor(“Declan Lynch”);
newPDF.addCreationDate();
newPDF.addCreator(“IBM Lotus Domino V8.5.2 : XPages iText Library”);
newPDF.addTitle(“PDF Export Demo”);

Advertisements
Tagged with: , ,
Posted in None
16 comments on “Dynamic PDF creation using XPages and the iTextPDF Java Libary
  1. Sean Cull says:

    Declan,

    have you nay tips on getting Rich Text ( entered via cleitn or XPages ) out to PDF ?

    Thanks, Sean

    Like

  2. Chris Toohey says:

    Great tip; gives us just enough details to not lock us into thinking this is a one-application technique while showcasing the power now available to IBM Lotus developers!

    Like

  3. David DeWell says:

    Ok now I have to try this on Portal

    Like

  4. Paul Withers says:

    Yet again XPages opens up so many opportunities. An awesome tip, I can see it being of great benefit. And like all the best, it’s so simple.

    Like

  5. Tim Tripcony says:

    Once word of caution: we implemented PDF generation via iText in XPages for several customers last year and almost all of them initially encountered problems because their filesystem permissions were locked down too tight. iText stores all font definitions within the JAR file, which is very handy, but as a result, when it’s encoding the PDF, it has to extract the definition for each font that is used to a temp folder. If the server can’t write to itself in that location, Domino freaks out. Once those permissions are updated, it works great.

    In one case, they refused to change the folder permissions, so to get it working, we actually had to create a custom font file (which was actually just a copy of the embedded definition file for Arial given a custom font name), store it in a fixed location on the server’s hard drive, and tell the code to load the font from there. It was a hassle, but it did the trick.

    Like

  6. noon says:

    I think the iText library is the best PDF library out there. I contains tonns of stuff to do with.

    Recently I discovered a way to create PDFs from Dominon by using OpenOffice Writer as a PDF template engine. By this I can create really complex forms and PDF files. The PDF template contains (adobe PDF) “acro fields” which I can fill with values I want by using the iText library. Very handy solution.

    Oh and this solution is not depended on XPages, just pure Java 🙂

    Like

  7. Another solution would be to create a java agent which you trigger from the beforepageload (or what’s it called) of the xpage you are calling. This xpage then executes the agent and when that agent is done shows the pdf that can be downloaded. This works also very well.

    Like

  8. Curt Stone says:

    Awesome tip, Declan!

    Can you create a template in a PDF and populate data via bookmarks like we do in MS Word?

    Like

  9. Jeroen Somhorst says:

    Curt,

    in fact I think it would be possible by processing the pdf if it contained form fields. Otherwise there is always the idea of generating xml out of notes documents and use this xml to process it to pdf files with the saxparser classes.

    Like

  10. Nicholas Hopkins says:

    Thanks for this Declan,

    Being using iText in traditional web Domino for years…Will now try from XPages with SSJS.

    One thing I found was that the iText jar file in the nsf causes terminal memory leak….placed in the usual domino jar directory (forgotten exact path) works fine.

    Is this an issue when using SSJS instead of a java agent?

    Like

  11. Bryan Lue says:

    Your Learning Xpages links are not working.

    Like

  12. Efindi Ongso says:

    Thanks for the tips. I tried implementing your codes, however they do not seem to work on the server side. Would you please share with me further tips what could the issues be?

    Like

  13. Efindi Ongso says:

    Thanks for the tips. I tried implementing your codes, however they do not seem to work on the server side. Would you please share with me further tips what could the issues be?

    Like

  14. Naveen Maurya says:

    I tried your code Dec, but I am getting this error on browser:

    java.lang.IllegalStateException: Can’t get a Writer while an OutputStream is already in use

    Like

  15. Fernando Levi says:

    Hi Declan, great post. I managed to generate PDF files using text, tables, fonts, alignment. No problem. I am trying to include an image with no success. I keep getting an error when instanciating the Image() object. Do you have a code snipet for this? Thanks in advance.

    Like

  16. Mike Brown says:

    Declan,

    > Just be aware that the iTextPDF library is licensed under GPL which means that the source code

    > of your application must also be available under the GPL license unless you purchase the commercial

    >iTextLib license

    Like

Comments are closed.

Archives
%d bloggers like this: