首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在fpdf multicell ()中自动调整行高

在fpdf multicell ()中自动调整行高
EN

Stack Overflow用户
提问于 2016-06-13 15:05:06
回答 5查看 15.3K关注 0票数 2

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

代码语言:javascript
运行
复制
<?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();
?>
EN

回答 5

Stack Overflow用户

发布于 2016-06-13 15:27:17

在FPDF MultiCell中不能自动设置线条高度。正如This页面所解释的,多单元格会为您打印的每一行文本创建一个单元格。2e参数(h)是每个单元格的高度。(线条高度)。

有没有更好的解决方案?

是的有。来自TCPDF的人已经接受了FPDF类,并对其进行了扩展/改进。它们支持HTML输出。在那里你可以使用html作为你的输出。也许你可以用这个。

票数 1
EN

Stack Overflow用户

发布于 2017-06-23 23:44:16

代码语言:javascript
运行
复制
$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的值来适应您的字体/边距/您需要做的任何事情,但是高度的公式将计算所需的行数,并将该数字乘以每行的高度。

票数 1
EN

Stack Overflow用户

发布于 2018-04-02 12:56:55

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

代码语言:javascript
运行
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37783632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档