【问题标题】:FPDF lib to create PDF for data with multiple pagesFPDF lib 为多页数据创建 PDF
【发布时间】:2017-01-06 19:33:47
【问题描述】:

此代码在为仅适合第一页的数据创建 PDF 时完美运行。但是,在进入第二页时,一切都变得杂乱无章,并且无法创建正确的 PDF。甚至第一页上的数据也杂乱无章。

任何人都可以帮我对此进行排序,以便第二页中的任何数据都可以生成正确的 pdf。谢谢你。

<?php
ob_start();
require('fpdf/fpdf.php');

//database connection

//Select the Products you want to show in your PDF file
$result= mysqli_query($conn, "SELECT * FROM customer");

$number_of_products = mysqli_num_rows($result);

//Initialize the  columns and the total
$column_id = "";
$column_name = "";
$column_trans_id = "";

//For each row, add the field to the corresponding column
while($row = mysqli_fetch_array($result))
{   
    $id = $row["id"];
    $name = $row["name"];
    $trans_id = substr($row["trans_id"],0,20);


    $column_id = $column_id.$id."\n";
    $column_name = $column_name.$name."\n";
    $column_trans_id = $column_trans_id.$trans_id."\n";
}
mysqli_close();

//Create a new PDF file
$pdf=new FPDF();
$pdf->AddPage();

//Fields Name position
$Y_Fields_Name_position = 20;
//Table position, under Fields Name
$Y_Table_Position = 26;

//First create each Field Name
//Gray color filling each Field Name box
$pdf->SetFillColor(232,232,232);
//Bold Font for Field Name
$pdf->SetFont('Arial','B',8);
$pdf->SetY($Y_Fields_Name_position);
$pdf->SetX(0);
$pdf->Cell(10,6,'ID',1,0,'L',1);
$pdf->SetX(10);
$pdf->Cell(30,6,'NAME',1,0,'L',1);
$pdf->SetX(40);
$pdf->Cell(20,6,'TRASACTION ID',1,0,'L',1);
$pdf->SetX(60);

$pdf->Ln();

//Now show the  columns
$pdf->SetFont('Arial','',8);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(0);
$pdf->MultiCell(10,6,$column_id,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(10);
$pdf->MultiCell(30,6,$column_name,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(40);
$pdf->MultiCell(20,6,$column_trans_id,1);
$pdf->SetY($Y_Table_Position);
$pdf->SetX(60);

//Create lines (boxes) for each ROW (Product)
//If you don't use the following code, you don't create the lines separating each row
$i = 0;
$pdf->SetY($Y_Table_Position);
while ($i < $number_of_products)
{
    $pdf->SetX(0);
    $pdf->MultiCell(60,6,'',1);
    $i = $i +1;
}
ob_end_clean();
$pdf->Output();
ob_end_flush();
?>

【问题讨论】:

    标签: php pdf-generation fpdf


    【解决方案1】:

    您的代码的问题是您将数据库中的所有结果行放入多行字符串中,并且每列只创建一个单元格,这很糟糕。单元格可以延伸到多于一页。添加单元格时,它将从前一个单元格扩展到的页面开始。虽然可以返回到表格开始的页面,但这不是在 FPDF 中输出表格的首选方式。

    一种更常见的方法是为从数据库中获得的每个结果行添加一组单元格。这为您节省了绘制单元格边框的第二次迭代,以及大量的重新定位。

    <?php
    require('fpdf/fpdf.php');
    
    // Connect to database...
    
    // Define your columns like so:
    $columns = array(array("name" => "id",             "width" => 10),
                     array("name" => "name",           "width" => 30),
                     array("name" => "transaction_id", "width" => 20));
    
    $pdf = new FPDF();
    $pdf->AddPage();
    
    // Table header
    $pdf->SetFillColor(232, 232, 232);
    $pdf->SetFont('Arial', 'B', 8);
    foreach ($columns as $column)
    {
        $pdf->Cell($column['width'], 6, strtoupper($column['name']), 1, 0, 'L', 1);
    }
    $pdf->Ln();
    
    // Table rows
    $pdf->SetFont('Arial', '', 8);
    $result = mysqli_query($conn, "SELECT id, name, transaction_id FROM customers");
    while ($row = mysqli_fetch_assoc($result))
    {
        foreach ($columns as $column)
        {
            $pdf->Cell($column['width'], 6, $row[$column['name']], 1);
        }
        $pdf->Ln();
    }
    
    // Clean up
    mysqli_free_result($result);
    mysqli_close($conn);
    
    $pdf->Output();
    

    【讨论】:

    • 谢谢!你救了我一场噩梦!像魅力一样工作。
    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 2021-10-16
    • 1970-01-01
    • 2013-05-24
    • 2017-03-04
    • 1970-01-01
    • 2013-11-12
    • 2022-11-10
    相关资源
    最近更新 更多