在 php 中使用 fpdf 创建带有包装数据的 pdf 表格

发布于 2024-07-26 05:29:17 字数 3474 浏览 3 评论 0原文

我在使用 fpdf 库创建 pdf 中的表结构时遇到问题。 当特定单元格的任何数据具有长字符串时,该单元格数据将与其他单元格数据重叠。 所以整个表的数据已经乱了。 那么 pdf 中的数据将看起来不正确。

任何人请帮我在 pdf 中创建表格,根据单元格中的数据自动调整所有单元格。

    <?php
if($_POST['cmbReportType'] == '1')
{
$fromdate = date_format(date_create($_POST['txtFromDate']), 'd-M-y');
$import = ociparse($c, "SELECT 
                            t2.membercardno, (t1.firstname||' '||t1.middlename||' '||t1.lastname) as fullname,
                            t3.description,
                            to_char(t2.startdate,'DD-MON-YY') startdate,
                            to_char(t2.expirydate,'DD-MON-YY') expirydate,
                            t2.ramount,
                            t1.address1,
                            t1.address2
                        FROM 
                            useradmin t1, 
                            userplan t2, 
                            plan t3
                        WHERE 
                            t1.memberid = t2.memberid
                        AND 
                            t2.planid = t3.planid
                        AND 
                            t1.branchid = 3
                        AND 
                            t2.startdate >= '$fromdate'
                        ORDER BY t2.membercardno");


OCIExecute($import);
$k=0;
while(OCIFetch($import))
{
    $a[$k]['membercardno'] = ociresult($import,"MEMBERCARDNO");
    $a[$k]['fullname']   = ociresult($import,"FULLNAME");
    $a[$k]['description'] = ociresult($import,"DESCRIPTION");
    $a[$k]['startdate']  = ociresult($import, "STARTDATE");
    $a[$k]['expirydate'] = ociresult($import, "EXPIRYDATE");
    $a[$k]['ramount']  =  ociresult($import, "RAMOUNT");
    $a[$k]['address1'] = ociresult($import, "ADDRESS1");
    $a[$k]['address2'] = ociresult($import, "ADDRESS2");    
    $k++;
}
$resultcount = count($a);

elseif($_POST['rdbReportFormat'] == 'pdf')
{
/***This report view in tabular format.****/
    $pdf=new FPDF();

$pdf->AddPage();
$reportdate = date('d-m-Y');
$filename = $reportdate.'_report.pdf';
$pdf->SetFillColor(255,0,0);
$pdf->SetTextColor(255);
$pdf->SetDrawColor(128,0,0);
$pdf->SetLineWidth(.3);
$pdf->SetFont('Arial', 'B', 6);
// Header
$header=array('Member Card No','Full Name','Description', 'Start Date', 'Expiry Date', 'ramount', 'Address1', 'Address2');
$w = array(25, 35, 35, 15, 18, 15, 30, 30);
for($i=0;$i<count($header); $i++)
    $pdf->Cell($w[$i],7, $header[$i], 1, 0, 'L', true);

$pdf->Ln();

// Reset colour set for data 
$pdf->SetFillColor(224,235,255);
$pdf->SetTextColor(0);
$pdf->SetFont('courier','',7);
$fill=false;
for($i=0;$i<$resultcount;$i++)
{
    $height =6;
    $pdf->Cell($w[0], '$height', $a[$i]['membercardno'], '1', '0', 'L', $fill);
    $pdf->Cell($w[1], '$height', $a[$i]['fullname'], '1', '0', 'L', $fill);
    $pdf->Cell($w[2], '$height', $a[$i]['description'], '1', '0', 'L', $fill);
    $pdf->Cell($w[3], '$height', $a[$i]['startdate'], '1', '0', 'L', $fill);
    $pdf->Cell($w[4], '$height', $a[$i]['expirydate'], '1', '0', 'L', $fill);
    $pdf->Cell($w[5], '$height', $a[$i]['ramount'], '1', '0', 'L', $fill);
    $pdf->Cell($w[5], '$height', $a[$i]['address1'], '1', '0', 'L', $fill);
    $pdf->Cell($w[5], '$height', $a[$i]['address2'], '1', '0', 'L', $fill);
    $pdf->Ln();
    $fill = !$fill;

}   
$pdf->Cell(array_sum($w),0,'','T');
$pdf->Output($filename, 'I');
}
}   
?>

I have a problem in creating table structure in pdf using fpdf library. when any data of perticular cell have a long string then the cell data will overlap with other cell data.
So whole table data has been disordered. then data in pdf will not seem correctly.

any one please help me to create table in pdf auto adjust all cells according to data in cell.

    <?php
if($_POST['cmbReportType'] == '1')
{
$fromdate = date_format(date_create($_POST['txtFromDate']), 'd-M-y');
$import = ociparse($c, "SELECT 
                            t2.membercardno, (t1.firstname||' '||t1.middlename||' '||t1.lastname) as fullname,
                            t3.description,
                            to_char(t2.startdate,'DD-MON-YY') startdate,
                            to_char(t2.expirydate,'DD-MON-YY') expirydate,
                            t2.ramount,
                            t1.address1,
                            t1.address2
                        FROM 
                            useradmin t1, 
                            userplan t2, 
                            plan t3
                        WHERE 
                            t1.memberid = t2.memberid
                        AND 
                            t2.planid = t3.planid
                        AND 
                            t1.branchid = 3
                        AND 
                            t2.startdate >= '$fromdate'
                        ORDER BY t2.membercardno");


OCIExecute($import);
$k=0;
while(OCIFetch($import))
{
    $a[$k]['membercardno'] = ociresult($import,"MEMBERCARDNO");
    $a[$k]['fullname']   = ociresult($import,"FULLNAME");
    $a[$k]['description'] = ociresult($import,"DESCRIPTION");
    $a[$k]['startdate']  = ociresult($import, "STARTDATE");
    $a[$k]['expirydate'] = ociresult($import, "EXPIRYDATE");
    $a[$k]['ramount']  =  ociresult($import, "RAMOUNT");
    $a[$k]['address1'] = ociresult($import, "ADDRESS1");
    $a[$k]['address2'] = ociresult($import, "ADDRESS2");    
    $k++;
}
$resultcount = count($a);

elseif($_POST['rdbReportFormat'] == 'pdf')
{
/***This report view in tabular format.****/
    $pdf=new FPDF();

$pdf->AddPage();
$reportdate = date('d-m-Y');
$filename = $reportdate.'_report.pdf';
$pdf->SetFillColor(255,0,0);
$pdf->SetTextColor(255);
$pdf->SetDrawColor(128,0,0);
$pdf->SetLineWidth(.3);
$pdf->SetFont('Arial', 'B', 6);
// Header
$header=array('Member Card No','Full Name','Description', 'Start Date', 'Expiry Date', 'ramount', 'Address1', 'Address2');
$w = array(25, 35, 35, 15, 18, 15, 30, 30);
for($i=0;$i<count($header); $i++)
    $pdf->Cell($w[$i],7, $header[$i], 1, 0, 'L', true);

$pdf->Ln();

// Reset colour set for data 
$pdf->SetFillColor(224,235,255);
$pdf->SetTextColor(0);
$pdf->SetFont('courier','',7);
$fill=false;
for($i=0;$i<$resultcount;$i++)
{
    $height =6;
    $pdf->Cell($w[0], '$height', $a[$i]['membercardno'], '1', '0', 'L', $fill);
    $pdf->Cell($w[1], '$height', $a[$i]['fullname'], '1', '0', 'L', $fill);
    $pdf->Cell($w[2], '$height', $a[$i]['description'], '1', '0', 'L', $fill);
    $pdf->Cell($w[3], '$height', $a[$i]['startdate'], '1', '0', 'L', $fill);
    $pdf->Cell($w[4], '$height', $a[$i]['expirydate'], '1', '0', 'L', $fill);
    $pdf->Cell($w[5], '$height', $a[$i]['ramount'], '1', '0', 'L', $fill);
    $pdf->Cell($w[5], '$height', $a[$i]['address1'], '1', '0', 'L', $fill);
    $pdf->Cell($w[5], '$height', $a[$i]['address2'], '1', '0', 'L', $fill);
    $pdf->Ln();
    $fill = !$fill;

}   
$pdf->Cell(array_sum($w),0,'','T');
$pdf->Output($filename, 'I');
}
}   
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆里有你的影子 2024-08-02 05:29:18

尝试使用 multicell() 编写问题字段(即描述 - 您无法预测其最大长度的字段)。 这会将它们包装在多行中。

对于每个多单元格字段,您将需要使用 nbLines 来查看它是否要换行并计算下一行位置。 您还需要执行 setXY() 来设置下一个单元格的开始。

这是nbLines(在这里找到:http ://www.svn.churchtool.org/viewvc/trunk/fpdf/mc_table.php?revision=1&view=markup):

function NbLines($w,$txt) {
//Computes the number of lines a MultiCell of width w will take
$cw=&$this->CurrentFont['cw'];
if($w==0)
    $w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
$s=str_replace("\r",'',$txt);
$nb=strlen($s);
if($nb>0 and $s[$nb-1]=="\n")
    $nb--;
$sep=-1;
$i=0;
$j=0;
$l=0;
$nl=1;
while($i<$nb)
{
    $c=$s[$i];
    if($c=="\n")
    {
        $i++;
        $sep=-1;
        $j=$i;
        $l=0;
        $nl++;
        continue;
    }
    if($c==' ')
        $sep=$i;
    $l+=$cw[$c];
    if($l>$wmax)
    {
        if($sep==-1)
        {
            if($i==$j)
                $i++;
        }
        else
            $i=$sep+1;
        $sep=-1;
        $j=$i;
        $l=0;
        $nl++;
    }
    else
        $i++;
}
return $nl;
}

Try writing problem fields (i.e. description - ones you cannot predict a max length for) using multicell(). This will wrap them on multiple lines.

For each multicell field you will need to use nbLines to see if it is going to wrap and calculate the next line position. you will also need to do a setXY() to set the start of the next cell.

Here is nbLines (found here: http://www.svn.churchtool.org/viewvc/trunk/fpdf/mc_table.php?revision=1&view=markup):

function NbLines($w,$txt) {
//Computes the number of lines a MultiCell of width w will take
$cw=&$this->CurrentFont['cw'];
if($w==0)
    $w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
$s=str_replace("\r",'',$txt);
$nb=strlen($s);
if($nb>0 and $s[$nb-1]=="\n")
    $nb--;
$sep=-1;
$i=0;
$j=0;
$l=0;
$nl=1;
while($i<$nb)
{
    $c=$s[$i];
    if($c=="\n")
    {
        $i++;
        $sep=-1;
        $j=$i;
        $l=0;
        $nl++;
        continue;
    }
    if($c==' ')
        $sep=$i;
    $l+=$cw[$c];
    if($l>$wmax)
    {
        if($sep==-1)
        {
            if($i==$j)
                $i++;
        }
        else
            $i=$sep+1;
        $sep=-1;
        $j=$i;
        $l=0;
        $nl++;
    }
    else
        $i++;
}
return $nl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文