douchuang4402 2013-05-24 09:30
浏览 97

列文本换行使用fpdf

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);
 }
  • 写回答

1条回答 默认 最新

  • dqz7636 2013-05-24 09:52
    关注

    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();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c