在FPDF内部构建多单元

时间:2017-03-15 09:35:12

标签: fpdf

我从数据库表中收到了名字:

ADEEB (staff) , ALISTER (staff), AMER (staff), AMIRABUBAKAR (staff), AMIRULHAFIZ (staff),
ANA (staff), ANDIKA (staff), ANDREW (staff), ARINA (staff), DIANADEWI (staff), DINA (staff),
DINIE (staff), DUDU (staff), DUNCAN (staff), ELIZABETH (staff), ERIC (staff), ERIN (staff), 
FAUZE(staff), FAZLY (staff),FELIZ (staff), HAYKAL (staff), IVANPINTO (staff), IVORY (staff), 
IVY (staff)

我想使用multicell创建pdf文件并在fpdf中打印这样的名称:

function Mach($header, $header1, $lbl3)
{
    $this->Line(10,$this->GetY(),200,$this->GetY());
    //Times 12
    $this->SetFont('Times','B',12);
    //Background color
    $this->SetFillColor(255,255,255);
    //Title
    $this->Cell(50,10,"$header1",0,1,'L');
    $this->SetFont('Times','',12);
    $this->MultiCell(185,10,"$lbl3",1,1,'L');

}

$con = mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
for($y=0;$y<$len;$y++)
{   
    $column_procStaff = '';

    $result6 = getProcessStaff($projectNo,$proc[$y]);
        if (!$result6) {
            die('Invalid query: ' . mysqli_error($con));
        }   
        $c=0;
        while($row6 = mysqli_fetch_array($result6))
        {  
            $procStaff = $row6['proc_staff'];
            if($c==0)
            {
                $column_procStaff .= $procStaff." (staff) ";
            } 
            else
            {
                $column_procStaff .= ", ".$procStaff." (staff)";
            }   
            $c++;
        }       
    $pdf->Mach("Process Name: $proc[$y]", $column_procStaff);
}

我也想要结构收到的名字。有没有办法像这样显示smth:

ADEEB (staff),          ALISTER (staff),            AMER (staff),
AMIRABUBAKAR (staff),   AMIRULHAFIZ (staff),        ANA (staff), 
ANDIKA (staff),         ANDREW (staff),             ARINA (staff), 
DIANADEWI (staff),      DINA (staff),               DINIE (staff), 
DUDU (staff),           DUNCAN (staff),             ELIZABETH (staff),
ERIC (staff),           ERIN (staff),               FAUZE(staff), 
FAZLY (staff),          FELIZ (staff),              HAYKAL (staff),
IVANPINTO (staff),      IVORY (staff),              IVY (staff) 

1 个答案:

答案 0 :(得分:2)

以下是使用fPDF以您希望的方式编写名称的代码段:

// Your data
$names= array("ADEEB (staff)", "ALISTER (staff)" ,"AMER (staff)" ,"AMIRABUBAKAR (staff)" ,"AMIRULHAFIZ (staff)", "ANA (staff)" ,"ANDIKA (staff)" ,"ANDREW (staff)" ,"ARINA (staff)" ,"DIANADEWI (staff)" ,"DINA (staff)", "DINIE (staff)" ,"DUDU (staff)" ,"DUNCAN (staff)" ,"ELIZABETH (staff)" ,"ERIC (staff)" ,"ERIN (staff)", "FAUZE(staff)" ,"FAZLY (staff),FELIZ (staff)" ,"HAYKAL (staff)" ,"IVANPINTO (staff)" ,"IVORY (staff)", "IVY (staff)");
// Number of column wanted
$nbColumn = 3;

// Computing width of columns
$maxWidth = 0;
foreach ($names as $n){
    $w = $pdf->GetStringWidth($n);
    if($maxWidth<$w){
        $maxWidth=$w;
    }
}
// add Margin
$maxWidth = $maxWidth + 10;

foreach ($names as $index => $n){
    // width, height, text, no norder, going to next line
    $pdf->Cell($maxWidth, 5, $n, 0, $index%$nbColumn==$nbColumn-1);
}