PeopleSoft Create PDF File Example

We all know it is possible to generate a PDF Document from PeopleSoft. But how? This blog post will aim to help a beginner to the world of creating PDF documents from a PeopleSoft application. I will not be covering in-depth PDF creation concepts, but will show a beginner, that is indeed possible to create PDF documents through PeopleCode. This post is essentially the "Hello World" of creating PDF documents in PeopleSoft. Using this post as a launch pad, you can work wonders..get into a greater depth of PDF creation and design your PDFs on the fly. We will be using iText PDF API to create PDF documents in PeopleSoft and you will be needing this library for the example to work.

1) The first step towards creating PDF documents is to download the required JAR file and push it to your application server class folder. (Note: you can also create PDF from Application Engine programs / Integration broker messages, you need to load the JAR file in appropriate location for this). Download a copy of iText-2.1.7.jar and move the file to your class folder. The class folder would be PS_HOME/appserv/classes. Once you have done this, you will have to bounce the application server domain once. We only need this JAR file to get started.

2) We will begin with writing some code in the Field Change event, which will create a PDF file for us. This tutorial is an extension of creating PDF using Itext in Java. We will be doing this in PeopleSoft though and the explanations offered in the linked post is also valid for our case.  We begin with creating an object for com.lowagie.text.Document using CreateJavaObject call. The code snippet to create a Document Object is provided below:
/* Create a document object */
Local JavaObject &Obj_createpdfdocument_l = CreateJavaObject("com.lowagie.text.Document");
This snippet will give us a Document object which is essential for us to do anything into the PDF, in the form of creating tables, adding images / JavaScript content, adding shapes like squares and rectangles and so on..

3) In order to write a PDF file, we need to get an instance of PdfWriter object. The PdfWriter will serve as a listener to the Document Object. Whatever the Document Object "logically" performs (adding content etc), the PdfWriter object will "realize" it for us by writing it to a file. We will use the following PeopleCode to get an instance of PdfWriter object (Note that we are passing the Document object created in Step 2, to this step)
/* Get an instance of PDF Writer */
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_createpdfdocument_l, CreateJavaObject("java.io.FileOutputStream", "Hello.pdf", True));
4) In the next step, we will open the PDF document we created by using the "Open" method from PeopleCode. As you open the document, the writer interprets it and starts writing the Header information for a PDF file. This is a mandatory step and it will not be possible to add any content into the PDF document without opening it. The following PeopleCode snippet can be used to open a PDF document
&Obj_createpdfdocument_l.open();
5) In this step we will add some contents into the PDF. This is done by using the add method of Document object. We will create a welcome message for the PDF file that will be generated from PeopleSoft.
&Obj_createpdfdocument_l.add(CreateJavaObject("com.lowagie.text.Paragraph", "First PDF file Generated from PeopleSoft Application"));
6) For the paragraph addition in Step 5, the PDF writer would be interpreted it for us now and would have dumped the contents for us into the actual file. In the last step, we will close the PDF document and the writer would take it as a EOF (End of File). The EOF information will be written to the PDF file. This is done by the following PeopleCode
&Obj_createpdfdocument_l.close();
That completes the tutorial to getting started with creating a PDF file from PeopleSoft. When you run this code, you will find a PDF document welcoming you to the world of itext-PeopleSoft integration. The complete code for this basic example is provided below
/* Create a document object */
Local JavaObject &Obj_createpdfdocument_l = CreateJavaObject("com.lowagie.text.Document");
/* Get an instance of PDF Writer */
Local JavaObject &obj_writePDFoutput_l = GetJavaClass("com.lowagie.text.pdf.PdfWriter").getInstance(&Obj_createpdfdocument_l, CreateJavaObject("java.io.FileOutputStream", "Hello.pdf", True));
/* add some contents into the PDF */
&Obj_createpdfdocument_l.open();
&Obj_createpdfdocument_l.add(CreateJavaObject("com.lowagie.text.Paragraph", "First PDF file Generated from PeopleSoft Application"));
&Obj_createpdfdocument_l.close();
Did you run into any issues with the code? Let us know.

