【问题标题】:how to create multiple id cards dynamically in fpdf using php and mysql如何使用php和mysql在fpdf中动态创建多个身份证
【发布时间】:2018-06-03 08:20:46
【问题描述】:

我试图使用 fpdf 创建身份证,其中要放入身份证中的数据来自 mysql 数据库,我已经成功地做到了,问题是我想生成许多身份证,比如 10 张身份证当我尝试使用打击代码时,它正在生成身份证,但将一张放在另一张上,我只能看到最后一张身份证:

A) 那么,我怎样才能动态地为他们提供不同的身份证位置? 下面是我的代码和显示输出的图像: output image

  <?php
         include('connect.php'); 
         require_once('fpdf/fpdf.php');

    if(ISSET($_POST['generate_id'])){   
            $semsec = $_POST['id']; 
    $result=$conn->query("SELECT * FROM `student` WHERE `semsec` = '$semsec'") or die(mysqli_error($conn));
    if ($result->num_rows == 0) {
        // output data of each row
     echo '
                    <script type = "text/javascript">
                    alert("Student Not Found For The Provided Semister and Section");
                        window.location = "home.php";
                    </script>
                ';
    } else {

    while($row=mysqli_fetch_array($result))
    {
    $cls[]=$row;
    }
    $json=json_encode($cls);
    $obj = json_decode($json,true);
    class PDF extends FPDF
    {

    }
        $pdf = new PDF();
        $pdf->AddPage();

        foreach($obj as $item) {

        $name=$item['firstname'];
        $lname=$item['lastname'];
        $id=$item['student_no'];
        $semsec=$item['semsec'];
        $profile=$item['image'];
        $qr=$item['barcode'];

            $pdf->Image('images/background2.jpg', 10, 10,100, 50);
            $pdf->Image($profile, 80, 15,25, 30);
            $pdf->Image($qr, 15, 45,20, 15);
            $pdf->AddFont('courier','','courier.php');  
            $pdf->SetFont('courier','b',10);
            $pdf->SetXY(33, 22.8);
            $pdf->SetFont('Arial','B',10);
            $pdf->Cell(9.5,7,$name,0,4,'L');
            $pdf->SetXY(33, 28);
            $pdf->SetFont('Arial','B',10);
            $pdf->Cell(9.5,7,$lname,0,4,'L');
            $pdf->SetXY(33, 33.5);
            $pdf->SetFont('Arial','B',10);
            $pdf->Cell(9.5,7,$id,0,4,'L');
            $pdf->SetXY(33, 39);
            $pdf->SetFont('Arial','B',10);
            $pdf->Cell(9.5,7,$semsec,0,4,'L');

            }
    $pdf->Output();
    }
    }

    ?>

【问题讨论】:

  • 请参阅php/MySQL中的prepared statements

标签: php mysql fpdf


【解决方案1】:

问题是您将 ID 设置在彼此之上,没有为每个 ID 设置偏移量,也没有为每个 ID 设置一个新页面。

如果您希望每页都有一个 ID,您需要在创建每个 ID 后致电$pdf-&gt;AddPage();。 这将创建一个新页面并将 XY 设置为页面的左上角。

如果您想要每页多张身份证,例如每页 X 数量,拆分身份证数组以便您一次循环 X 数量,PHP 函数 array_chunk 将为您拆分它,然后只需将 XY 坐标偏移您想要的量,例如我这样做了

$pdf = new PDF();
$pdf->AddPage();

$input_array = array('a', 'b', 'c', 'd', 'e');

$farray = array_chunk($input_array, 2);
foreach($farray as $obj) {
    $yOffset = 0;
    foreach($obj as $item) {
        $pdf->SetXY(33, 28 + $yOffset);
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(9.5, 7, $item, 0, 4, 'L');
        $yOffset += 40; // Y distance between letters
    }
    $pdf->AddPage();
}
$pdf->Output();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2018-12-07
    • 2019-06-20
    • 2013-01-27
    相关资源
    最近更新 更多