简体   繁体   English

为什么这段代码不能使用 PHP Zend_Pdf 库将文本居中放置在 PDF 中?

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

I'm attempting to dynamically create PDF documents on the server and send them to the client using the Zend_Pdf library.我正在尝试在服务器上动态创建 PDF 文档并使用 Zend_Pdf 库将它们发送到客户端。 All text on the PDF needs to be center aligned to the page, which will be letter-sized, landscape. PDF 上的所有文本都需要与页面居中对齐,页面为信纸大小、横向。 Using functions that I have found multiple times on various sites, I'm having a problem - the center justification is off.使用我在各种网站上多次找到的功能,我遇到了问题 - 中心理由不正确。 All text is appearing way too far to the left.所有文本都向左显示太远。 Here is my code:这是我的代码:

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

?>

...and this is the result. ……这就是结果。

非居中文本

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.构建字符数组时,字符加载不正确 - 8 位,而不是 16 位。

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

This solves the problem, and correctly calculates text width.这解决了问题,并正确计算文本宽度。

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, ...);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM