【问题标题】:line height auto adjust in fpdf multicell ()fpdf multicell()中的行高自动调整
【发布时间】:2016-06-13 07:05:06
【问题描述】:

我正在使用 fpdf 从 MySql 表中的数据动态创建 pdf。我正在使用 MultiCell() 函数打印它。我的问题是我希望它自动调整文本的行高。我已经阅读了网站上的文档,但它只讲述了自动调整宽度。但我找不到任何线高自动调整的解决方案。有没有办法这样做。有哪些替代方法?请帮忙。这是一段代码。 :-

<?php session_start();

include 'conn.php';
$table=$_SESSION["name"];
$testid=$_SESSION["id"];
$stuid=$_SESSION["stuid"];
$ans=$_SESSION["score1"];

//pdf creation
require('../fpdf/fpdf.php');

class PDF extends FPDF
{
function Header()
{
global $title;
$this->Image('../fpdf/logo.JPG',10,10,200);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Calculate width of title and position
$w = $this->GetStringWidth($title)+6;
$this->SetX((210-$w)/2);
// Colors of frame, background and text

// Thickness of frame (1 mm)
$this->SetLineWidth(1);
// Title
// Line break
$this->Ln(20);
}

function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Text color in gray
$this->SetTextColor(128);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
}

function ChapterTitle($num, $label)
{
// Arial 12
$this->SetFont('Arial','',12);
// Background color
$this->SetFillColor(200,220,255);
// Title
$this->Cell(0,6,"Chapter $num : $label",0,1,'L',true);
// Line break
$this->Ln(4);
}

function ChapterBody($file)
{
// Read text file
$txt = file_get_contents($file);
// Times 12
$this->SetFont('Times','',12);
// Output justified text
$this->MultiCell(0,5,$txt);
// Line break
$this->Ln();
// Mention in italics
$this->SetFont('','I');
$this->Cell(0,5,'(end of excerpt)');
}

function PrintChapter($num, $title, $file)
{
$this->AddPage();
$this->ChapterTitle($num,$title);
$this->ChapterBody($file);
}
}

$pdf = new PDF();
$pdf->SetAuthor('OSP Classes');
$pdf->AddPage();
$x=1;
$i=0;
$sql1=mysql_query("select * from $table") or die (mysql_error());
while($result1=mysql_fetch_row($sql1))
{
$pdf->SetFont('Arial','B',14);
$pdf->MultiCell(0,10,'Ques No.'.$x .'.'. $result1[1]);
$pdf->Ln(10);
$pdf->SetFont('Arial','',14);
$pdf ->MultiCell(0,0,'Ans 1.'. $result1[2]);
$pdf->Ln(10);
$pdf->SetFont('Arial','',14);
$pdf ->MultiCell(0,0,'Ans 2.'. $result1[3]);
$pdf->Ln(10);
$pdf->SetFont('Arial','',14);
$pdf ->MultiCell(0,0,'Ans 3.'. $result1[4]);
$pdf->Ln(10);
$pdf->SetFont('Arial','',14);
$pdf ->MultiCell(0,0,'Ans 4.'. $result1[5]);
$pdf->Ln(10);
$pdf->MultiCell(0,0,'Right Ans . '. $result1[6].'           '.'Your Ans . '. $ans[$i]);
$pdf->Ln(20);
$x++;
$i++;
}
$pdf->Output();
?>

【问题讨论】:

    标签: php mysql fpdf


    【解决方案1】:

    在 FPDF MultiCell 中无法使用自动行高。正如This page 解释的那样,Multicell 为您打印的每一行文本创建一个单元格。 2e 参数 (h) 是每个单元格的高度。 (行高)。

    有更好的解决方案吗?

    是的,有。 TCPDF 的人学习了 FPDF 课程并对其进行了扩展/改进。它们支持 HTML 输出。在那里你可以使用 html 作为你的输出。也许你可以使用它。

    【讨论】:

      【解决方案2】:
      $line_height = 5;
      $width = 189;
      $text = ("Your text goes here");    
      $height = (ceil(($pdf->GetStringWidth($text) / $width)) * $line_height);
      
      $pdf->Multicell($width,$height,$text,1,1);
      

      您可以使用$line_height$width 的值来适应您的字体/边距/您需要做的任何事情,但是高度的公式将计算所需的行数并将该数字乘以高度每一行。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题并搜索了一段时间以寻求解决方案。终于得出一个比较简单的答案:

        function GetMultiCellHeight($w, $txt, $pdf)
        {
            $height = 1;
            $strlen = strlen($txt);
            $wdth = 0;
            for ($i = 0; $i <= $strlen; $i++) {
                $char = substr($txt, $i, 1);
                $wdth += $pdf->GetStringWidth($char);
                if($char == "\n"){
                    $height++;
                    $wdth = 0;
                }
                if($wdth >= $w){
                    $height++;
                    $wdth = 0;
                }
            }
            return $height;
        }
        

        【讨论】:

          【解决方案4】:

          您可以使用此代码来解决此问题。这里的概念是,只有当字符串大小大于单元格宽度时,字体大小才会减小。

          $pdf->CellFit(30,20,"This is long string message.",1,0,'C',0,'',1,0);
          

          检查此网址 how to auto adjust cell width in fpdf using php and mysql

          【讨论】:

            【解决方案5】:

            我一直在寻找复杂的解决方案,但找不到。 我最终只是在玩单元格的宽度和高度。 我发现帽子 FPDF 将使用可用空间,并根据该空间决定行之间有多少空间。 因此,考虑到我的字符串中可能包含的最大字符数,我可以计算出行之间有多少空间。 有效

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-11-28
              • 2021-11-09
              • 1970-01-01
              • 2013-12-29
              • 2013-04-15
              • 2014-02-20
              • 2017-04-07
              • 2012-11-13
              相关资源
              最近更新 更多