6 comments:

  1. Hi Author,
    Thanks for the information and I am trying to generate a Pdf from a txt file that come from an Application Engine. So can you tell me how i can wrap this Code with in an AE peoplecode to get the desired result.
    Thanks
    P

    ReplyDelete
  2. Hi!
    I'm trying to generate a PDF file pormedio PeopleCode.
    and download and store the jar mentioned (Download iText-2.1.7.jar), and run the event shows me this error:

    Excepci├│n Java: java.lang.NoClassDefFoundError: com.lowagie.text.Document

    could please inform me if I miss something?
    thank you

    ReplyDelete
  3. @Ray, can you check if you have the JAR file in your class path ?

    ReplyDelete
  4. I'm working on a project to convert Excel .xlsx files to pdf files. I've done some research and found that maybe using Java in Peoplecode is the best way. Correct me if I'm wrong. I'm still quite new on this.

    I installed these Java libraries:
    poi-3.11.jar

    commons-codec-1.9.jar
    poi-ooxml-3.11.jar
    poi-ooxml-schemas-3.11.jar
    xmlbeans-2.6.0.jar
    stax-api-1.0.1.jar

    I'm trying to use Reflection technique. I started my code to read an .xlsx file and I'm stuck here:

    Local string &file_name = "C:\temp\test1.xlsx";
    Local integer &tempint = 0;

    Local JavaObject &input_file = CreateJavaObject("java.io.File", &file_name);
    Local JavaObject &XSSFWorkbook = CreateJavaObject("org.apache.poi.xssf.usermodel.XSSFWorkbook", &input_file);
    Local JavaObject &jClass = GetJavaClass("java.lang.Class");
    Local JavaObject &jInt = GetJavaClass("java.lang.Integer");
    Local JavaObject &jIntType = &jInt.TYPE;
    Local JavaObject &jIntArgTypes = CreateJavaObject("java.lang.Class[]", &jIntType);
    Local JavaObject &jGetSheetAt = &XSSFWorkbook.getClass().getDeclaredMethod("getSheetAt", &jIntArgTypes);

    Local JavaObject &jSheet1 = &jGetSheetAt.invoke(&XSSFWorkbook, CreateJavaObject("java.lang.Object[]", CreateJavaObject("java.lang.Integer", "0")));
    rem Local JavaObject &jSheet1 = &jGetSheetAt.invoke(&XSSFWorkbook, CreateJavaObject("java.lang.Object[]", 0));

    Local JavaObject &jRowIterator = &jSheet1.getClass().getDeclaredMethod("iterator", Null);
    Local JavaObject &jIterator = &jRowIterator.invoke(&jSheet1, CreateJavaObject("java.lang.Object[]"));

    rem Local JavaObject &jRowNext = &jIterator.getClass().getDeclaredMethod("next", Null);
    rem Local JavaObject &jNextRow = &jRowNext.invoke(&jIterator, CreateJavaObject("java.lang.Object[]"));

    rem Local JavaObject &jRowHasNext = &jIterator.getClass().getDeclaredMethod("hasNext", Null);
    rem Local JavaObject &jHasNext = &jRowHasNext.invoke(&jIterator, CreateJavaObject("java.lang.Object[]"));


    The last four commented lines are where I'm having a lot of trouble with. I understand eventually I need to put them in a While loop to read each row that has content and within that each cell. But I even can't get that far. I don't seem to be able to call either the next() or the hasNext() methods properly. When I uncomment the first two lines to call next(), it gives me error "Java Exception: java.lang.InternalError: during call of java.lang.reflect.Method.invoke. (2,763) VH_XLTOPDF.MAIN.GBL.default.1900-01-01.Step07.OnExecute PCPC:1675 Statement:14"

    And when I uncomment the last two lines to call hasNext(), it gives error "Java Exception: java.lang.NoSuchMethodException: java.util.TreeMap$ValueIterator.hasNext(): during call of java.lang.Class.getDeclaredMethod. (2,763) VH_XLTOPDF.MAIN.GBL.default.1900-01-01.Step07.OnExecute PCPC:1539 Statement:13"

    Any help would be appreciated. Or if this is the wrong path to go for what I'm trying to achieve, please advice. Thank you very much in advance.

    ReplyDelete
  5. Hi, can anyone tell me how to extract contents of a pdf document i have tried a lot but could not understand
    PdfTextExtractor class getTextFromPage always throws no overload matches error, tried implementing reflection concept but could not can any one guide me how to do this

    ReplyDelete
  6. hi any one tell me how to extract contents from a pdf document any sample code to refer?

    ReplyDelete