39199-vm/export_pdf.php
Flatlogic Bot 270903d716 roni
2026-03-15 02:57:10 +00:00

79 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
@ini_set('display_errors', '1');
@error_reporting(E_ALL);
@date_default_timezone_set('UTC');
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/registration_db.php';
require_admin();
ensure_registration_table();
$registrations = fetch_registrations();
function pdf_escape(string $text): string {
$text = str_replace(['\\', '(', ')'], ['\\\\', '\\(', '\\)'], $text);
return $text;
}
function build_pdf(array $lines): string {
$y = 760;
$leading = 16;
$content = "BT\n/F1 12 Tf\n50 $y Td\n";
foreach ($lines as $line) {
$content .= "(" . pdf_escape($line) . ") Tj\n";
$y -= $leading;
$content .= "0 -" . $leading . " Td\n";
}
$content .= "ET";
$objects = [];
$objects[] = "<< /Type /Catalog /Pages 2 0 R >>";
$objects[] = "<< /Type /Pages /Kids [3 0 R] /Count 1 >>";
$objects[] = "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>";
$objects[] = "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>";
$objects[] = "<< /Length " . strlen($content) . " >>\nstream\n" . $content . "\nendstream";
$pdf = "%PDF-1.4\n";
$offsets = [0];
foreach ($objects as $i => $obj) {
$offsets[] = strlen($pdf);
$pdf .= ($i + 1) . " 0 obj\n" . $obj . "\nendobj\n";
}
$xref = strlen($pdf);
$pdf .= "xref\n0 " . (count($objects) + 1) . "\n";
$pdf .= "0000000000 65535 f \n";
for ($i = 1; $i <= count($objects); $i++) {
$pdf .= sprintf("%010d 00000 n \n", $offsets[$i]);
}
$pdf .= "trailer\n<< /Size " . (count($objects) + 1) . " /Root 1 0 R >>\n";
$pdf .= "startxref\n$xref\n%%EOF";
return $pdf;
}
$lines = [
'Laporan Pendaftar',
'Tanggal: ' . date('Y-m-d H:i:s') . ' UTC',
'Total: ' . count($registrations),
'------------------------------------------------------------',
];
foreach ($registrations as $row) {
$lines[] = sprintf(
'%s | %s | %s | %s',
$row['reg_code'],
$row['name'],
$row['education'],
$row['major']
);
}
$pdf = build_pdf($lines);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="report_pendaftar.pdf"');
header('Content-Length: ' . strlen($pdf));
echo $pdf;
exit;