如何在单击按钮时生成多个pdf文件

时间:2015-04-27 13:42:39

标签: php pdf fpdf

我正在为教师创建一个组件,教师可以为所有完成课程的学生生成pdf。 应该生成检查所有学生和pdf并将其保存在磁盘上。之后提供下载链接以下载生成的所有pdfs的zip。这就是我想要实现的目标。我使用fpdf生成pdf。 有什么建议吗?

以下是发布的表单和学生ID

    <form
        action="<?php echo JRoute::_('index.php?option=com_mentor&view=download_certificate&cid=' . $cid . '&Itemid=529') ?>"
        name="download_certificate" method="post" id="download_certificate">
        <table class="adminlist" border="1" cellpadding="0" cellspacing="0"
               style="table-layout: fixed" id="content">
            <thead>
            <tr>
                <th class="nowrap" style="width: 35px">
                    <input type="checkbox" name="selectall" id="selectall">
                </th>
                <th class="nowrap" align="center">
                    <?php echo JText::_('COM_MENTOR_USER_NAME'); ?>
                </th>
                <th class="nowrap" style="width: 140px">
                    <?php echo JText::_('COM_MENTOR_COURSE_STATUS'); ?>
                </th>
                <th class="nowrap" style="width: 140px">
                    <?php echo JText::_('COM_MENTOR_ENROLLMENT_DATE'); ?>
                </th>
                <th class="nowrap" style="width: 140px">
                    <?php echo JText::_('COM_MENTOR_ACTIVITY'); ?>
                </th>
                <th class="nowrap" style="width: 50px">
                    <?php echo JText::_('COM_MENTOR_SCORE'); ?>
                </th>
                <th class="nowrap" style="width: 50px">
                    <?php echo JText::_('COM_MENTOR_RESULT'); ?>
                </th>
            </tr>
            </thead>
            <tbody>
            <?php
            //echo '<pre>';print_r($this->mentor_details); die;
            foreach ($this->mentor_details as $students) {
                $cid = $this->mentor_details['cid'];
                $i = 1;
                foreach ($students['students'] as $student) {
                    $userid = $student['id'];
//                echo '<pre>';
//                print_r($student);
//                die;
                    ?>
                    <tr class="status" id="<?php echo $userid ?>">
                        <td align="center">
                            <input type="checkbox" id="<?php echo $userid ?>" name="check[]" class="checkbox1"
                                   value="<?php echo $userid ?>">
                        </td>
                        <td>
                            <a href="<?php echo JRoute::_('index.php?option=com_mentor&view=grader&cid=' . $cid . '&uid='
                                . $userid . $itemid) ?>">
                                <?php echo $student['username']; ?>
                            </a>
                        </td>
                        <!--                    <td>
                        <?php// echo $student['email']; ?>
                    </td> -->
                        <td align="center">
                            <?php
                            $incomplete = $completed = $not_started = 0;
                            for ($k = 0; $k < count($student['elements']); $k++) {
                                foreach ($student['elements'] as $elements) {
                                    if ($elements['userid'] == $userid) {
//                                    echo '<pre>';print_r($elements); die;
                                        if ($elements['element']['cmi.core.lesson_status'] == 'incomplete') {
                                            $incomplete++;
                                        } else {
                                            $completed++;
                                        }
                                    }
                                }
                            }
                            if ($incomplete == 0 && $completed == 0) {
                                echo 'Not yet started';
                            } else {
                                if ($completed == count($student['elements'])) {
                                    echo 'Completed';
                                } else {
                                    echo 'Incomplete';
                                }
                            }
                            ?>
                        </td>
                        <td align="center">
                            <?php
                            if (!empty($student['timestart'])) {
                                $date = date('d-m-Y H:i', $student['timestart']);
                                echo $date;
                            } else {
                                echo "Not yet started";
                            } ?>
                        </td>
                        <td align="center">
                            <?php
                            if (!empty($student['activity']['lasttime']) && (!empty($student['activity']['starttime']))) {
                                $start_date = date('d-m-Y H:i', $student['activity']['starttime']);
                                $last_date = date('d-m-Y H:i', $student['activity']['lasttime']);
                                echo $start_date . '<br/>' . $last_date;
                            } else {
                                echo "-";
                            } ?>
                        </td>
                        <td align="center">
                            <?php
                            $grades = $student['grades'];
                            $total_grade = array();
                            $j = 0;
                            //for ($j = 0; $j < count($grades); $j++) {
                            //    $total_grade[$j] = $grades[$j]['finalgrade'];
                            //}
                            //print_r($total_grade);die;
                            if (!empty($grades)) {
                                //echo number_format(array_sum($total_grade), 2);
                                $total_grade[$j] = $grades[$j]['finalgrade'];
                                echo number_format($total_grade[$j], 2);
                            } else {
                                echo '-';
                            }
                            //echo '<pre>';
                            //print_r($student['grades']);
                            //die;
                            ?>
                        </td>
                        <td align="center">
                            <?php
                            //echo '<pre>';print_r($student);die;
                            if (!empty($student['scores'])) {
                                if (isset($grades[$j]['feedbacktext'])) {
                                    echo $grades[$j]['feedbacktext'];
                                } else {
                                    echo '-';
                                }
                            } else {
                                echo '-';
                            }
                            ?>
                        </td>
                    </tr>
                    <?php $i++;
                }
            } ?>
            </tbody>
        </table>
    </form>
<script>
function checked_value() {
        var checkedValue = [];
        var $len = $(".checkbox1:checked").length;

        if ($len == 0) {
            alert('Please select user');
        }
//        else if ($len > 1) {
//            alert('Please select a single user only.');
//        }
        else {
            $(".checkbox1").each(function () {
                var $this = $(this);
                if ($this.is(":checked")) {
                    checkedValue.push($this.attr("id"));
                }
            });

            $("#download_certificate").submit();
    </script>

在点击图片标签上,表单会与学生ID一起提交,我会收到学生数据,他的姓名,成绩,课程,

<img src="/components/com_mentor/images/certificate_blue.png" class="certificate-ico right"
             title="Download Certificate" onclick="checked_value();"/>

完成此处理后,页面将重定向到pdf.php页面

require_once('/wamp/opt/bitnami/apache2/htdocs/lms/lib/fpdf/fpdf.php');
$pdf = new FPDF(); $pdf->SetFont('times', '', 12);
$pdf->SetTextColor(50, 60, 100); $pdf->AddPage('L');
$pdf->SetDisplayMode(real, 'default'); $pdf->SetXY(10, 60);
$pdf->SetFontSize(12);
$pdf->Write(5, 'Dear Ms.XYX');
$filename = "test.pdf";
$dir = "/assets/";
$pdf->Output($dir . $filename, 'F');

1 个答案:

答案 0 :(得分:0)

谢谢你的帮助..解决了我的问题。 通过pdf函数循环n否。用户。