Using Externally Described Printer File in RPGLE

Coding Externally described Printer file and printing external prtf using RPGLE program in IBM i
Using Externally Described Printer File in RPGLE , printer file,  external printer file, externally described printer file, prtf, external prtf,  ibmi, as400, as400 and sql tricks
Using Externally Described Printer File in RPGLE 

Introduction to Externally Described Printer file

Externally described printer file (records and fields) defined in external DDS. It can be used within a different RPG program.

Coding DDS for External Printer File(PRTF) in IBM i (AS400)

Let's see the example Using Externally Described Printer File in A-SPECS DDS source of type PRTF.

      A          R HEADER                                                     
      A                                  1  1'PAGE NUMBER:'                   
      A                                  1 14PAGNBR                           
      A                                  1 25'EXTERNALLY DESCRIBED PRTF'      
      A                                  1 70'DATE:'                          
      A                                  1 76DATE                             
      A                                  2  5'FLD1'                           
      A                                  2 20'FLD2'                           
      A          R DETAIL                                                     
      A            PFLD1         10A        5                                 
      A                                      SPACEB(1)                        
      A            PFLD2          5S 0     +5                                 
      A          R FOOTER                                                     
      A                                 62 20'---END OF REPORT---'            

Let's discuss the explanation of the above code below.

This is the layout of the Externally Described Printer File in A-SPECS which signifies how the records and their fields would be printed in the printer file.

The first record format is HEADER. Within header record format we will print the page number using the PAGNBR keyword and date using the DATE keyword and other headings information. Please note that in the External Printer file (PRTF) the positions referred to are the starting position.

The next record format is DETAIL and printer file field PFLD1 will be started printed from the 5th position after providing one space using keyword SPACEB(1) and then PFLD2 will be printed from the 20th position i.e. leaving spaces after printing PFLD1 of length 10 starts from 5th position.

At last record, format FOOTER gets printed which prints END OF REPORT once all the record gets printed using DETAIL record format.

Create External Printer File(PRTF) using the CRTPRTF command in IBM i (AS400)

External printer file PRTF2 will be created in library EASYCLASS1 using source member PRTF2 at source location EASYCLASS1/PRTF by providing line number 66 and width 132 to be printed per page and setting the overflow line number at 60th line.

 CRTPRTF FILE(EASYCLASS1/PRTF2)    
         SRCFILE(EASYCLASS1/PRTF)  
         SRCMBR(PRTF2)             
         PAGESIZE(66 132)          
         OVRFLW(60)                                                

Using Option 19 on External printer file in IBM i (AS400) to view PRTF design

Let's see the design of printer file using option 19

         -------------------------------------------------- Start of Page 001 ----------------------------------------------------
FLD1      .......... ..          ......................                     ...   ....                                        
 HEADER PAGE NUMBER: 9999       EXTERNALLY DESCRIBED PRTF                    DATE: 040422                                        
FLD1         ..            ..                                                                                                  
00002 +     FLD1           FLD2                                                                                                  
 DETAIL                                                                                                                          
FLD1        ........        ...                                                                                                 
00004 +     XXXXXXXXXX     99999                                                                                                 
00005 .                                                                                                                          
00006 .                                                                                                                          
00007 .                                                                                                                          
00008 .                                                                                                                          
00009 .                                                                                                                          
00010 .                                                                                                                          
00011 .                                                                                                                          
00012 .                                                                                                                          
00013 .                                                                                                                          
00014 .                                                                                                                          
00015 .                                                                                                                          
00016 . 
00017 .                                                                                                                  
00018 .                                                                                                                  
00019 .                                                                                                                  
00020 .                                                                                                                  
00021 .                                                                                                                  
00022 .                                                                                                                  
00023 .                                                                                                                  
00024 .                                                                                                                  
00025 .                                                                                                                  
00026 .                                                                                                                  
00027 .                                                                                                                  
00028 .                                                                                                                  
00029 .                                                                                                                  
00030 .                                                                                                                  
00031 .                                                                                                                  
00032 .                                                                                                                  
00033 .                                                                                                                  
00034 .                                                                                                                  
00035 .                                                                                                                  
00036 .
00037 .                                                                                                        
00038 .                                                                                                        
00039 .                                                                                                        
00040 .                                                                                                        
00041 .                                                                                                        
00042 .                                                                                                        
00043 .                                                                                                        
00044 .                                                                                                        
00045 .                                                                                                        
00046 .                                                                                                        
00047 .                                                                                                        
00048 .                                                                                                        
00049 .                                                                                                        
00050 .                                                                                                        
00051 .                                                                                                        
00052 .                                                                                                        
00053 .                                                                                                        
00054 .                                                                                                        
00055 .                                                                                                        
00056 .
00057 .                                                                                                 
00058 .                                                                                                 
00059 .                                                                                                 
00060 .                                                                                                 
00061 .                                                                                                 
FLD1                       <.................>                                                         
 FOOTER                    ---END OF REPORT---                                                          
                                                                                                                                 

