문제

누구든지 특정 글꼴과 크기에서 텍스트 조각이 소비 할 페이지에서 얼마나 많은 포인트를 계산할 수 있습니까? (Easy = 코드의 최소 줄 + 계산적으로 저렴합니다). zend_pdf는 getGlyphforCaracter (), getUnitsperem () 및 getWidthsForglyph ()에 대한 각 문자에 대한 매우 비싼 호출을 제외하고는이 작업을 수행하는 기능이없는 것으로 보입니다.

각 페이지에 여러 테이블이있는 멀티 페이지 PDF를 생성하고 열 내에서 텍스트를 랩핑해야합니다. 이미 그것을 만들 때 몇 초가 걸리고 있으며, 너무 오래 걸리기를 원하지 않거나 배경 작업이나 진행 막대 등으로 엉망이되어야합니다.

내가 제기 한 유일한 해결책은 사용 된 각 글꼴 크기의 각 문자의 너비 (포인트)를 미리 컴퓨팅 한 다음 각 문자열에 추가하는 것입니다. 여전히 비싸다.

내가 뭔가를 놓치고 있습니까? 아니면 더 간단한 것이 있습니까?

감사해요!

도움이 되었습니까?

해결책

사용하기보다는 폭을 정확하게 계산하는 방법이 있습니다. Gorilla3d의 최악의 경우 알고리즘.

이 코드를 시도하십시오 http://devzone.zend.com/article/2525-zend_pdf-tutorial#comments-2535

내 응용 프로그램에서이를 사용하여 올바른 정렬 텍스트에 대한 오프셋을 계산했으며 작동합니다.

/**
* Returns the total width in points of the string using the specified font and
* size.
*
* This is not the most efficient way to perform this calculation. I'm
* concentrating optimization efforts on the upcoming layout manager class.
* Similar calculations exist inside the layout manager class, but widths are
* generally calculated only after determining line fragments.
* 
* @link http://devzone.zend.com/article/2525-Zend_Pdf-tutorial#comments-2535 
* @param string $string
* @param Zend_Pdf_Resource_Font $font
* @param float $fontSize Font size in points
* @return float
*/
function widthForStringUsingFontSize($string, $font, $fontSize)
{
     $drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $string);
     $characters = array();
     for ($i = 0; $i < strlen($drawingString); $i++) {
         $characters[] = (ord($drawingString[$i++]) << 8 ) | ord($drawingString[$i]);
     }
     $glyphs = $font->glyphNumbersForCharacters($characters);
     $widths = $font->widthsForGlyphs($glyphs);
     $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
     return $stringWidth;
 }

성능과 관련하여, 나는 이것을 스크립트에서 집중적으로 사용하지 않았지만 느리다고 상상할 수 있습니다. 가능하면 PDF를 디스크에 작성하는 것이 좋습니다. 따라서 반복보기가 매우 빠르며 가능한 경우 캐싱/하드 코딩 데이터입니다.

다른 팁

이것에 대해 조금 더 생각합니다. 사용하는 글꼴의 가장 넓은 글리프를 사용하고 각 문자의 너비로 기준하십시오. 정확하지는 않지만 텍스트가 마크를 지나는 것을 막을 수 있습니다.

$pdf = new Zend_Pdf();
$font      = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER); 
$font_size = $pdf->getFontSize();


$letters = array();
foreach(range(0, 127) as $idx)
{
    array_push($letters, chr($idx));
}
$max_width = max($font->widthsForGlyphs($letters));

// Text wrapping settings
$text_font_size = $max_width; // widest possible glyph
$text_max_width = 238;        // 238px

// Text wrapping calcs
$posible_character_limit = round($text_max_width / $text_font_size);
$text = wordwrap($text, $posible_character_limit, "@newline@");
$text = explode('@newline@', $text);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top