【问题标题】:Draw an image on the last page using Zend PDF?使用 Zend PDF 在最后一页上绘制图像?
【发布时间】:2012-02-21 15:47:00
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 看起来不错,有没有办法在没有任何 x/y 值的情况下绘制它?我必须将它绘制在 PDF 上的另一个元素下方。元素始终不在同一个位置。
    • 不,据我所知。比您必须针对另一个元素 x/y 值设置 x/y 动态。
    • 我该怎么做?另一个元素是不会改变的文本。
    • 这会返回 67 ... 我现在该怎么办?用它来绘制图像?如果是,如何? :) 谢谢!
    • 那么您的徽标 x 位置将是:x-position-of-text + 67 + right-padding.....
    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2019-12-26
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多