#php #pdf #moodle
Вопрос:
Я новичок в 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();
?>
Ответ №1:
Попробуйте эту версию.
Не выводите ничего на экран, иначе pdf не будет работать, поэтому удалите заголовок $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();