How can I right align text with Zend_PDF?
Asked Answered
S

1

5

I am working with Zend_PDF. The only task I can't figure out by myself is to align my texts to the right of my page.

How can I achive that?

EDIT

I used the drew010 answer below with others info found on the web to create a new class I am using to horizontally align the text.

<?php
/**
 * Advanced PDF functionnalities
 */
class Advanced_Pdf
{
    /**
     * Align text at left of provided coordinates
     */
    const TEXT_ALIGN_LEFT = 'left';

    /**
     * Align text at right of provided coordinates
     */
    const TEXT_ALIGN_RIGHT = 'right';

    /**
     * Center-text horizontally within provided coordinates
     */
    const TEXT_ALIGN_CENTER = 'center';

    /**
     * Extension of basic draw-text function to allow it to horizontally center text
     */
    public function drawTextWithPosition(Zend_Pdf_Page $page, $text, $y1, $xOffset = 0, $position = self::TEXT_ALIGN_LEFT, $encoding = null)
    {
        $bottom = $y1; // could do the same for vertical-centering
        $text_width = $this->getTextWidth($text, $page->getFont(), $page->getFontSize());

        switch ($position) {
            case self::TEXT_ALIGN_LEFT:
                $left = 60 + $xOffset;
                break;
            case self::TEXT_ALIGN_RIGHT:
                $left = $page->getWidth() - $text_width - $page->getFontSize() - 35 + $xOffset;
                break;
            case self::TEXT_ALIGN_CENTER:
                $left = ($page->getWidth() / 2) - ($text_width / 2) + $xOffset;
                break;
            default:
                throw new Exception("Invalid position value \"$position\"");
        }

        // display multi-line text
        foreach (explode(PHP_EOL, $text) as $i => $line) {
            $page->drawText($line,$left,$y1,$encoding);
        }
        return $this;
    }

    /**
     * Return length of generated string in points
     *
     * @param string $string
     * @param Zend_Pdf_Resource_Font $font
     * @param int $font_size
     * @return double
     */
    public function getTextWidth($text, Zend_Pdf_Resource_Font $font, $font_size) 
    {
        $drawing_text = iconv('', 'UTF-16BE', $text);
        $characters    = array();
        for ($i = 0; $i < strlen($drawing_text); $i++) {
            $characters[] = (ord($drawing_text[$i++]) << 8) | ord ($drawing_text[$i]);
        }
        $glyphs        = $font->glyphNumbersForCharacters($characters);
        $widths        = $font->widthsForGlyphs($glyphs);
        $text_width   = (array_sum($widths) / $font->getUnitsPerEm()) * $font_size;
        return $text_width; 
    }
}
?>
Steen answered 7/11, 2012 at 12:33 Comment(0)
S
10

Using the code from this answer, you can calculate the width of the text you want to draw based on the font and font size. Once you have the width of the string you can right justify it based on the width of the page.

$text = 'Some text to right align';
$textWidth = getTextWidth($text, $zendPdfFont, $fontSize);

$page->drawText($text,
                $page->getWidth() - $textWidth - $fontSize,
                $verticalPosition);

Similarly to center it:

$page->drawText($text,
               ($page->getWidth() / 2) - ($textWidth / 2),
                $verticalPosition);
Sheer answered 7/11, 2012 at 23:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.