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


Draw an image on the last page using Zend PDF?

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

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

在这里添加坐标会把它画在给定位置的每一页上,不是吗?

谢谢!

如果您创建这样的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);