-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_pdf.php
More file actions
103 lines (91 loc) · 4.49 KB
/
Copy pathexport_pdf.php
File metadata and controls
103 lines (91 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
require_once("includes/auth.php");
require_role(['admin', 'staff']);
include("config.php");
require_once("includes/export_data.php");
require_once __DIR__ . '/vendor/autoload.php';
$filters = [
'internship_status' => $_GET['internship_status'] ?? '',
'year_level' => $_GET['year_level'] ?? '',
'semester' => $_GET['semester'] ?? '',
'academic_year' => $_GET['academic_year'] ?? '',
];
$rows = build_export_rows($conn, $filters);
$generatedAt = date('d/m/Y H:i');
$generatedBy = htmlspecialchars($_SESSION['full_name'] ?? '-');
$semesterLabel = $filters['semester'] !== '' ? htmlspecialchars($filters['semester']) : 'ทั้งหมด';
$yearLabel = $filters['academic_year'] !== '' ? htmlspecialchars($filters['academic_year']) : 'ทั้งหมด';
function td($v) { return htmlspecialchars((string) $v); }
$internshipRowsHtml = '';
$n = 1;
foreach ($rows as $r) {
if ($r['company_name'] === '-') { continue; } // ยังไม่มีข้อมูลฝึกงาน ไม่ต้องใส่ในตารางนี้
$internshipRowsHtml .= '<tr>'
. '<td>' . $n++ . '</td>'
. '<td>' . td($r['student_id']) . '</td>'
. '<td>' . td($r['first_name'] . ' ' . $r['last_name']) . '</td>'
. '<td>' . td($r['company_name']) . '</td>'
. '<td>' . td($r['position']) . '</td>'
. '<td class="center">' . td($r['start_date']) . ' - ' . td($r['end_date']) . '</td>'
. '<td class="center">' . td($r['internship_status']) . '</td>'
. '</tr>';
}
$internshipCount = substr_count($internshipRowsHtml, '<tr>');
$html = <<<HTML
<style>
body { font-family: sarabun; font-size: 12px; }
h1 { font-size: 18px; color: #1976D2; margin-bottom: 4px; }
.meta { font-size: 11px; color: #555; margin-bottom: 12px; }
table { width: 100%; border-collapse: collapse; margin-bottom: 18px; }
th, td { border: 1px solid #999; padding: 4px 6px; font-size: 10.5px; }
th { background: #1976D2; color: #fff; }
.center { text-align: center; }
h2 { font-size: 14px; color: #1976D2; margin: 10px 0 6px 0; }
</style>
<h1>InternBase — รายงานข้อมูลการฝึกงาน</h1>
<div class="meta">
ภาคการศึกษา: {$semesterLabel} | ปีการศึกษา: {$yearLabel} |
วันที่ออกรายงาน: {$generatedAt} | จำนวนข้อมูลทั้งหมด: {$internshipCount} รายการ |
ผู้จัดทำรายงาน: {$generatedBy}
</div>
<h2>ตารางข้อมูลการฝึกงาน ({$internshipCount} รายการ)</h2>
<table>
<thead>
<tr>
<th>ลำดับ</th><th>รหัสนักศึกษา</th><th>ชื่อ-นามสกุล</th><th>สถานประกอบการ</th>
<th>ตำแหน่ง</th><th>ช่วงเวลาฝึกงาน</th><th>สถานะ</th>
</tr>
</thead>
<tbody>{$internshipRowsHtml}</tbody>
</table>
HTML;
try {
$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$mpdf = new \Mpdf\Mpdf([
'mode' => 'utf-8',
'format' => 'A4-L', // แนวนอน เพราะมีหลายคอลัมน์
'fontDir' => array_merge($fontDirs, [__DIR__ . '/includes/fonts']),
'fontdata' => $fontData + [
'sarabun' => [
'R' => 'Sarabun-Regular.ttf',
'B' => 'Sarabun-Bold.ttf',
'I' => 'Sarabun-Italic.ttf',
'BI' => 'Sarabun-BoldItalic.ttf',
],
],
'default_font' => 'sarabun',
'margin_top' => 15, 'margin_bottom' => 15,
]);
$mpdf->SetHTMLFooter('<div style="text-align:center; font-size:9px; color:#777;">หน้า {PAGENO} / {nbpg}</div>');
$mpdf->WriteHTML($html);
$filename = 'internbase_report_' . date('Ymd_His') . '.pdf';
$mpdf->Output($filename, \Mpdf\Output\Destination::DOWNLOAD);
exit();
} catch (\Throwable $e) {
error_log('[export_pdf.php] ' . $e->getMessage());
http_response_code(500);
die('ไม่สามารถสร้างไฟล์ PDF ได้ในขณะนี้ กรุณาลองใหม่อีกครั้ง');
}