使用Zend PDF在最后一页上绘制图像?

时间:2012-02-21 15:47:01

标签: php zend-framework pdf-generation zend-pdf

我知道有drawImage函数可以绘制图像。我想要的是只打印我的图像到底部的PDF的最后一页。我怎样才能做到这一点?这是功能:

page->drawImage($image, $imageTopLeft, $imageTop, $imageBottomRight, $imageBottom);

在这里添加坐标会在给定位置的每个页面上绘制它,不是吗?

谢谢!

1 个答案:

答案 0 :(得分:4)

如果您创建PDF和这样的页面:

$pdf = new Zend_Pdf();

$page1 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
// set content to this page
$pdf->pages[] = $page1;

$page2 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
// set content to this page
$pdf->pages[] = $page2;

// and so on ...

你可以这样做:

//Get your last page with $pdf->pages[count($pdf->pages[])-1]

$pdf->pages[count($pdf->pages[])-1]->drawImage($image, $imageTopLeft, $imageTop, $imageBottomRight, $imageBottom);

如果要查找文本宽度以调整其他元素,可以使用此功能:

    /**
     * Calculate width of given text
     * 
     * @param String $text 
     * @param Zend_Pdf_Resource_Font $font
     * @param int $font_size
     * @return int 
     */
    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;
    }

并且比你打电话:

$this->getTextWidth('some dinamyc text ', $font, $font_size);