Vous êtes sur la page 1sur 6

Create PDF from iSeries using iText

Written by David Morris Monday, 25 July 2011 15:45 - Last Updated Friday, 19 August 2011 18:32

I was thinking in this day and age that it would be easy to generate a PDF document from an iSeries AS/400. Surely if a Windows PC can do it then our wonderful platform can do it. But then I suddenly came to a realisation. Microsoft actually can't do it. Creating a PDF on Windows is achieved by installing another piece of software onto Windows (such as Acrobat or numerous free alternatives available now).

So how can we create a PDF from the iSeries?

With the use of Java running on the iSeries we can write a program to generate a PDF and store it on the IFS. We can then even send this PDF via email or print it directly if you have a printer that can handle PDF documents directly.

This small tutorial will show you how to create a PDF on the iSeries following these steps: Download and install the necessary software to your PC Create a Java program on the PC Run a test on the PC to create a PDF document Transfer the source code to the iSeries Compile the source code on the iSeries Run a test on the iSeries to create a PDF document

Step 1. Necessary Software

- OS/400 v5r4 - Windows XP - JDK 1.4.2 Update 19 ( click here to download ). Size = 50MB. - NetBeans 7 ( click here to download ). Size = 244MB - iText 2.1.7 Binary ( click here to download ) Size = 1MB (Make sure you get the Binary version, not the Source version).

Note: Paul Santangelo has pointed out that In order to install and use NetBeans 7 we actually

1/6

Create PDF from iSeries using iText


Written by David Morris Monday, 25 July 2011 15:45 - Last Updated Friday, 19 August 2011 18:32

need JDK 1.6 installed on our PC. Even though we will not use JDK 1.6 for our project, we do need to install it just so we can use NetBeans 7.

Why are we using old versions of JDK and iText?

OS/400 v5r4 includes JDK 1.4.2 so we need to install and use an old version of the JDK and an old version of iText. This is because we cannot run java classes built for JDK 1.6 on v5r4. - Install JDK 1.4.2. I installed it to C:Program FilesJavajdk1.4.2_19 - Install JDK 1.6 (required to install and run NetBeans 7). - Install NetBeans 7. Later we will tell NetBeans to use JDK 1.4.2 for our project. - Unzip the iText JAR file to any location on your PC. You cannot actually 'install' iText, instead later we will tell NetBeans to use the iText JAR file.

Step 2. Create a Java program on the PC

Now that NetBeans and JDK 1.4.2 have been installed and we have unzipped the iText JAR file we are read to create our Java program in NetBeans. - Start NetBeans. - Click on File and then New Project. - Select the category Java and the project Java Application and then click on Next. Name the project anything you like. I have chosen HelloPdf. Leave 'Use Dedicated Folder for Storing Libraries' unticked. Make sure there is a tick in 'Create Main Class'. Make sure there is a tick in 'Set as Main Project'. Then click on Finish.

The project will be setup and you should see the source code for the main class file loaded.

2/6

Create PDF from iSeries using iText


Written by David Morris Monday, 25 July 2011 15:45 - Last Updated Friday, 19 August 2011 18:32

We need to change the JDK to be used. - Click on File and then click on Project Properties. - Select Libraries. - Click on Manage Platforms button towards the top right corner. - Click on Add Platform towards the bottom left corner. - Navigate to the folder where you installed JDK 1.4.2. In my instance the folder is C:Progr am FilesJavajdk1.4.2_19 . Then click on Finish and then on Close . - Now click on the combo box next to Java Platform and select JDK 1.4. - A warning screen will appear confirming if you really want to change to version 1.4. Click Change Platform .

While we are in the Project Properties we need to add the iText JAR file to the project. If you are not in project properties click on File and then on Project Properties. Make sure you still have Libraries selected. Then click on Add JAR/Folder. Navigate to the location where you saved iText-2.1.7.jar and select it. Then click on Open. Then click on OK to close the Project Properties box.

Now you should be back at the source code of the main class. - Replace the entire source code with the following.

