2011-10-12 5 views
6

Próbuję dynamicznie tworzyć dokumenty PDF na serwerze i wysyłać je do klienta za pomocą biblioteki Zend_Pdf. Cały tekst w pliku PDF musi być wyśrodkowany względem strony, która będzie miała rozmiar litery, krajobraz. Korzystając z funkcji, które znalazłem wiele razy na różnych stronach, mam problem - centralne uzasadnienie jest wyłączone. Cały tekst pojawia się zbyt daleko po lewej stronie. Oto mój kod:Dlaczego ten kod wyśrodkowuje tekst na pliku PDF przy użyciu biblioteki PHP Zend_Pdf nie działa?

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

?> 

... i to jest wynik.

Non-centered text

Odpowiedz

9

W przypadku ktoś wpada na podobny problem, problem jest tutaj:

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

Budując tablicę znaków, znaki są nieprawidłowo załadowany - 8 bitów, a nie 16.

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

Rozwiązuje to problem i poprawnie oblicza szerokość tekstu.

+0

Cheers, to pomogło mi się :) – zenzelezz

+0

Jak wdrożyć zawijanie tekstu za pomocą funkcja centrum? – Tom

+0

Mam ten sam problem. I ten kod też mi nie pomaga. Kiedy print_r $ font-> widthsForGlyphs ($ glyphs); otrzymują tylko tablice pełne zer – returnvoid

5

Spróbuj skorzystać z tej funkcji:

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

i wezwać go na swojej funkcji renderowania, takie jak:

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