dongyi6183 2018-06-03 08:20
浏览 60

如何使用php和mysql在fpdf中动态创建多个id卡

I was trying to create id cards using fpdf in which the data to be plased in the id card is coming from mysql database and i have successfully made it the problem is that i want to generate many id card like 10 id cards once and when i try the blow code it is generating the id cards but placing one on the other and i can only see the last id card:

A)so, how could i give them diffrent position for the id cards dynamically? below is my code and image showing the output: 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();
    }
    }

    ?>
  • 写回答

1条回答 默认 最新

  • dongtan2017 2018-06-03 09:02
    关注

    The problem is you are setting the ID's on top of each other, without an offset for each one or a new page per ID.

    If you want an ID per page you need to call $pdf->AddPage(); after making each ID. This will create a new page and set the XY to the top left of the page.

    If you want multiple ID cards per page, for example X amount per page, split the array of id cards so you are looping over X amount at a time, the PHP function array_chunk would split it for you, and then just offset the XY coordinates by the amount you want to, so for example I did this

    $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();
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?