Why is this code to center text on a PDF using the PHP Zend_Pdf Library not working?

前端 未结 3 808
忘掉有多难
忘掉有多难 2021-02-09 15:14

I\'m attempting to dynamically create PDF documents on the server and send them to the client using the Zend_Pdf library. All text on the PDF needs to be center aligned to the p

相关标签:
3条回答
  • 2021-02-09 15:51

    try to use this function:

    /**
        * Return length of generated string in points
        *
        * @param string                     $text
        * @param Zend_Pdf_Resource_Font|Zend_Pdf_Page     $font
        * @param int                         $fontSize
        * @return double
    */
    public static function getTextWidth($text, $resource, $fontSize = null/*, $encoding = null*/) {
        //if( $encoding == null ) $encoding = 'UTF-8';
    
        if( $resource instanceof Zend_Pdf_Page ){
            $font = $resource->getFont();
            $fontSize = $resource->getFontSize();
        }elseif( $resource instanceof Zend_Pdf_Resource_Font ){
            $font = $resource;
            if( $fontSize === null ) throw new Exception('The fontsize is unknown');
        }
    
        if( !$font instanceof Zend_Pdf_Resource_Font ){
            throw new Exception('Invalid resource passed');
        }
    
        $drawingText = $text;//iconv ( '', $encoding, $text );
        $characters = array ();
        for($i = 0; $i < strlen ( $drawingText ); $i ++) {
            $characters [] = ord ( $drawingText [$i] );
        }
        $glyphs = $font->glyphNumbersForCharacters ( $characters );
        $widths = $font->widthsForGlyphs ( $glyphs );
    
        $textWidth = (array_sum ( $widths ) / $font->getUnitsPerEm ()) * $fontSize;
        return $textWidth;
    }
    

    and call it on your render function, like:

    if ($this->getAlign() == self::TEXT_ALIGN_CENTER)
    {
            $x = ($currentPage->getWidth() - $this->getTextWidth($text, $currentPage)) / 2;
    }
    ...
    $currentPage->drawText($text, $x, $y, ...);
    
    0 讨论(0)
  • 2021-02-09 15:58

    In case anyone else runs into a similar problem, the issue is here:

    function getTextWidth($text, $font, $font_size) {
        $drawing_text = iconv('', 'UTF-8', $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;
    }
    

    When building the characters array, the characters are being loaded incorrectly - 8 bits, not 16.

    $characters[] = ord ($drawing_text[$i]);
    

    This solves the problem, and correctly calculates text width.

    0 讨论(0)
  • 2021-02-09 16:07

    If there is anyone looking for solution to Latin american characters like ó,á,é this worked for me

    try {
              $drawing_text = iconv('UTF-8', 'UTF-8', $text);
              $characters    = array();
              for ($i = 0; $i < strlen($drawing_text); $i++) {
                  $characters[] = ord ($drawing_text[$i]);
              }
              $glyphs        = $font->glyphNumbersForCharacters($characters);
              $widths        = $font->widthsForGlyphs($glyphs);
              $text_width   = (array_sum($widths) / $font->getUnitsPerEm()) * $font_size;
              return $text_width;
            } catch (\Exception $e) {
              //echo "$e";
            }
    
    0 讨论(0)
提交回复
热议问题