Drawing

Geometry

PDF uses the same geometry as PostScript. It starts from bottom-left corner of page and by default is measured in points (1/72 of an inch).

Page size can be retrieved from a page object:

  1. $width  = $pdfPage->getWidth();
  2. $height = $pdfPage->getHeight();

Colors

PDF has a powerful capabilities for colors representation. Zend_Pdf module supports Gray Scale, RGB and CMYK color spaces. Any of them can be used in any place, where Zend_Pdf_Color object is required. Zend_Pdf_Color_GrayScale, Zend_Pdf_Color_Rgb and Zend_Pdf_Color_Cmyk classes provide this functionality:

  1. // $grayLevel (float number). 0.0 (black) - 1.0 (white)
  2. $color1 = new Zend_Pdf_Color_GrayScale($grayLevel);
  3.  
  4. // $r, $g, $b (float numbers). 0.0 (min intensity) - 1.0 (max intensity)
  5. $color2 = new Zend_Pdf_Color_Rgb($r, $g, $b);
  6.  
  7. // $c, $m, $y, $k (float numbers). 0.0 (min intensity) - 1.0 (max intensity)
  8. $color3 = new Zend_Pdf_Color_Cmyk($c, $m, $y, $k);

HTML style colors are also provided with Zend_Pdf_Color_Html class:

  1. $color1 = new Zend_Pdf_Color_Html('#3366FF');
  2. $color2 = new Zend_Pdf_Color_Html('silver');
  3. $color3 = new Zend_Pdf_Color_Html('forestgreen');

Shape Drawing

All drawing operations can be done in a context of PDF page.

Zend_Pdf_Page class provides a set of drawing primitives:

  1. /**
  2. * Draw a line from x1,y1 to x2,y2.
  3. *
  4. * @param float $x1
  5. * @param float $y1
  6. * @param float $x2
  7. * @param float $y2
  8. * @return Zend_Pdf_Page
  9. */
  10. public function drawLine($x1, $y1, $x2, $y2);
  1. /**
  2. * Draw a rectangle.
  3. *
  4. * Fill types:
  5. * Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill rectangle
  6. *                                             and stroke (default)
  7. * Zend_Pdf_Page::SHAPE_DRAW_STROKE          - stroke rectangle
  8. * Zend_Pdf_Page::SHAPE_DRAW_FILL            - fill rectangle
  9. *
  10. * @param float $x1
  11. * @param float $y1
  12. * @param float $x2
  13. * @param float $y2
  14. * @param integer $fillType
  15. * @return Zend_Pdf_Page
  16. */
  17. public function drawRectangle($x1, $y1, $x2, $y2,
  18.                     $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);
  1. /**
  2. * Draw a rounded rectangle.
  3. *
  4. * Fill types:
  5. * Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill rectangle
  6. *                                             and stroke (default)
  7. * Zend_Pdf_Page::SHAPE_DRAW_STROKE      - stroke rectangle
  8. * Zend_Pdf_Page::SHAPE_DRAW_FILL        - fill rectangle
  9. *
  10. * radius is an integer representing radius of the four corners, or an array
  11. * of four integers representing the radius starting at top left, going
  12. * clockwise
  13. *
  14. * @param float $x1
  15. * @param float $y1
  16. * @param float $x2
  17. * @param float $y2
  18. * @param integer|array $radius
  19. * @param integer $fillType
  20. * @return Zend_Pdf_Page
  21. */
  22. public function drawRoundedRectangle($x1, $y1, $x2, $y2, $radius,
  23.                        $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);
  1. /**
  2. * Draw a polygon.
  3. *
  4. * If $fillType is Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE or
  5. * Zend_Pdf_Page::SHAPE_DRAW_FILL, then polygon is automatically closed.
  6. * See detailed description of these methods in a PDF documentation
  7. * (section 4.4.2 Path painting Operators, Filling)
  8. *
  9. * @param array $x  - array of float (the X co-ordinates of the vertices)
  10. * @param array $y  - array of float (the Y co-ordinates of the vertices)
  11. * @param integer $fillType
  12. * @param integer $fillMethod
  13. * @return Zend_Pdf_Page
  14. */
  15. public function drawPolygon($x, $y,
  16.                             $fillType =
  17.                                 Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
  18.                             $fillMethod =
  19.                                 Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING);
  1. /**
  2. * Draw a circle centered on x, y with a radius of radius.
  3. *
  4. * Angles are specified in radians
  5. *
  6. * Method signatures:
  7. * drawCircle($x, $y, $radius);
  8. * drawCircle($x, $y, $radius, $fillType);
  9. * drawCircle($x, $y, $radius, $startAngle, $endAngle);
  10. * drawCircle($x, $y, $radius, $startAngle, $endAngle, $fillType);
  11. *
  12. *
  13. * It's not a really circle, because PDF supports only cubic Bezier
  14. * curves. But very good approximation.
  15. * It differs from a real circle on a maximum 0.00026 radiuses (at PI/8,
  16. * 3*PI/8, 5*PI/8, 7*PI/8, 9*PI/8, 11*PI/8, 13*PI/8 and 15*PI/8 angles).
  17. * At 0, PI/4, PI/2, 3*PI/4, PI, 5*PI/4, 3*PI/2 and 7*PI/4 it's exactly
  18. * a tangent to a circle.
  19. *
  20. * @param float $x
  21. * @param float $y
  22. * @param float $radius
  23. * @param mixed $param4
  24. * @param mixed $param5
  25. * @param mixed $param6
  26. * @return Zend_Pdf_Page
  27. */
  28. public function  drawCircle($x,
  29.                             $y,
  30.                             $radius,
  31.                             $param4 = null,
  32.                             $param5 = null,
  33.                             $param6 = null);
  1. /**
  2. * Draw an ellipse inside the specified rectangle.
  3. *
  4. * Method signatures:
  5. * drawEllipse($x1, $y1, $x2, $y2);
  6. * drawEllipse($x1, $y1, $x2, $y2, $fillType);
  7. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  8. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType);
  9. *
  10. * Angles are specified in radians
  11. *
  12. * @param float $x1
  13. * @param float $y1
  14. * @param float $x2
  15. * @param float $y2
  16. * @param mixed $param5
  17. * @param mixed $param6
  18. * @param mixed $param7
  19. * @return Zend_Pdf_Page
  20. */
  21. public function drawEllipse($x1,
  22.                             $y1,
  23.                             $x2,
  24.                             $y2,
  25.                             $param5 = null,
  26.                             $param6 = null,
  27.                             $param7 = null);

