簡體   English   中英

無法將數據從數據庫轉換為 pdf

[英]not being able to convert data from database to pdf

我是 moodle 的新手 我正在嘗試從數據庫中獲取數據並將其轉換為 Pdf 文件,但 pdf 文件為空,它不顯示數據庫元素。 我不明白我做錯了什么,你能幫我嗎?

這是帶有觸發進程的按鈕的文件

              // lib.php
              <?php
              /**
               * @package     tool_report
               * @author      Kristian
               * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
               */

               require_once(__DIR__ . '/../../../config.php');
               defined('MOODLE_INTERNAL') || die();

               $PAGE->set_url(new moodle_url('/admin/tool/report/lib.php'));
               $PAGE->set_context(\context_system::instance());
               $PAGE->set_title(get_string('manage_reports', 'tool_report'));

               //what level of the site where are
               echo $OUTPUT->header();

               ?>
               <style media="screen">
                .btn {
                      height: 30px;
                      width: 60px;
                      background-color: red;
                 }
               </style>

              <form action="manage.php" method="POST">
                 <button class="btn" type="submit" name="button">PDF</button> 
              </form>

              <?php
              echo $OUTPUT->footer();

這是包含邏輯的文件:

              //manage.php

              <?php
              /**
               * @package     tool_report
               * @author      Kristian
               * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
               */
               require('fpdf/fpdf.php');
               ob_start(); //
               require_once(__DIR__ . '/../../../config.php');
               defined('MOODLE_INTERNAL') || die();

               $PAGE->set_url(new moodle_url('/admin/tool/report/manage.php'));
               $PAGE->set_context(\context_system::instance());
               $PAGE->set_title(get_string('manage_reports', 'tool_report'));

               //what level of the site where are

               global $DB;

               echo $OUTPUT->header();

               $sql = " SELECT *
                        FROM mdl_bigbluebuttonbn
                       ";
               $record = $DB->get_record_sql($sql);
               //print_r($record);
               $pdf = new FPDF();
               $pdf->AddPage();
               $pdf->SetFont('Courier','B',16);
               $pdf->Cell(50,10,'Course ID', '1', '0', 'C');
               $pdf->Cell(50,10,'Course TYPE', '1', '0', 'C');
               $pdf->Cell(50,10,'Course COURSE', '1', '0', 'C');
               $pdf->Cell(50,10,'Course NAME', '1', '0', 'C');
               ob_end_clean();
               $pdf->Output();
               ?><?php
               while ($row = mysqli_fetch_assoc($record)) {
                   print_r($row); die();
               ?>
               <tr>
                  <td><?php echo $row['id'] ?></td>
                  <td><?php echo $row['type'] ?></td>
                  <td><?php echo $row['course'] ?></td>
                  <td><?php echo $row['name'] ?></td>
               </tr>
               <?php
               }

                echo $OUTPUT->footer();
               ?>

試試這個版本。

不要在屏幕上顯示 output 任何內容,否則 pdf 將無法工作,因此刪除 $OUTPUT->header 等。

Output 最后是pdf

/**
 * @package     tool_report
 * @author      Kristian
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once(__DIR__ . '/../../../config.php');
defined('MOODLE_INTERNAL') || die();

$PAGE->set_url(new moodle_url('/admin/tool/report/manage.php'));
$PAGE->set_context(\context_system::instance());
$PAGE->set_title(get_string('manage_reports', 'tool_report'));

// No need to use SQL.
$records = $DB->get_records('bigbluebuttonbn');

// Create an html table.
$table = new html_table();
$table->head  = array(
    get_string('courseid', 'tool_report'),
    get_string('coursetype', 'tool_report'),
    get_string('coursename', 'tool_report'),
    get_string('coursecourse', 'tool_report'),
 );

$table->data  = array();

foreach ($records as $record) {
    $row = array();
    $row[] = $record->id;
    $row[] = $record->type;
    $row[] = $record->name;
    $row[] = $record->course;

    $table->data[] = $row;
}

// Render the table.
$report = html_writer::table($table);

$pdf = new pdf();
$pdf->SetFont('Courier','B',16);

// Add the html.
$pdf->writeHTML($report);

// Output at the end.
$pdf->Output();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM