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
var con = facesContext.getExternalContext();
var response:com.ibm.xsp.webapp.XspHttpServletResponse = con.getResponse();
//setting response headers for browser to recognize data
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.addCreator(“IBM Lotus Domino V8.5.2 : XPages iText Library”);
newPDF.addTitle(“PDF Export Demo”);