Text Drawing

Text drawing operations also exist in the context of a PDF page. You can draw a single line of text at any position on the page by supplying the x and y coordinates of the baseline. Current font, font size and page fill color are used for text drawing operations (see detailed description below).

  1. /**
  2. * Draw a line of text at the specified position.
  3. *
  4. * @param string $text
  5. * @param float $x
  6. * @param float $y
  7. * @param string $charEncoding (optional) Character encoding of source
  8. *               text.Defaults to current locale.
  9. * @throws Zend_Pdf_Exception
  10. * @return Zend_Pdf_Page
  11. */
  12. public function drawText($text, $x, $y, $charEncoding = '');

Example #1 Draw a string on the page

  1. ...
  2. $pdfPage->drawText('Hello world!', 72, 720);
  3. ...

Example #2 Set font color

  1. ...
  2. $pdfPage->setFillColor(Zend_Pdf_Color_Html::color('#990000'))
  3.         ->drawText('Hello world (in red)!', 72, 720);
  4. ....

By default, text strings are interpreted using the character encoding method of the current locale. if you have a string that uses a different encoding method (such as a UTF-8 string read from a file on disk, or a MacRoman string obtained from a legacy database), you can indicate the character encoding at draw time and Zend_Pdf will handle the conversion for you. You can supply source strings in any encoding method supported by PHP's » iconv() function:

Example #3 Draw a UTF-8-encoded string on the page

  1. ...
  2. // Read a UTF-8-encoded string from disk
  3. $unicodeString = fread($fp, 1024);
  4.  
  5. // Draw the string on the page
  6. $pdfPage->drawText($unicodeString, 72, 720, 'UTF-8');
  7. ...

Using fonts

Zend_Pdf_Page::drawText() uses the page's current font and font size, which is set with the Zend_Pdf_Page::setFont() method:

  1. /**
  2. * Set current font.
  3. *
  4. * @param Zend_Pdf_Resource_Font $font
  5. * @param float $fontSize
  6. * @return Zend_Pdf_Page
  7. */
  8. public function setFont(Zend_Pdf_Resource_Font $font, $fontSize);

