使用fpdf的列文本换行


column text wrap using fpdf

我使用fpdf来生成pdf文件。我想输出一个包含一些列的表。我遇到的问题是我无法将列标签的文本标题从第二个数组包装到第八个数组。超出数组长度的文本不在列标签的视图中。任何建议都会很有帮助。

代码

$header = array(
array("label"=>"Subject Category", "length"=>75, "align"=>"L"),
array("label"=>"Total Pubs", "length"=>15, "align"=>"L"),
array("label"=>"%Pubs in Top 10% SNIP", "length"=>15, "align"=>"L"),
array("label"=>"%Pubs in Top 25% SNIP", "length"=>15, "align"=>"L"),
array("label"=>"Total Cites", "length"=>15, "align"=>"L"),
array("label"=>"%Cites in Top 10% SNIP", "length"=>15, "align"=>"L"),
array("label"=>"%Cites in Top 25% SNIP", "length"=>15, "align"=>"L"),
array("label"=>"4-year H_Index", "length"=>15, "align"=>"L")
);
foreach ($header as $col) {
  $pdf->Cell($col['length'],15, $col['label'], 1, '0', $col['align'], true);
 }

我之前不得不使用fpdf包装一些文本,最后这样做:

function word_wrap(&$fpdf, $text_that_might_need_wrapping) {
    // 64 was the maximum length that worked in my instance, in your case
    // I'm guessing this would be 15
    $max_length_before_wrap = 64;
    if (strlen($text_that_might_need_wrapping) > $max_length_before_wrap) {
        // Split text into words
        $words = explode(" ", $text_that_might_need_wrapping);
        $total_words = count($words);
        $line = '';
        $word = 0;
        // Generate a new text line from those words until the new line is nearly too long
        while ($word < $total_words and strlen($line . $words[0] . " ") < $max_length_before_wrap){
            $word++;
            $line .= array_shift($words) . " ";
        }
        // Add text to PDF and a new line
        $fpdf->Cell(0,5,$line, 0, 2, 'C');
        $fpdf->Ln(2);
        // Continue to wrap the remaining text
        $rest_of_text = implode(' ', $words);
        word_wrap($fpdf, $rest_of_text);
    } else {
        $fpdf->Cell(0,5,$text_that_might_need_wrapping, 0, 2, 'C');
    }
}

下面是一个从fpdf.org复制的例子:

$fpdf = new FPDF();
$fpdf->AddPage();
$fpdf->SetFont('Arial','B',16);
$fpdf->Cell(40,10,'Hello World!');
word_wrap($fpdf, "Some really really long text that needs wrapping a lot");
$fpdf->Output();