Convert AFP spooled files to PDF.
by Giuseppe Costagliola
SCS2PDF and SCS2ITEXT, presented in previous tips, are two simple utilities that allow you to convert SCS spooled files into PDF. Many readers are now eager to know how to convert AFP spooled files as well.
But before going any further, I again suggest trying IBM Infoprint Server, which allows you to transform any iSeries output format--such as SCS, AFP, IPDS, and OfficeVision/400--into PDF. This product creates text-based, space-saving, non-image ASCII PDF files, which preserves document fidelity and provides the ability to navigate through the document and distribute information via email, Web, or local printing. Furthermore, the Infoprint Server has been designed for high volumes and reliable delivery, and the latest version, V5.3, provides new capabilities like enhanced control over PDF file names and IFS locations, easy deployment and improved usability, encryption of document (along with password protection), full-color documents, control over the orientation of a document or report in the viewer, and miscellaneous enhancements that significantly reduce average PDF file size; some of these functions are also available in V5R2 as a PTF update. The product (5722-IP1) is pre-loaded on the OS/400 release CD set and can be installed for a 70-day free evaluation. To learn more about it, take a look at the IBM Redpapers "Printing VI" (SG24-6250) and "Printing VII" (REDP-3752-00). You will find sample programs, tips, application techniques, and performance considerations.
However, if you don't want to spend any money or you just want to try an open-source solution, keep on reading this tip. At the end, you will be able to create your PDF documents from any SCS or AFP report for free.
But first, let me tell you something about Tiff, libtiff, and Host Print Transform.
Tiff and libtiff
TIFF (Tag Image File Format) is an extremely common raster image format that was originally produced by Adobe, and libtiff is a set of C functions (a library) that support the manipulation of TIFF image files. The libtiff package, which can be downloaded from the Web, contains the tiff libraries and associated utilities. Among these utilities, you can find tiff2pdf, a C program that converts one TIFF file to one PDF file, including multiple page TIFF files, tiled TIFF files, as well as black and white and color TIFF files.
Host Print Transform
The Host Print Transform (HPT) API QWPZHPTR allows you to convert an AFPDS data stream to a TIFF file. (For more information, see the API Reference.) To generate a TIFF file, this API requires the IBM-supplied QWPTIFFG4 WSCST object, which can be found in QSYS. However, you can create your own Work Station Customization Object (WSCST) by running the CRTWSCST command against these instructions:
:INITPRT DATA ='4D4D002A'X.
:RESETPRT DATA ='00000000'X.
The AFP2PDF Utility
Let's put things together so you can build your utility that first converts spooled files into TIFF and then TIFF into PDF.
The HPT API creates a TIFF file that contains single-strip CCIT G4 compressed information (it's like a black and white scan) that is written directly to PDF from the tiff2pdf program without transcoding, thus making this final step very fast. The HPT API in this utility has been designed to convert spooled files one at a time; however, if you require heavier usage, you will dramatically reduce the CPU usage (and the conversion time) by initializing the HPT engine at the beginning of the job and terminating when all processing is complete. You can find detailed information in the QWPZHPTR API reference. The intermediate TIFF can also be re-sampled in order to achieve the best quality at the lowest PDF size, but this will be discussed in a future issue.
To see this utility in action, first create in your iSeries a subdirectory called libtiff-3.7.2 under the \pdf directory you created in Part Two of this series and a source file called QAFP2PDF. Download the content of the libtiff-3.7.2, and move the source files into your iSeries subdirectory. Then download the other source files into the QAFP2PDF. Because you won't use the entire libtiff source library, you will find only the modules needed by this implementation and an iSeries version of the unix.c and tiff2pdf.c modules, but you can download the entire library here. Finally, run all five MAKE REXX files in the correct sequence in order to create the CLE program TIFF2PDF, and compile all the RPG, CMD, and CLLE as usual.
If you already produce AFP reports, just run the AFP2PDF command to put the PDF in the specified IFS directory. But if you never created an AFP report, follow these steps to test the utility:
- Create a printer driver on a PC that has Client Access Express: Control Panel>Add Printer>Local Printer>Print to File>IBM AFP240.
- Design the overlay using a PC application like Microsoft Word.
- Print it to the AFP printer that you just created; make sure the Output Type is Overlay. You will be prompted for the directory and name where the overlay should be put.
- Open iSeries Navigator, and in the left panel, expand AFPmanager and Resources. Right-click Overlay and Import your overlay.
- Override your print file with DEVTYPE(*AFPDS) and FRONTOVL(library/overlay).
- Create your AFP report.
Enjoy creating your PDF directly from this spooled file!
Download all files by clicking here.