PDF documents support PostScript Type 1 and TrueType fonts, as well as two specialized PDF types, Type 3 and composite fonts. There are also 14 standard Type 1 fonts built-in to every PDF viewer: Courier (4 styles), Helvetica (4 styles), Times (4 styles), Symbol, and Zapf Dingbats.

Zend_Pdf currently supports the standard 14 PDF fonts as well as your own custom TrueType fonts. Font objects are obtained via one of two factory methods: Zend_Pdf_Font::fontWithName($fontName) for the standard 14 PDF fonts or Zend_Pdf_Font::fontWithPath($filePath) for custom fonts.

Example #4 Create a standard font

  1. ...
  2. // Create new font
  3. $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
  4.  
  5. // Apply font
  6. $pdfPage->setFont($font, 36);
  7. ...

Constants for the standard 14 PDF font names are defined in the Zend_Pdf_Font class:

  • Zend_Pdf_Font::FONT_COURIER

  • Zend_Pdf_Font::FONT_COURIER_BOLD

  • Zend_Pdf_Font::FONT_COURIER_ITALIC

  • Zend_Pdf_Font::FONT_COURIER_BOLD_ITALIC

  • Zend_Pdf_Font::FONT_TIMES

  • Zend_Pdf_Font::FONT_TIMES_BOLD

  • Zend_Pdf_Font::FONT_TIMES_ITALIC

  • Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC

  • Zend_Pdf_Font::FONT_HELVETICA

  • Zend_Pdf_Font::FONT_HELVETICA_BOLD

  • Zend_Pdf_Font::FONT_HELVETICA_ITALIC

  • Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC

  • Zend_Pdf_Font::FONT_SYMBOL

  • Zend_Pdf_Font::FONT_ZAPFDINGBATS

You can also use any individual TrueType font (which usually has a '.ttf' extension) or an OpenType font ('.otf' extension) if it contains TrueType outlines. Currently unsupported, but planned for a future release are Mac OS X .dfont files and Microsoft TrueType Collection ('.ttc' extension) files.

To use a TrueType font, you must provide the full file path to the font program. If the font cannot be read for some reason, or if it is not a TrueType font, the factory method will throw an exception:

Example #5 Create a TrueType font

  1. ...
  2. // Create new font
  3. $goodDogCoolFont = Zend_Pdf_Font::fontWithPath('/path/to/GOODDC__.TTF');
  4.  
  5. // Apply font
  6. $pdfPage->setFont($goodDogCoolFont, 36);
  7. ...

By default, custom fonts will be embedded in the resulting PDF document. This allows recipients to view the page as intended, even if they don't have the proper fonts installed on their system. If you are concerned about file size, you can request that the font program not be embedded by passing a 'do not embed' option to the factory method:

Example #6 Create a TrueType font, but do not embed it in the PDF document

  1. ...
  2. // Create new font
  3. $goodDogCoolFont = Zend_Pdf_Font::fontWithPath('/path/to/GOODDC__.TTF',
  4.                                                Zend_Pdf_Font::EMBED_DONT_EMBED);
  5.  
  6. // Apply font
  7. $pdfPage->setFont($goodDogCoolFont, 36);
  8. ...

If the font program is not embedded but the recipient of the PDF file has the font installed on their system, they will see the document as intended. If they do not have the correct font installed, the PDF viewer application will do its best to synthesize a replacement.

Some fonts have very specific licensing rules which prevent them from being embedded in PDF documents. So you are not caught off-guard by this, if you try to use a font that cannot be embedded, the factory method will throw an exception.

You can still use these fonts, but you must either pass the do not embed flag as described above, or you can simply suppress the exception:

Example #7 Do not throw an exception for fonts that cannot be embedded

  1. ...
  2. $font = Zend_Pdf_Font::fontWithPath(
  3.            '/path/to/unEmbeddableFont.ttf',
  4.            Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION
  5.         );
  6. ...

This suppression technique is preferred if you allow an end-user to choose their own fonts. Fonts which can be embedded in the PDF document will be; those that cannot, won't.

Font programs can be rather large, some reaching into the tens of megabytes. By default, all embedded fonts are compressed using the Flate compression scheme, resulting in a space savings of 50% on average. If, for some reason, you do not want to compress the font program, you can disable it with an option:

