PDF = Pretty Darn Fine!
April 30, 2014 Ted Holt
If you’re running IBM i 6.1 or later, you have a great alternative to spooled files at your disposal–PDF. Here are four easy ways to add PDF generation to your programming. Method 1: Write directly to PDF. You can make your RPG (or COBOL or whatever) programs write directly to PDF stream files. You’ll need to specify the proper values in the Create Printer File (CRTPRTF), Change Printer File (CHGPRTF), or Override with Printer File (OVRPRTF) commands:
If you specify a directory, the system will invent a name for the generated stream file. If you specify a stream file name, that stream file must not exist at run time. By way of example, consider a simple report. First, create the printer to generate a PDF. * To create: * * CRTPRTF FILE(MYLIB/QAD03320P) * SRCFILE(MYLIB/QDDSSRC) SRCMBR(QAD03320P) * DEVTYPE(*AFPDS) + * TOSTMF('/tmp/Customer-list.PDF') WSCST(*PDF) A REF(QCUSTCDT) A R DETAIL SPACEB(1) A CUSNUM R 1 A LSTNAM R +1 A INIT R +1 A CITY R +1 Write an RPG program to build the report. Fqcustcdt if e disk Fqad03320p o e printer /free *inlr = *on; dow '1'; read cusrec; if %eof(); leave; endif; write detail; enddo; return; Voila! Method 2: Copy a spooled file to PDF. Use the Copy Spooled File (CPYSPLF) command to copy a spooled file to PDF. I have used this technique with both SCS and AFPDS spooled files. Specify *TOSTMF in the TOFILE parameter. The TOSTMF and WSCST parameters behave as in CRTPRTF. You may specify STMFOPT(*REPLACE) to build a new stream file over an existing stream file of the same name. CPYSPLF FILE(QPRTLIBL) TOFILE(*TOSTMF) SPLNBR(*LAST) + TOSTMF('Some-report.pdf') WSCST(*PDF) Method 3: A Slightly Longer Way to Copy a Spooled File to PDF. I’m told that Method 2 does not work under IBM i 6.1. Since I’ve never worked at 6.1, I’m not sure. Here’s a variation on the previous method. This method is slightly longer. It requires you to copy the spooled file to a temporary database file, and from there to a stream file. Notice the use of first-character forms control. CRTPF QTEMP/REPORT RCDLEN(133) CPYSPLF FILE(QPRINT01) TOFILE(QTEMP/REPORT) + SPLNBR(*LAST) CTLCHAR(*FCFC) OVRPRTF FILE(QSYSPRT) DEVTYPE(*AFPDS) CTLCHAR(*FCFC) + TOSTMF('test-report.pdf') WSCST(*PDF) CPYF FROMFILE(QTEMP/REPORT) TOFILE(QSYSPRT) One big disadvantage of this method is that it only handles SCS spooled files. Method 4: Read and reformat the spooled file. The previous methods all produce PDFs that look like the reports. If you’re willing to read the report, you can reformat it any way you like. Here’s an RPG program that reads a spooled file and builds a stream file. FSpoolIn if f 136 disk usropn FSpoolOut o f 132 printer oflind(*inof) F prtctl(prtctlds) F usropn D prtctlds ds 15 D pSpaceBefore 1 3 D pSpaceAfter 4 6 D pSkipBefore 7 9 D pSkipAfter 10 12 D pLineNbr 13 15 D OutputData ds 132 D InputDS ds D sSkipBefore 1 3 D sSpaceBefore 4 4 D InputData 5 136 /free *inlr = *on; open SpoolIn ; open SpoolOut; dow '1'; read SpoolIn InputDS; if %eof(); leave; endif; OutputData = InputData; // make changes to OutputData as desired if sSkipBefore <> *blanks; pSkipBefore = sSkipBefore; else; pSkipBefore = *blanks; endif; if sSpaceBefore <> *blanks; %subst(pSpaceBefore:3:1) = sSpaceBefore; else; pSpaceBefore = *blanks; endif; write SpoolOut OutputData; enddo; return; Here’s the CL that drives it. pgm dcl &Stmf *char 40 '/tmp/My-library-list.PDF' crtprtf qtemp/spoolout devtype(*afpds) + tostmf(&Stmf) wscst(*pdf) clrpfm qtemp/spoolin monmsg cpf3142 exec(do) crtpf qtemp/spoolin rcdlen(136) enddo cpysplf file(qprtlibl) tofile(qtemp/spoolin) + job(*) splnbr(*last) mbropt(*add) + ctlchar(*prtctl) /* Delete the stream file if it exists. */ rmvlnk objlnk(&Stmf) monmsg cpfa0a9 ovrdbf spoolin tofile(qtemp/spoolin) call qad03321r dltovr spoolin endpgm The program reads a database file into which a spooled file has been copied and writes to a stream file. This version uses a printer control data structure to control spacing and skipping. There you have it. If you want to integrate PDF into your programming, you can easily do so. If this is a topic you’d like to explore further, check out some of the links below. RELATED STORIES Converting a Spooled File to PDF
|