【问题标题】:FPDF - Inline bold textFPDF - 内嵌粗体文本
【发布时间】:2014-03-27 19:37:04
【问题描述】:

我正在尝试从 PHP 创建 PDF,出于法律原因,我们需要将免责声明的一部分加粗,并且需要概述免责声明。

我当前的代码使用:

if(isset($_POST['optout']) && $_POST['optout'] == "yes"){
    $pdf->Ln(5);
    $pdf->SetFont('Arial','I',12);
    $pdf->SetTextColor(128);
    $pdf->MultiCell(0,4,'This is my disclaimer. THESE WORDS NEED TO BE BOLD. These words do not need to be bold.',1,'C');
}

我目前正在将 WriteHTML 用于文档的其他部分,我可以轻松地使用它来代替 MultiCell,但是如何创建边框?

所以我有两个选择..

原生 FPDF 函数

优点:提供边框选项

缺点:没有简单的方法使内联文本变为粗体

WriteHTML 扩展类

优点:让我可以轻松地添加粗体文本

缺点:不确定如何创建边框

建议?

【问题讨论】:

    标签: php html border fpdf


    【解决方案1】:

    您可以重写扩展的一部分,但使用扩展 writeHTML 可能更容易,然后在您使用 writeHTML 创建的单元格上绘制一个带有边框的单元格(空文本)。通过正确调整您的单元格,它应该可以工作。

    不要忘记使用 SetY 然后 SetX 来定位您的单元格。

    例子:

    if(isset($_POST['optout']) && $_POST['optout'] == "yes"){
       $pdf->Ln(5);
       $pdf->SetFont('Arial','I',12);
       $pdf->SetTextColor(128);
    
       //Your text cell
       $pdf->SetY($pos_Y);
       $pdf->SetX($pos_X);
       $pdf->writeHTML('This is my disclaimer. <b>THESE WORDS NEED TO BE BOLD.</b> These words do not need to be bold.');
    
       //Your bordered cell
       $pdf->SetY($pos_Y);
       $pdf->SetX($pos_X);
       $pdf->Cell($width, $height, '', 1, 0, 'C');
     } 
    

    【讨论】:

    • 这就是我最终要做的,除了我使用Rect() 而不是单元格。谢谢。
    • 当我写“

      Word 1: Word 2

      ”时,它会在 Word 1 和 Word 2 之间创建一个换行符,如何解决这个问题?
    • MultiCell 怎么样?
    【解决方案2】:

    使用Write() & Rect()

    例子:

    $pdf->Rect($pdf->GetX(),$pdf->GetY(),2,0.1);
    $pdf->SetFont('Arial','',8);
    $pdf->Write(0.1,"this is not bold, but this ");
    $pdf->SetFont('Arial','B',8);
    $pdf->Write(0.1,"is bold.");
    $pdf->SetFont('Arial','',8);
    $pdf->Ln();
    

    您需要调整 Rect() 参数的宽度和高度。在这种情况下,我将 width = 2 和 height 设置为 0.1 User Units

    【讨论】:

      【解决方案3】:

      我是这样解决的:

      $pdf->SetFont('Arial','',10);
      $cell = 'This is my disclaimer.';
      $pdf->Cell($pdf->GetStringWidth($cell),3,$cell, 0, 'L');
      $pdf->SetFont('Arial','B',10);
      $boldCell = "THESE WORDS NEED TO BE BOLD.";
      $pdf->Cell($pdf->GetStringWidth($boldCell),3,$boldCell, 0, 'L');
      $pdf->SetFont('Arial','',10);
      $cell = 'These words do not need to be bold.';
      $pdf->Cell($pdf->GetStringWidth($cell),3,$cell, 0, 'L');
      

      基本上,制作一个单元格,更改字体,然后另一个单元格的宽度与您想要加粗的文本相同,依此类推。

      似乎有更好的工具可以制作使用 HTML / Blade 模板等的 PDF,因此您可能需要考虑使用它。

      【讨论】:

        【解决方案4】:

        我找到了其他解决方案。

        http://fpdf.de/downloads/add-ons/tag-based-formatting.html

        复制并粘贴文件fpdf.php中的片段代码write_tag,在创建pdf时调用函数write_tag,这样我可以在pdf中的文本上进行格式化。

        【讨论】:

          【解决方案5】:

          或者TCPDF支持通过writeHTMLCell()编写包含HTML的Cell,语法here

          $pdf->writeHTMLCell($width, $height, $x, $y, "<b>bold</b> and <u>underlined</u> text.");
          

          【讨论】:

            【解决方案6】:

            TCPD 比 FPDF 更有用

            在 TCPDF 中

            如果您在多单元格中将 $ishtml 参数设置为 true,则可以将 html 属性调整为您的文本。

            $text = "It <b>must</b> be like that";
            
            $this->MultiCell($w, $h, $text, $border, $align, $fill, $ln, $x, $y, $reseth, $stretch, true, $autopadding, $maxh);
            

            默认功能;

            MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0)
            

            你需要做的功能

            MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=true, $autopadding=true, $maxh=0)
            

            希望对你有帮助..

            【讨论】:

            • 回答一个超过五年的问题......大胆的举动。
            • 有点像与死熊搏斗:-p
            猜你喜欢
            • 2014-09-04
            • 2018-11-18
            • 2011-03-09
            • 2013-01-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-10
            相关资源
            最近更新 更多