{include_code_listing images/stories/attachments/hellopdf.txt [show_linenum=0] [use_table=0]}

3/6

Create PDF from iSeries using iText


Written by David Morris Monday, 25 July 2011 15:45 - Last Updated Friday, 19 August 2011 18:32

Notice in Step two in the source code you can see the name of the file to be created. The forward slash can still be used even though we are running under windows. You can change this location and file name to anything you like. c:/temp/HelloPdf.pdf is another valid choice for the name of the file. By keeping it generic like we have it will still work on the iSeries and create the PDF document on the root level of the IFS. If you have different folders setup on the IFS then use the folder structure with the file name such as "/folder1/subfolder/HelloPdf.pdf"

Step 3. Run a test on the PC

Save your changes and then click on Run -> Run Main Project. The PDF document should have been created in the location you chose.

If you have successfully created your PDF congratulations! you are ready to move on to Step 4.

If you have not been able to get the PDF to be created then we need to look at a few things.

Did NetBeans report any errors when you ran the project? - Make sure you are using the same versions of the software that I have. The version numbers are shown towards the top of this tutorial. - Make sure you have JDK 1.4.2 selected (under Project Properties in the Libraries category). - Make sure you have added the iText-2.1.7.jar file (under Project Properties in the Libraries category).

Step 4. Transfer the source code to the iSeries

more coming soon, but basically:

4/6

Create PDF from iSeries using iText


Written by David Morris Monday, 25 July 2011 15:45 - Last Updated Friday, 19 August 2011 18:32

- Use FileZilla to connect to the iSeries - Create a folder to store your java programs (such as /java) - Create a hellopdf sub-folder (therefore you will now have /java/hellopdf) - Create a classes sub-folder under hellopdf (therefore you will now have /java/hellopdf/classes) - Transfer HelloPdf.java and iText-2.1.7.jar to /java/hellopdf.

Before proceeding to step 5 your environment should now look like the below: - /java/hellopdf/HelloPdf.java - /java/hellopdf/iText-2.1.7.jar - /java/hellopdf/classes (this is an empty folder)

Our java source file and the iText jar file are both in the hellopdf folder and we have an empty classes folder that will be used in the next step.

Step 5. Compile the source code on the iSeries

We need to set our CLASSPATH environment variable. This is a bit like a library list. It will tell the compiler and the java interpreter where to find our source file and the iText jar file. Using ADDENVVAR is safe as the default level for the command is *JOB which means it will only affect your current interactive job. You will not affect any other jobs on the system.

Once the classpath has been set we then start Qshell, change to our folder created in step 4 above and then compile our java class. - Log on to the iSeries. - ADDENVVAR CLASSPATH /java/hellopdf:/java/hellopdf/classes:/java/hellopdf/iText-2.1.7.jar - Start Qshell (STRQSH) - cd /java/hellopdf - javac -d classes HelloPdf.java

5/6

Create PDF from iSeries using iText


Written by David Morris Monday, 25 July 2011 15:45 - Last Updated Friday, 19 August 2011 18:32

The last command (beginning with javac) is telling the system to compile the HelloPdf class into a folder called classes . It can take 30 seconds or so to compile the class. You will know it has finished when Qshell returns a $ prompt.

Step 6. Run a test on the iSeries

Given we setup the CLASSPATH in step 5 above we can now simply run our class. Take note of the case sensitivity here. The package name is in all lowercase. The class name is in CamelCase. - java hellopdf.HelloPdf

The PDF should have been created and stored in the root level of the IFS. Use FileZilla to transfer the PDF document to your local PC. Then open the PDF file. Presto!

What's Next?

- Refer to http://itextpdf.com/ for loads of information on how to use iText including adding images, tables, changing orientation (portrait and landscape) etc. There are heaps of sample programs to help you along the way as well as a link to purchase the companion book. - You could write an RPG program to now send the PDF document via FTP to a printer that can print PDF documents directly (such as the Lexmark T654). - You could write a Java program to email the document.

6/6

Vous aimerez peut-être aussi