36735-vm/export_drill_pdf.php
Flatlogic Bot d076708932 feat: Implement new design and features for the main page
- Redesigned the main page with a modern look and feel.
- Added search and filtering functionality for drills.
- Implemented pagination for browsing drills.
- Added the ability for users to mark drills as favorites.
2025-12-07 18:15:23 +00:00

135 lines
3.8 KiB
PHP

<?php
require_once 'vendor/autoload.php';
require_once 'db/config.php';
use Dompdf\Dompdf;
use Dompdf\Options;
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
die('Invalid drill ID');
}
$drill_id = $_GET['id'];
// Fetch drill data from the database
$stmt = db()->prepare("SELECT d.title, d.description, d.image_path, d.created_at, c.name as category_name FROM drills d LEFT JOIN categories c ON d.category_id = c.id WHERE d.id = ?");
$stmt->execute([$drill_id]);
$drill = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$drill) {
die('Drill not found');
}
// HTML content for the PDF
$html = '
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Drill Report</title>
<style>
@page {
margin: 30px;
}
body {
font-family: 'Helvetica', sans-serif;
color: #333;
}
.header {
text-align: center;
margin-bottom: 20px;
}
.header img {
max-width: 150px;
margin-bottom: 10px;
}
.container {
padding: 20px;
border: 1px solid #eee;
border-radius: 10px;
}
h1 {
color: #007bff;
margin-top: 0;
border-bottom: 2px solid #007bff;
padding-bottom: 10px;
margin-bottom: 20px;
}
p {
line-height: 1.6;
margin: 0 0 15px;
}
strong {
color: #555;
}
.footer {
text-align: center;
margin-top: 30px;
font-size: 0.9em;
color: #777;
}
.drill-image {
text-align: center;
margin-top: 20px;
}
.drill-image img {
max-width: 100%;
height: auto;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="header">
<?php
$logo_path = __DIR__ . '/assets/images/logo.svg';
if (file_exists($logo_path)) {
$logo_data = base64_encode(file_get_contents($logo_path));
$logo_mime = mime_content_type($logo_path);
$logo_src = 'data:' . $logo_mime . ';base64,' . $logo_data;
} else {
$logo_src = '';
}
?>
<?php if ($logo_src): ?>
<img src="<?php echo $logo_src; ?>" alt="Logo">
<?php endif; ?>
<h2>Drill Report</h2>
</div>
<div class="container">
<h1><?php echo htmlspecialchars($drill['title']); ?></h1>
<p><strong>Category:</strong> <?php echo htmlspecialchars($drill['category_name']); ?></p>
<p><strong>Description:</strong></p>
<div><?php echo nl2br(htmlspecialchars($drill['description'])); ?></div>
<?php if (!empty($drill['image_path'])):
$image_path = __DIR__ . '/' . $drill['image_path'];
if (file_exists($image_path)) {
$image_data = base64_encode(file_get_contents($image_path));
$image_mime = mime_content_type($image_path);
$image_src = 'data:' . $image_mime . ';base64,' . $image_data;
} else {
$image_src = '';
}
?>
<?php if ($image_src): ?>
<div class="drill-image">
<img src="<?php echo $image_src; ?>" alt="Drill Image">
</div>
<?php endif; ?>
<p><strong>Created at:</strong> <?php echo htmlspecialchars($drill['created_at']); ?></p>
</div>
<div class="footer">
<p>Generated by Drillex on <?php echo date('Y-m-d H:i:s'); ?></p>
</div>
</body>
</html>
';
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("drill_" . $drill['id'] . ".pdf", ["Attachment" => false]);