Example #8 Do not compress an embedded font

  1. ...
  2. $font = Zend_Pdf_Font::fontWithPath('/path/to/someReallyBigFont.ttf',
  3.                                     Zend_Pdf_Font::EMBED_DONT_COMPRESS);
  4. ...

Finally, when necessary, you can combine the embedding options by using the bitwise OR operator:

Example #9 Combining font embedding options

  1. ...
  2. $font = Zend_Pdf_Font::fontWithPath(
  3.             $someUserSelectedFontPath,
  4.             (Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION |
  5.             Zend_Pdf_Font::EMBED_DONT_COMPRESS));
  6. ...

Standard PDF fonts limitations

Standard PDF fonts use several single byte encodings internally (see » PDF Reference, Sixth Edition, version 1.7 Appendix D for details). They are generally equal to Latin1 character set (except Symbol and ZapfDingbats fonts).

Zend_Pdf uses CP1252 (WinLatin1) for drawing text with standard fonts.

Text still can be provided in any other encoding, which must be specified if it differs from a current locale. Only WinLatin1 characters will be actually drawn.

Example #10 Combining font embedding options

  1. ...
  2. $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);
  3. $pdfPage->setFont($font, 36)
  4.         ->drawText('Euro sign - €', 72, 720, 'UTF-8')
  5.         ->drawText('Text with umlauts - à è ì', 72, 650, 'UTF-8');
  6. ...

Extracting fonts

Zend_Pdf module provides a possibility to extract fonts from loaded documents.

It may be useful for incremental document updates. Without this functionality you have to attach and possibly embed font into a document each time you want to update it.

Zend_Pdf and Zend_Pdf_Page objects provide special methods to extract all fonts mentioned within a document or a page:

Example #11 Extracting fonts from a loaded document

  1. ...
  2. $pdf = Zend_Pdf::load($documentPath);
  3. ...
  4. // Get all document fonts
  5. $fontList = $pdf->extractFonts();
  6. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
  7. $yPosition = 700;
  8. foreach ($fontList as $font) {
  9.     $page->setFont($font, 15);
  10.     $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
  11.                                    'en',
  12.                                    'UTF-8');
  13.     $page->drawText($fontName . ': The quick brown fox jumps over the lazy dog',
  14.                     100,
  15.                     $yPosition,
  16.                     'UTF-8');
  17.     $yPosition -= 30;
  18. }
  19. ...
  20. // Get fonts referenced within the first document page
  21. $firstPage = reset($pdf->pages);
  22. $firstPageFonts = $firstPage->extractFonts();
  23. ...

Example #12 Extracting font from a loaded document by specifying font name

  1. ...
  2. $pdf = new Zend_Pdf();
  3. ...
  4. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
  5.  
  6. $font = Zend_Pdf_Font::fontWithPath($fontPath);
  7. $page->setFont($font, $fontSize);
  8. $page->drawText($text, $x, $y);
  9. ...
  10. // This font name should be stored somewhere...
  11. $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
  12.                                'en',
  13.                                'UTF-8');
  14. ...
  15. $pdf->save($docPath);
  16. ...
  1. ...
  2. $pdf = Zend_Pdf::load($docPath);
  3. ...
  4. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
  5.  
  6. /* $srcPage->extractFont($fontName) can also be used here */
  7. $font = $pdf->extractFont($fontName);
  8.  
  9. $page->setFont($font, $fontSize);
  10. $page->drawText($text, $x, $y);
  11. ...
  12. $pdf->save($docPath, true /* incremental update mode */);
  13. ...

Extracted fonts can be used in the place of any other font with the following limitations:

  • Extracted font can be used only in the context of the document from which it was extracted.

  • Possibly embedded font program is actually not extracted. So extracted font can't provide correct font metrics and original font has to be used for text width calculations:

    1. ...
    2. $font = $pdf->extractFont($fontName);
    3. $originalFont = Zend_Pdf_Font::fontWithPath($fontPath);
    4.  
    5. $page->setFont($font /* use extracted font for drawing */, $fontSize);
    6. $xPosition = $x;
    7. for ($charIndex = 0; $charIndex < strlen($text); $charIndex++) {
    8.     $page->drawText($text[$charIndex], xPosition, $y);
    9.  
    10.     // Use original font for text width calculation
    11.     $width = $originalFont->widthForGlyph(
    12.                  $originalFont->glyphNumberForCharacter($text[$charIndex])
    13.              );
    14.     $xPosition += $width/$originalFont->getUnitsPerEm()*$fontSize;
    15. }
    16. ...

