Advanced Integrated RPG: Creating PDFs with RPG PDF Print E-mail
Programming - RPG
Written by Thomas Snyder   
Wednesday, 18 August 2010 00:00

Support MC Press - Visit Our Sponsors

Forums Sponsor

POPULAR FORUMS

Forums

Search Sponsor

POPULAR SEARCHES

Search

Create a PDF "Hello World" program with RPG.

 

Editor's note: This article is excerpted from the MC Press book Advanced Integrated RPG.

 

I hope you enjoyed my Video Tour presented on July 21 in which I discussed the possibilities of using Java with RPG to send email and to create Excel spreadsheets and PDFs. This article contains condensed excerpts from my book that will show you how to install the iText open-source project, which can be used to create PDFs right from within RPG.

 

Since the printing of my book, Advanced Integrated RPG, the iText project has had a new release that included a move to a new iText Web site. You can download the version of the iText jar file that is referred to in the book in the archives here.

Installing iText

To install iText, go to the download section of the iText Web site and obtain the latest Jar file supported on the Java Runtime Environment (JRE) installed on your IBM i system. You'll want to download the current release of the iText core file, which you'll find in the "Compiled code" section of the download table.

           

This book uses an older version of iText  that you can obtain by following a link to the iText archive on the download page. I've deliberately used the older version to set the minimum Java requirement to 1.4.2.

           

Copy the iText Jar file into your publicly accessible Jar file directory. Then, add the Jar file to your environment variable before starting your Java Virtual Machine.

           

We'll specify the /Public/Java/PDF_iText/iText-2.1.2u.jar location of the iText Jar file in the main procedure of the SVAIRJAVA service program (Figure 10.1).

 

//----------------------------------

//---      iText for PDF         ---

//----------------------------------

localPath = %TRIM(localPath)

          + ':/Public/Java'

          + '/PDF_iText/iText-2.1.2u.jar';

Figure 10.1: Setting the location of the iText Jar file in the SVAIRJAVA main procedure

 

