-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_inventory.php
More file actions
54 lines (45 loc) · 1.38 KB
/
export_inventory.php
File metadata and controls
54 lines (45 loc) · 1.38 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
<?php
require_once 'connect/connection.php';
require_once __DIR__ . '/vendor/autoload.php'; // Load TCPDF
// Initialize database connection
$database = new Connection();
$pdo = $database->getConnection();
// Fetch inventory data
$query = "SELECT item_name, stock, price FROM inventory";
$stmt = $pdo->prepare($query);
$stmt->execute();
$inventoryData = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Clean output buffer before sending PDF
ob_start();
// Create a new PDF document
$pdf = new TCPDF();
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Your Company');
$pdf->SetTitle('Inventory Report');
$pdf->SetMargins(10, 10, 10);
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
// PDF Title
$pdf->Cell(0, 10, 'Inventory Report', 1, 1, 'C');
$pdf->Ln(5);
// Table Headers
$html = '<table border="1" cellpadding="5">
<tr>
<th><b>Item Name</b></th>
<th><b>Stock</b></th>
<th><b>Price</b></th>
</tr>';
// Add inventory data to PDF
foreach ($inventoryData as $item) {
$html .= "<tr>
<td>{$item['item_name']}</td>
<td>{$item['stock']}</td>
<td>\${$item['price']}</td>
</tr>";
}
$html .= '</table>';
$pdf->writeHTML($html, true, false, true, false, '');
// Clear output buffer and output PDF
ob_end_clean();
$pdf->Output('inventory_report.pdf', 'D'); // 'D' forces download
?>