Image Drawing

Zend_Pdf_Page class provides drawImage() method to draw image:

  1. /**
  2. * Draw an image at the specified position on the page.
  3. *
  4. * @param Zend_Pdf_Resource_Image $image
  5. * @param float $x1
  6. * @param float $y1
  7. * @param float $x2
  8. * @param float $y2
  9. * @return Zend_Pdf_Page
  10. */
  11. public function drawImage(Zend_Pdf_Resource_Image $image, $x1, $y1, $x2, $y2);

Image objects should be created with Zend_Pdf_Image::imageWithPath($filePath) method (JPG, PNG and TIFF images are supported now):

Example #13 Image drawing

  1. ...
  2. // load image
  3. $image = Zend_Pdf_Image::imageWithPath('my_image.jpg');
  4.  
  5. $pdfPage->drawImage($image, 100, 100, 400, 300);
  6. ...

Important! JPEG support requires PHP GD extension to be configured.Important! PNG support requires ZLIB extension to be configured to work with Alpha channel images.

Refer to the PHP documentation for detailed information (» http://www.php.net/manual/en/ref.image.php). (» http://www.php.net/manual/en/ref.zlib.php).

Line drawing style

Line drawing style is defined by line width, line color and line dashing pattern. All of this parameters can be assigned by Zend_Pdf_Page class methods:

  1. /** Set line color. */
  2. public function setLineColor(Zend_Pdf_Color $color);
  3.  
  4. /** Set line width. */
  5. public function setLineWidth(float $width);
  6.  
  7. /**
  8. * Set line dashing pattern.
  9. *
  10. * Pattern is an array of floats:
  11. *     array(on_length, off_length, on_length, off_length, ...)
  12. * Phase is shift from the beginning of line.
  13. *
  14. * @param array $pattern
  15. * @param array $phase
  16. * @return Zend_Pdf_Page
  17. */
  18. public function setLineDashingPattern($pattern, $phase = 0);

Fill style

Zend_Pdf_Page::drawRectangle(), Zend_Pdf_Page::drawPolygon(), Zend_Pdf_Page::drawCircle() and Zend_Pdf_Page::drawEllipse() methods take $fillType argument as an optional parameter. It can be:

  • Zend_Pdf_Page::SHAPE_DRAW_STROKE - stroke shape

  • Zend_Pdf_Page::SHAPE_DRAW_FILL - only fill shape

  • Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill and stroke (default behavior)

Zend_Pdf_Page::drawPolygon() methods also takes an additional parameter $fillMethod:

  • Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING (default behavior)

    PDF reference describes this rule as follows:

    The nonzero winding number rule determines whether a given point is inside a path by conceptually drawing a ray from that point to infinity in any direction and then examining the places where a segment of the path crosses the ray. Starting with a count of 0, the rule adds 1 each time a path segment crosses the ray from left to right and subtracts 1 each time a segment crosses from right to left. After counting all the crossings, if the result is 0 then the point is outside the path; otherwise it is inside. Note: The method just described does not specify what to do if a path segment coincides with or is tangent to the chosen ray. Since the direction of the ray is arbitrary, the rule simply chooses a ray that does not encounter such problem intersections. For simple convex paths, the nonzero winding number rule defines the inside and outside as one would intuitively expect. The more interesting cases are those involving complex or self-intersecting paths like the ones shown in Figure 4.10 (in a PDF Reference). For a path consisting of a five-pointed star, drawn with five connected straight line segments intersecting each other, the rule considers the inside to be the entire area enclosed by the star, including the pentagon in the center. For a path composed of two concentric circles, the areas enclosed by both circles are considered to be inside, provided that both are drawn in the same direction. If the circles are drawn in opposite directions, only the "doughnut" shape between them is inside, according to the rule; the "doughnut hole" is outside.

  • Zend_Pdf_Page::FILL_METHOD_EVEN_ODD

    PDF reference describes this rule as follows:

    An alternative to the nonzero winding number rule is the even-odd rule. This rule determines the "insideness" of a point by drawing a ray from that point in any direction and simply counting the number of path segments that cross the ray, regardless of direction. If this number is odd, the point is inside; if even, the point is outside. This yields the same results as the nonzero winding number rule for paths with simple shapes, but produces different results for more complex shapes. Figure 4.11 (in a PDF Reference) shows the effects of applying the even-odd rule to complex paths. For the five-pointed star, the rule considers the triangular points to be inside the path, but not the pentagon in the center. For the two concentric circles, only the "doughnut" shape between the two circles is considered inside, regardless of the directions in which the circles are drawn.

Linear Transformations

Rotations

PDF page can be rotated before applying any draw operation. It can be done by Zend_Pdf_Page::rotate() method:

  1. /**
  2. * Rotate the page.
  3. *
  4. * @param float $x  - the X co-ordinate of rotation point
  5. * @param float $y  - the Y co-ordinate of rotation point
  6. * @param float $angle - rotation angle
  7. * @return Zend_Pdf_Page
  8. */
  9. public function rotate($x, $y, $angle);

Starting from ZF 1.8, scaling

Scaling transformation is provided by Zend_Pdf_Page::scale() method:

  1. /**
  2. * Scale coordination system.
  3. *
  4. * @param float $xScale - X dimention scale factor
  5. * @param float $yScale - Y dimention scale factor
  6. * @return Zend_Pdf_Page
  7. */
  8. public function scale($xScale, $yScale);

Starting from ZF 1.8, translating

Coordinate system shifting is performed by Zend_Pdf_Page::translate() method:

  1. /**
  2. * Translate coordination system.
  3. *
  4. * @param float $xShift - X coordinate shift
  5. * @param float $yShift - Y coordinate shift
  6. * @return Zend_Pdf_Page
  7. */
  8. public function translate($xShift, $yShift);

Starting from ZF 1.8, skewing

Page skewing can be done using Zend_Pdf_Page::skew() method:

  1. /**
  2. * Translate coordination system.
  3. *
  4. * @param float $x  - the X co-ordinate of axis skew point
  5. * @param float $y  - the Y co-ordinate of axis skew point
  6. * @param float $xAngle - X axis skew angle
  7. * @param float $yAngle - Y axis skew angle
  8. * @return Zend_Pdf_Page
  9. */
  10. public function skew($x, $y, $xAngle, $yAngle);

Save/restore graphics state

At any time page graphics state (current font, font size, line color, fill color, line style, page rotation, clip area) can be saved and then restored. Save operation puts data to a graphics state stack, restore operation retrieves it from there.

There are two methods in Zend_Pdf_Page class for these operations:

  1. /**
  2. * Save the graphics state of this page.
  3. * This takes a snapshot of the currently applied style, position,
  4. * clipping area and any rotation/translation/scaling that has been
  5. * applied.
  6. *
  7. * @return Zend_Pdf_Page
  8. */
  9. public function saveGS();
  10.  
  11. /**
  12. * Restore the graphics state that was saved with the last call to
  13. * saveGS().
  14. *
  15. * @return Zend_Pdf_Page
  16. */
  17. public function restoreGS();

Clipping draw area

PDF and Zend_Pdf module support clipping of draw area. Current clip area limits the regions of the page affected by painting operators. It's a whole page initially.

Zend_Pdf_Page class provides a set of methods for clipping operations.

  1. /**
  2. * Intersect current clipping area with a rectangle.
  3. *
  4. * @param float $x1
  5. * @param float $y1
  6. * @param float $x2
  7. * @param float $y2
  8. * @return Zend_Pdf_Page
  9. */
  10. public function clipRectangle($x1, $y1, $x2, $y2);
  1. /**
  2. * Intersect current clipping area with a polygon.
  3. *
  4. * @param array $x  - array of float (the X co-ordinates of the vertices)
  5. * @param array $y  - array of float (the Y co-ordinates of the vertices)
  6. * @param integer $fillMethod
  7. * @return Zend_Pdf_Page
  8. */
  9. public function clipPolygon($x,
  10.                             $y,
  11.                             $fillMethod =
  12.                                 Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING);
  1. /**
  2. * Intersect current clipping area with a circle.
  3. *
  4. * @param float $x
  5. * @param float $y
  6. * @param float $radius
  7. * @param float $startAngle
  8. * @param float $endAngle
  9. * @return Zend_Pdf_Page
  10. */
  11. public function clipCircle($x,
  12.                            $y,
  13.                            $radius,
  14.                            $startAngle = null,
  15.                            $endAngle = null);
  1. /**
  2. * Intersect current clipping area with an ellipse.
  3. *
  4. * Method signatures:
  5. * drawEllipse($x1, $y1, $x2, $y2);
  6. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  7. *
  8. * @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
  9. *
  10. * @param float $x1
  11. * @param float $y1
  12. * @param float $x2
  13. * @param float $y2
  14. * @param float $startAngle
  15. * @param float $endAngle
  16. * @return Zend_Pdf_Page
  17. */
  18. public function clipEllipse($x1,
  19.                             $y1,
  20.                             $x2,
  21.                             $y2,
  22.                             $startAngle = null,
  23.                             $endAngle = null);

Styles

Zend_Pdf_Style class provides styles functionality.

Styles can be used to store a set of graphic state parameters and apply it to a PDF page by one operation:

  1. /**
  2. * Set the style to use for future drawing operations on this page
  3. *
  4. * @param Zend_Pdf_Style $style
  5. * @return Zend_Pdf_Page
  6. */
  7. public function setStyle(Zend_Pdf_Style $style);
  8.  
  9. /**
  10. * Return the style, applied to the page.
  11. *
  12. * @return Zend_Pdf_Style|null
  13. */
  14. public function getStyle();

Zend_Pdf_Style class provides a set of methods to set or get different graphics state parameters:

  1. /**
  2. * Set line color.
  3. *
  4. * @param Zend_Pdf_Color $color
  5. * @return Zend_Pdf_Page
  6. */
  7. public function setLineColor(Zend_Pdf_Color $color);
  1. /**
  2. * Get line color.
  3. *
  4. * @return Zend_Pdf_Color|null
  5. */
  6. public function getLineColor();
  1. /**
  2. * Set line width.
  3. *
  4. * @param float $width
  5. * @return Zend_Pdf_Page
  6. */
  7. public function setLineWidth($width);
  1. /**
  2. * Get line width.
  3. *
  4. * @return float
  5. */
  6. public function getLineWidth();
  1. /**
  2. * Set line dashing pattern
  3. *
  4. * @param array $pattern
  5. * @param float $phase
  6. * @return Zend_Pdf_Page
  7. */
  8. public function setLineDashingPattern($pattern, $phase = 0);
  1. /**
  2. * Get line dashing pattern
  3. *
  4. * @return array
  5. */
  6. public function getLineDashingPattern();
  1. /**
  2. * Get line dashing phase
  3. *
  4. * @return float
  5. */
  6. public function getLineDashingPhase();
  1. /**
  2. * Set fill color.
  3. *
  4. * @param Zend_Pdf_Color $color
  5. * @return Zend_Pdf_Page
  6. */
  7. public function setFillColor(Zend_Pdf_Color $color);
  1. /**
  2. * Get fill color.
  3. *
  4. * @return Zend_Pdf_Color|null
  5. */
  6. public function getFillColor();
  1. /**
  2. * Set current font.
  3. *
  4. * @param Zend_Pdf_Resource_Font $font
  5. * @param float $fontSize
  6. * @return Zend_Pdf_Page
  7. */
  8. public function setFont(Zend_Pdf_Resource_Font $font, $fontSize);
  1. /**
  2. * Modify current font size
  3. *
  4. * @param float $fontSize
  5. * @return Zend_Pdf_Page
  6. */
  7. public function setFontSize($fontSize);
  1. /**
  2. * Get current font.
  3. *
  4. * @return Zend_Pdf_Resource_Font $font
  5. */
  6. public function getFont();
  1. /**
  2. * Get current font size
  3. *
  4. * @return float $fontSize
  5. */
  6. public function getFontSize();

Transparency

Zend_Pdf module supports transparency handling.

Transparency may be set using Zend_Pdf_Page::setAlpha() method:

  1. /**
  2. * Set the transparency
  3. *
  4. * $alpha == 0  - transparent
  5. * $alpha == 1  - opaque
  6. *
  7. * Transparency modes, supported by PDF:
  8. * Normal (default), Multiply, Screen, Overlay, Darken, Lighten,
  9. * ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion
  10. *
  11. * @param float $alpha
  12. * @param string $mode
  13. * @throws Zend_Pdf_Exception
  14. * @return Zend_Pdf_Page
  15. */
  16. public function setAlpha($alpha, $mode = 'Normal');
blog comments powered by Disqus