Coding RPGLE program to print External printer file (PRTF) in RPGLE

     FPF1       IF   E           K DISK                           
     FPRTF2     O    E             PRINTER OFLIND(*IN01)          
      /Free                                                       
         WRITE HEADER;                                            
         setll *loval PF1;                                        
         read PF1;                                                
         dow not%eof(pf1);                                        
           if *in01 = *ON;                                        
             WRITE HEADER;                                        
             *IN01 = *OFF;                                        
           endif;                                                 
             PFLD1 = FLD1;                                        
             PFLD2 = FLD2;                                        
             WRITE DETAIL;                                        
           read PF1;                                              
         enddo;                                                   
         WRITE FOOTER;                                            
         *inlr =  *ON;                                            
         return;                                                  

Let's discuss the explanation of the above code line by line as below.

     FPF1       IF   E           K DISK

Here, the Externally described keyed physical file named PF1 has defined F specs in Input mode.

     FPRTF2     O    E             PRINTER OFLIND(*IN01)   

Here, the external printer file PRTF2 has been defined and associated with the overflow indicator *IN01 using OFLIND keyword.

 WRITE HEADER;

Here, we are printing header record format on the first page of the printer file as defined in the A SPECS external printer file.

    setll *loval PF1;                                    
         read PF1;        

Here, first the pointer set to lower limit on PF1 and then the record gets read.

         dow not%eof(pf1);                                    
           if *in01 = *ON;                                    
             WRITE HEADER;                                   
             *IN01 = *OFF;                                    
           endif;
             PFLD1 = FLD1;                                        
             PFLD2 = FLD2;        
             WRIT DETAIL;                                   
           read PF1;                                          
         enddo;

Here, we are reading records from file PF1 in a loop until they reached the end of the file. Within the loop, we check overflow indicator *IN01 sets to ON then only write the header record format and also reset the overflow indicator *IN01. Evaluate the physical file fields fld1 and fld2 to the printer file fields PFLD1 and PFLD2 respectively and always write the detailed record format within the loop so that all the records from file PF1 get printed.

     WRITE FOOTER;                                       
         *inlr =  *ON;                                        
         return;       

Write the footer record format at last once all detail has been printed and set last record indicator to ON and return from the program.

Compile RPGLE program using External printer file (PRTF) in IBM i (AS400)

                       Create Bound RPG Program (CRTBNDRPG)                     
                                                                               
 Type choices, press Enter.                                                    
                                                                               
 Program  . . . . . . . . . . . . PGM          > RPGLE2                        
   Library  . . . . . . . . . . .              >   EASYCLASS1                  
 Source file  . . . . . . . . . . SRCFILE      > PRTF                          
   Library  . . . . . . . . . . .              >   EASYCLASS1                  
 Source member  . . . . . . . . . SRCMBR       > RPGLE2                        
 Source stream file . . . . . . . SRCSTMF                                      
                                                                               
 Generation severity level  . . . GENLVL         10                            
 Text 'description' . . . . . . . TEXT           *SRCMBRTXT                    
                                                                               
 Default activation group . . . . DFTACTGRP      *YES                                 

Run RPGLE program using External printer file (PRTF) in IBM i (AS400)

 Call RPGLE2

Printed Output of Externally Described Spooled File

 PAGE NUMBER: 0001       EXTERNALLY DESCRIBED PRTF                    DATE: 040522               
    FLD1           FLD2                                                                         
    b              00006                                                                        
    b              00006                                                                        
    b              00006                                                                        
    b              00006                                                                        
    b              00006                                                                        
    b              00006                                                                        
    b              00006                                                                        
    b              00006                                                                        
    c              00007                                                                        
    c              00007                                                                        
    c              00007                                                                        
    c              00007                                                                        
    c              00007                                                                        
    c              00007                                                                        
    c              00007                                                                        
    c              00007                                                                        
    c              00007 
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                        
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                  
    c              00007                                                                                                                                                     c              00007                                                                  
    c              00007                                                                  
    A              00005                                                                  
    A              00005                                                                  
    A              00005                                                                  
    A              00005                                                                  
    A              00005                                                                  
    A              00005                                                                  
    A              00005                                                                  
    A              00005                                                                  
                   ---END OF REPORT---                                                                   

Post a Comment

© AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain