Zend Pdf Coordinates

, ,

Thought I would share a few things I recently learned about coordinates when using Zend Framework‘s Zend Pdf library.

  • The typographical point is the unit of measurement used for all coordinates. 72 points equals 1 inch. An 8.5″ * 11″ page is 612 * 792 points.
  • The x/y coordinate origin is the bottom left corner of the page. If you are laying out a page from top to bottom, the y-coordinate will get smaller as you work your way down the page.
  • Lines are centered on their coordinates. A 1-unit thick vertical line drawn at x-coordinate 50—
    $page->drawLine(50, $startY, 50, $endY);

    —will visually appear to start at x-coordinate 49.5 and end at x-coordinate 50.5. You can compute these values by taking the x-coordinate and subtracting (for the starting x) or adding (for the ending x) one half of the line thickness:

    starting x = x – thickness/2
    ending x = x + thickness/2

    The same rule applies to y-coordinates and horizontal lines.

  • When drawing rectangles, lines are also centered on x- and y-coordinates (see previous point). If you want a rectangle with a 2-unit thick border drawn so that its outside edges appear between 100, 50 and 200, 100, you’ll need to call draw rectangle with the following arguments:
    $page->drawRectangle(101, 51, 199, 99);

    Here’s the formula to compute these values:

    starting x = desired starting x + 1/2 line thickness
    starting y = desired starting y + 1/2 line thickness
    ending x = desired ending x – 1/2 line thickness
    ending y = desired ending y – 1/2 line thickness

  • The y-coordinate for text specifies the text’s baseline. If you know the y-coordinate you’d like to use for the very top of the line of text, you can calculate the baseline y-coordinate as follows:
    $yCoordinateBaseline = $desiredYCoordinate -
        ($font->getAscent() / $font->getUnitsPerEm() * $fontSize);

    (where $font is the appropriate Zend_Pdf font instance and $fontSize is the font’s size).

Leave a Reply

Your email address will not be published. Required fields are marked *