Then, we'll use the location to set the environment variable to make the iText package visible to the JVM using the class path (Figure 10.2).

 

       //------------------------------------------------------

       // Put the entire class path together and implement.

       //------------------------------------------------------

       commandString = 'ADDENVVAR ENVVAR(CLASSPATH) '

                     + 'VALUE(''.:'

                     + %TRIM(localPath)

                     + ''') REPLACE(*YES)';

       monitor;

         ExecuteCommand(%trim(commandString):%len(%trim(commandString)));

       on-error;

         displayBytes = 'ERROR occurred on Class Path!';

         DSPLY displayBytes;

       endmon;

Figure 10.2: Setting the CLASSPATH environment variable in the SVAIRJAVA main procedure

 

The ADDENVVAR command will be executed when you call the main procedure of SVAIRJAVA using the CallP JavaServiceProgram(); prototyped call.

 

Paragraph

 

The Paragraph object (Figure 10.19) is a sequence of Phrase objects. It is possible to construct a Paragraph by adding Chunk and Phrase objects to the Paragraph.

 

D ITextParagraph...

D                 S               O   CLASS(*JAVA

D                                     :'com.lowagie.text-

D                                     .Paragraph')

Figure 10.19: Paragraph RPG object reference

 

Figure 10.20 shows the prototype for the Paragraph object constructor we'll be using.

 

D new_ITextParagraph...

D                 PR                  like(ITextParagraph)

D                                     ExtProc(*JAVA:

D                                     'com.lowagie.text-

D                                     .Paragraph':

D                                     *CONSTRUCTOR)

D  inString                           like(jString)

Figure 10.20: Prototype for constructor method of the Paragraph class

 

Paragraphs automatically insert new line characters at the end of the paragraphs and let you specify the alignment of the paragraphs. You can also specify the font with the Paragraph constructor (Figure 10.21).

 

D new_ITextParagraphFromFont...

D                 PR                  like(ITextParagraph)

D                                     ExtProc(*JAVA:

D                                     'com.lowagie.text-

D                                     .Paragraph':

D                                     *CONSTRUCTOR)

D  inString                           like(jString)

D  inFont                             like(ITextFont)

Figure 10.21: Prototype for constructor method of the Paragraph class with font

 

To make things easier, we'll create a custom procedure to take care of the bytes-to-String details. We'll also support both of the Paragraph constructors just as they were intended, by providing an optional Font parameter on the procedure to determine which version of the constructor to use. Figure 10.22 shows the custom procedure, AirPdf_newParagraph.

 

P AirPdf_newParagraph...

P                 B                   EXPORT

D AirPdf_newParagraph...

D                 PI                  like(ITextParagraph)

D  argBytes                  65535A   const varying

D                                     options(*varsize)

D  argFont                            like(ITextFont)

D                                     options(*nopass)

D  svString       S                   like(jString)

D  svParagraph    S                   like(ITextParagraph)

D                                     inz(*NULL)

 /free

  svString = new_String(argBytes);

  if (%parms > 1);

    svParagraph = new_ITextParagraphFromFont(svString: argFont);

  else;

    svParagraph = new_ITextParagraph(svString);

  endif;

  // Clean Up

  freeLocalRef(svString);

  return svParagraph;

 /end-free

P                 E

Figure 10.22: Custom procedure to create a new Paragraph with optional font

Adding Elements to a Document

Documents have a generic add method (Figure 10.23) that lets you add content to a document. All the text components we've discussed — Chunk, Phrase, and Paragraph — implement the Element interface, so they can be passed into the add method of the Document class (Figure 10.23).

 

081810Snyderfigure10_23

Figure 10.23: Partial JavaDoc for Document add method

 

Figure 10.24 shows the prototype for the add method of the Document class.

 

D ITextDocument_add...

D                 PR             1N

D                                     EXTPROC(*JAVA

D                                     :'com.lowagie.text.Document'

D                                     :'add')

D  inElement                          like(ITextElement)

Figure 10.24: Prototype for add method of the Document class

 

The Element class (Figure 10.25) is an interface that needs to be implemented by components that will use the add method of the Document class.

 

D ITextElement...

D                 S               O   CLASS(*JAVA

D                                     :'com.lowagie.text-

D                                     .Element')

Figure 10.25: Element RPG object reference

Saving the PDF File

iText provides other writers to create RTF and HTML files, but we'll limit our discussion to using PDFWriter to create PDFs. To write the document, we'll employ the PDFWriter class using the file output stream. The details of getting the writer and the output file will be handled within a new procedure, AirPdf_newDocumentOutput. Figure 10.29 shows the first part of this custom procedure.

 

P AirPdf_newDocumentOutput...

P                 B                   EXPORT

D AirPdf_newDocumentOutput...

D                 PI                  like(ITextDocument)

D  argFileName                2048A   const varying

D                                     options(*varsize)

D  argSizeName                  64A   const varying

D                                     options(*nopass: *varsize)

D  argRotate                     1N   const options(*nopass)

D  svString       S                   like(jString)

D  svDocument     S                   like(ITextDocument)

D                                     inz(*NULL)

D  svRectangle    S                   like(ITextRectangle)

D                                     inz(*NULL)

D  svOutFile      S                   like(FileOutputStream)

 /free

  if (%parms > 1);

    svString = new_String(%trim(argSizeName));

    monitor;

      svRectangle = PageSize_getRectangle(svString);

      if (%parms > 2);

        if (argRotate);

          svRectangle = ITextRectangle_Rotate(svRectangle);

        else;

        endif;

      else;

      endif;

      svDocument = new_ITextDocumentFromRectangle(svRectangle);

    on-error;

      svDocument = *NULL;

    endmon;

  else;

  endif;

Figure 10.29: RPG procedure to open an iText document for output (part 1 of 3)

 

Figure 10.32 shows the JavaDoc for the open and close methods of the Document class.

 

081810Snyderfigure10_32

Figure 10.32: Partial JavaDoc for the open and close methods of the Document class

 

Figure 10.33 shows the open method prototype for the Document class.

 

D ITextDocument_open...

D                 PR                  EXTPROC(*JAVA

D                                     :'com.lowagie.text.Document'

D                                     :'open')

Figure 10.33: Prototype for open Method of the Document Class

 

And Figure 10.34 shows the close method prototype for the Document class.

 

D ITextDocument_close...

D                 PR                  EXTPROC(*JAVA

D                                     :'com.lowagie.text.Document'

D                                     :'close')

Figure 10.34: Prototype for close method of the Document class

Hello World

It's time to create our first PDF from RPG! As an example, we'll create a new PDF to contain the "Hello World!" text string. Figure 10.35 shows the sample PDF.

 

081810Snyderfigure10_35

Figure 10.35: Hello World PDF

 

This example is a classic Hello World application, which simply puts the text into a PDF using RPG with the prototypes and service programs we've created. Figure 10.36 shows the RPG program.

 

 .

 . <Insert common header code here (Figure 10.3)>

 .

D  airDocument    S                   LIKE(ITextDocument)

D  airParagraph   S                   LIKE(ITextParagraph)

 /free

  CallP JavaServiceProgram();

  airDocument = AirPdf_newDocumentOutput(

                       '/Public/AIR10_01.pdf');

  airParagraph = AirPdf_newParagraph('Hello World!');

  ITextDocument_add(airDocument: airParagraph);

  ITextDocument_close(airDocument);

  // Clean Up

   .

   . <Insert cleanup code here>

   .

  *inlr = *ON;

 /end-free

Figure 10.36: RPG program for Hello World PDF

 

Thanks to all the front-end work done with the development of the SPAIRPDF and SVAIRPDF prototypes and service program — and, most of all, to the efforts of those who contributed to the iText project — the Hello World program is very simple. We simply open the document as an output file, create a paragraph containing the Hello World text, add the paragraph to the document, and close the document. That's the way it should be — nice and simple.

Advanced Integrated RPG Excerpt Conclusion

I hope you enjoyed the samples of what you can find in my book and that you'll give it a try. This is the final segment in my three-part excerpt series. You might like to refer back to the first two parts: "Advanced Integrated RPG: Providing Solutions to Meet Today's Industry Standards" and "Advanced Integrated RPG: Using Java with RPG."

 

The call to JavaServiceProgram will include the classpath settings above and also a call to getJNIEnv().  The references to SPAIRPDF and SVAIRPDF are talking about the prototypes and service program that are built throughout the book. Once you have these tools in your belt, you can see how easy it is to create a PDF in a few lines of code, as illustrated in Figure 10.36 above.


Thomas Snyder
About the Author:

Tom Snyder has a diverse spectrum of programming experience encompassing IBM technologies, open-source, Apple, and Microsoft and utilizing these technologies with applications on the server, on the web, or on mobile devices.

 

Tom has over 20 years experience as a software developer in various environments, primarily in RPG, Java, C#, and PHP and holds certifications in Java from Sun and PHP from Zend. Prior to software development, Tom worked as a Hardware Engineer at Intel and is a proud United States Naval Veteran Submariner who served aboard the USS Whale SSN638 submarine.

 

Tom is the best-selling author of Advanced Integrated RPG, which covers the latest programming techniques for RPG ILE and Java to utilize open-source technologies.

 

Originally from and currently residing in Scranton, Pennsylvania. Tom is currently involved in a Mobile Application Start-up company named JoltRabbit LLC.

 

 


MC Press books written by Thomas Snyder available now on the MC Press Bookstore.

 

Advanced, Integrated RPG Advanced, Integrated RPG

This book shows you how to take advantage of the latest technologies from within existing RPG applications.

List Price $79.95
Now On Sale
 

 

Read More >>
Last Updated on Monday, 16 August 2010 12:48
 
User Rating: / 6
PoorBest 
   MC-STORE.COM