使用fpdf进行文字换行[英] column text wrap using fpdf

本文是小编为大家收集整理的关于使用fpdf进行文字换行的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用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();

本文地址:https://www.itbaoku.cn/post/2090923.html

问题描述

I am using fpdf for generating a pdf file. I wanted to output a table with some columns. The problem I encountered is I am unable to wrap the text header of column label from second to eighth array. The text exceeding the array length is not in a view of column label.Any suggestions will be great help.

here is the code

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

推荐答案

I had to wrap some text using fpdf before and ended up doing this:

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

Here's an example copied from 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();