【问题标题】:FPDF multicell table issue?FPDF 多单元格表问题?
【发布时间】:2015-01-13 18:37:40
【问题描述】:

我遇到了自动输入多单元 FPDF 的问题

$pdf=new PDF('P','cm','A4');
$pdf->Open();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetMargins(1.5,1,1.5);
$pdf->SetFont('Times','',12);

$x=$pdf->GetY();
$pdf->SetY($x+1);
$pdf->SetFont('Times','B',12);
$pdf->Cell(1,0.5,'#',1,0,'L');
$pdf->Cell(4,0.5,'Name',1,0,'L');
$pdf->Cell(4,0.5,'Blabla',1,0,'L');
$pdf->Cell(4,0.5,'Blabla',1,0,'L');

//query dan arraying
$sql   ="SELECT * FROM `tb_unknown`";
$query = mysql_query( $sql );
$i = 1;

while( $result= mysql_fetch_array( $query )){
   $pdf->Ln();
   $name = $result['name'];
   $blabla1 = $result['blabla1'];
   $blabla2 = $result['blabla2'];
   $pdf->SetFont('Times','',12);
   $pdf->MultiCell(1, 0.5, $i, 1, 'L');
   $pdf->MultiCell(4, 0.5, $name, 1, 'L');
   $pdf->MultiCell(4, 0.5, $blabla1, 1, 'L');
   $pdf->MultiCell(4, 0.5, $blabla2, 1, 'L');
   $i++;
}
$pdf->Output("report.pdf", "I");

这是我的结果....

太难看了,表错误对齐位置。 有人可以帮我修一下吗?...

【问题讨论】:

  • 嗨!我目前正面临这个问题。我想知道你是否已经修好了你的?如果是,那你介意告诉我你做了什么吗?谢谢。

标签: fpdf


【解决方案1】:

这个问题困扰了我一段时间,我在link 上找到了解决方案。

这背后的想法是在打印多单元格之前获取 X 和 Y 坐标,并在打印后立即使用 SetXY 函数设置两个坐标。

您还必须确定行中最高单元格的高度,然后在每行末尾使用 SetY 移动到适当的 Y 坐标。

您的代码应如下所示:

$name = $result['name'];
$blabla1 = $result['blabla1'];
$blabla2 = $result['blabla2'];
$pdf->SetFont('Times','',12);

while( $result= mysql_fetch_array( $query )) {  
    $current_y = $pdf->GetY();
    $current_x = $pdf->GetX();
    $pdf->MultiCell(1, 0.5, $i, 1, 'L');
    $end_y = $pdf->GetY();

    $current_x = $current_x + 1;
    $pdf->SetXY($current_x, $current_y);
    $pdf->MultiCell(4, 0.5, $name, 1, 'L');
    $end_y = ($pdf->GetY() > $end_y)?$pdf->GetY() : $end_y;

    $current_x = $current_x + 4;
    $pdf->SetXY($current_x, $current_y); 
    $pdf->MultiCell(4, 0.5, $blabla1, 1, 'L');
    $end_y = ($pdf->GetY() > $end_y)?$pdf->GetY() : $end_y;

    $current_x = $current_x + 4;
    $pdf->SetXY($current_x, $current_y);
    $pdf->MultiCell(4, 0.5, $blabla2, 1, 'L');
    $end_y = ($pdf->GetY() > $end_y)?$pdf->GetY() : $end_y;
    $i++;
    $pdf->SetY($end_y);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    相关资源
    最近更新 更多