为什么此代码无法使用PHP Zend_Pdf库在PDF上居中显示文本?

问题描述 投票:6回答:3

我正在尝试在服务器上动态创建PDF文档,并使用Zend_Pdf库将其发送给客户端。 PDF上的所有文本都必须与页面居中对齐,页面大小应为字母大小,横向。使用在各种站点上多次发现的功能时,我遇到了一个问题-中心对齐不正确。所有文本都显示在左侧。这是我的代码:

<?
require('Zend/Pdf.php');

$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$pdf = new Zend_Pdf();

// Create a new page, add to page listing
$pdfPage = $pdf->newPage(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE);
$pdf->pages[] = $pdfPage;

// Add certify that
$pdfPage->setFont($font, 15.75);
drawCenteredText($pdfPage, "THIS IS TO CERTIFY THAT", 378);

// Add name
$pdfPage->setFont($font, 39.75);
drawCenteredText($pdfPage, "Example Name", 314.25);

// Headers
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=\"cert.pdf\"");
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

// Output PDF
echo $pdf->render();


function drawCenteredText($page, $text, $bottom) {  
    $text_width = getTextWidth($text, $page->getFont(), $page->getFontSize());
    $box_width = $page->getWidth();
    $left = ($box_width - $text_width) / 2;

    $page->drawText($text, $left, $bottom, 'UTF-8');
}

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

?>

...这就是结果。

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS83dU9YZC5wbmcifQ==” alt =“非居中的文字”>

php zend-framework pdf
3个回答
9
投票

如果其他任何人遇到类似的问题,问题在这里:

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

[构建字符数组时,字符加载不正确-8位而不是16位。

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

这解决了问题,并正确计算了文本宽度。


5
投票

尝试使用此功能:

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

并在您的渲染函数上调用它,例如:

if ($this->getAlign() == self::TEXT_ALIGN_CENTER)
{
        $x = ($currentPage->getWidth() - $this->getTextWidth($text, $currentPage)) / 2;
}
...
$currentPage->drawText($text, $x, $y, ...);

0
投票

[如果有人在寻找解决诸如ó,á,é这样的拉丁美洲字符的方法对我有用

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";
        }
© www.soinside.com 2019 - 2024. All rights reserved.