Convert a spool file to PDF using PHP on i5/OS


A simple PHP class to export a spool file in PDF Posted by on January 30, 2010

Recently, I started to develop on IBM i5 systems using PHP. PHP is becoming a very popular languages in the IBM i5 community to produce web applications and to modernize “green screen” applications. If you want to know how to use PHP on IBM i5 systems you have to look at the Zend Core for i5, the PHP distribution of Zend Technologies.

Zend Core for i5/OS provides the i5 Toolkit for PHP, an API system to call i5 functionalities from PHP. One of the function of this API is i5_spool that is able to manage spool files for specific i5 users. In this post I present a class to convert a spool file in PDF using the Zend_Pdf class of the Zend Framework.

The class is the i5Spool and you can download it here. You can easly convert a spool file in PDF choosing the page format, the page margins, the font type, the font size, the interline, etc.

Here an example of usage:

require_once 'i5Spool.php';
$spool= new i5Spool('host','username','password');
$sp = array (
    'JOBNAME' => 'QPADEV0012',
    'SPLFNAME' => 'QPJOBLOG',
    'SPLFNBR' => 3,
    'USERNAME' => 'ENRICO',
    'JOBNBR' => '697478'
);
header("Content-Disposition: inline; filename=spool.pdf");
header("Content-type: application/x-pdf");
echo $spool->toPdf($sp);

In this example, we create an i5Spool object passing the host, username and password to connect to the i5 system. In order to export a spool to PDF we use the toPdf method.

We have to pass the information related to the spool file: job name (JOBNAME), job number (JOBNBR), user (USERNAME), spool name (SPLFNAME) and spool number (SPLFNBR).

The toPdf function returns a string contains the PDF so you have to print it if you want to pass to the browser.

The toPdf method has a second optional parameter, an array contains the format of the PDF page.

The format of a PDF page is composed by (the default values are reported in parenthesis):

  • page format (Zend_Pdf_Page::SIZE_A4_LANDSCAPE);
  • font type (Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER));
  • font size (9);
  • interline (9);
  • margin-left (20);
  • margin-top (20);
  • margin-bottom (20);
  • encoding (utf-8);

For instance if you want to change the font size, the margins and the interline you can use the follow array as second parameter of toPdf method:

$format = array (
    'font-size'     => 10,
    'interline'     => 11,
    'margin-left'   => 30,
    'margin-top'    => 30,
    'margin-bottom' => 30
);