行高自动调整fpdf multicell()

问题描述 投票:2回答:3

我正在使用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
3个回答
1
投票

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

有更好的解决方案吗?

就在这里。来自TCPDF的人们参加了FPDF课程并对其进行了扩展/改进。它们支持HTML输出。在那里你可以使用html输出。也许你可以使用它。


1
投票
$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的值来适应您的字体/边距/您需要做的任何事情,但高度公式将计算所需的行数并将该数字乘以每行的高度。


0
投票

我遇到了同样的问题,并寻找了一段时间来寻求解决方案。最后得出一个相对简单的答案:

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.