This tutorial is intended for the PHP programmer who needs to incorporate PDF generation in a script without using external libraries such as PDFlib (often unavailable due to licensing restrictions or lack of funds). This tutorial is the second of two parts, and builds on what was covered in the first part. Therefore, if you have not yet gone through Part 1, you are advised to do so (or at least read through it), before going through this tutorial (Part 2). Apart from what was dealt with in Part 1, no knowledge of PDF file structure is required to understand this tutorial, as all references are explained.
The only code we still need to add for colors and drawings are some checks in the addPage() function. These make sure that the colors and line widths, set before any page is added, are actually inserted into the buffer, and that they are remembered from page to page.
For the addPage() function, introduced in Part 1, the three if() checks appear below, at the bottom of the function:
function addPage
() { $this->_page++; // Increment page count. $this->_pages[$this->_page] = ''; // Start the page buffer. $this->_state = 2; // Set state to page // opened. /* Check if font has been set before this page. */ if ($this->_font_family) { $this->setFont($this->_font_family, $this->_font_style, $this->_font_size); } /* Check if fill color has been set before this page. */ if ($this->_fill_color != '0 g') { $this->_out($this->_fill_color); } /* Check if draw color has been set before this page. */ if ($this->_draw_color != '0 G') { $this->_out($this->_draw_color); } /* Check if line width has been set before this page. */ if ($this->_line_width != 1) { $this->_out($this->_line_width); } }
Images The last step in this tutorial will be a brief look at images: specifically the inserting of the JPEG image type (since it is the simplest one to explain).
function image
($file, $x, $y, $width = 0, $height = 0) { if (!isset($this->_images[$file])) { /* First use of requested image, get the extension. */ if (($pos = strrpos($file, '.')) === false) { die(sprintf('Image file %s has no extension and no type was specified', $file)); } $type = strtolower(substr($file, $pos + 1)); /* Check the image type and parse. */ if ($type == 'jpg' || $type == 'jpeg') { $info = $this->_parseJPG($file); } else { die(sprintf('Unsupported image file type: %s', $type)); } /* Set the image object id. */ $info['i'] = count($this->_images) + 1; /* Set image to array. */ $this->_images[$file] = $info; } else { $info = $this->_images[$file]; // Known image, retrieve // from array. } /* If not specified, do automatic width and height * calculations, either setting to original or * proportionally scaling to one or the other given * dimension. */ if (empty($width) && empty($height)) { $width = $info['w']; $height = $info['h']; } elseif (empty($width)) { $width = $height * $info['w'] / $info['h']; } elseif (empty($height)) { $height = $width * $info['h'] / $info['w']; } $this->_out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q', $width, $height, $x, $this->_h - ($y + $height), $info['i'])); }