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

发布于 2025-01-08 01:35:05 字数 231 浏览 7 评论 0原文

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

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

在此处添加坐标将在给定位置的每个页面上绘制它,不是吗?

谢谢!

I know there is the drawImage function to draw an image. What i want is to only print my image to the last page of the PDF, on the bottom. How can I do that? This is the function:

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

Adding coordinates here will draw it on every page on the given location, wont it?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生不灭 2025-01-15 01:35:05

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

If you create your PDF and Pages like this:

$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 ...

You can than do this:

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

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

If you want to find text width to adjust other elements you can use this function:

    /**
     * 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;
    }

and than you jus call:

$this->getTextWidth('some dinamyc text ', $font, $font_size);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文