Jul 02, 2010
Generating pdf with iText and Xpages
Brief tutorial to simply create PDF's inside XPages
Many thanks to John Mackey:
http://www.jmackey.net/groupwareinc/johnblog/johnblog.nsf/d6plinks/GROC-7G9GT4
and to Daniele Vistalli http://factor-y.com for this idea, for their help and the great powers.
Since Domino 8.5 is Eclipse based, you can switch to the Java perspective and add Java code to your project. The nsf database is the project. (If you haven't done this yet, it is interesting to switch perspectives and look around at the structure and the code.) By switching to the Java perspective, you open up your code to the importing or referencing of Java libraries, or the creation of your own classes.
Now we investigate how to create PDF using XPages. This tutorial summarize John's article, with some other things that you have to pay attention to.
Step by step.....
- Create your own .nsf
- Switch to the Java perspective by selecting: Window->Open Perspective->Other.
- Expand your project on the Left hand Project navigator. Select the WebContent/WEB-INF Folder
- right click and select New->Folder. Name the folder source, click Finish.
- right click on the source folder and select New->Package. Name the Package mypackage click Finish.
- right click on the mypackage package and select New->File. Enter Test.java for the name. Click Finish.
- Now double click on the Test.java and paste in the following code:
package mypackage;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* First iText example: Hello World.
*/
public class Test {
public static void createPdf(String filename) throws Exception {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
document.add(new Paragraph("Hello World!"));
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- select the project and then select Project->Properties from the menu (or right click)
- select the Java Build Path and click on Add Folder button. Select the new source folder. Click OK.
- In WebContent\WEB-INF add the folder called lib
- Right-click on it and select Import->File System->Browse and select itext jar (i.e iText-5.0.2.jar)
- import itext jar (i.e iText-5.0.2.jar) in your Java Build Path: Project->Properties->Java Build Path->Libraries->Add Jar and select jar from WebContent\WEB-INF
- Note: you have to deploy jar in your server, in data\domino\java and append this line in notes.ini, JavaUserClasses variable
JavaUserClasses=......;C:\Programmi\IBM\Lotus\Domino\data\domino\java\iText-5.0.2.jar
this will not cause security problem when you call iText methods to generate PDF
- Create a Script Library Server Side called SLCode and save this code on it
function create(){
mypackage.Test.createPdf("hello.pdf");
}
- Now create an xpage
- in Resources section, include SLCode library
- Drag a Button control in Xpage
- Goto Event section of this button, onClick event, and put this JSS code
create();
- Open your XPage in a browser and click the button: pdf will be created in domino directory on server
That's all!