ArtiGrid
Generating PDF Invoices with ArtiGrid
This example demonstrates how to create a PDF invoice using the PDF library integrated in ArtiGrid.
1. Get PDF Object
$pdf = $grid->getPDFObject();
This retrieves the PDF object from ArtiGrid, which allows you to generate PDF documents using HTML content.
2. Prepare Invoice Data
$invoiceData = [
"client_name" => "John Smith",
"invoice_number" => "INV20260316",
"invoice_date" => "2026-03-16",
"due_date" => "2026-03-30",
"notes" => "This is a sample invoice generated with ArtiGrid."
];
$items = [
["description" => "Product A", "quantity" => 2, "unit_price" => 1500],
["description" => "Product B", "quantity" => 1, "unit_price" => 3000],
["description" => "Service C", "quantity" => 3, "unit_price" => 1200]
];
Define the invoice header information and the line items for the invoice.
3. Generate Table Rows and Calculate Totals
$totalAmount = 0;
$rowsHtml = "";
foreach ($items as $index => $item) {
$amount = $item['quantity'] * $item['unit_price'];
$totalAmount += $amount;
$rowsHtml .= "<tr><td>".($index+1)."</td><td>".$item['description']."</td><td>".$item['quantity']."</td><td>".$item['unit_price']."</td><td>".$amount."</td></tr>";
}
This loop calculates the total amount and prepares the HTML rows for the invoice table.
4. Create Full HTML for Invoice
$html = <<<EOD
<h1 style="text-align:center;">Invoice</h1>
<table>...</table>
<table border="1">...$rowsHtml...</table>
<p><strong>Notes:</strong> {$invoiceData['notes']}</p>
EOD;
You can customize the HTML layout, fonts, colors, and table structure. ArtiGrid will render this HTML inside the PDF.
5. Generate PDF
$pdf->SetFont('dejavusans', '', 12, '', true);
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->Output("invoice_demo.pdf", "I");
exit;
This sets the UTF-8 font, adds a page, writes the HTML content to the PDF, and outputs it directly in the browser.
Summary:
- Use
to access PDF functions.getPDFObject() - Create dynamic HTML content for headers, line items, and totals.
- ArtiGrid handles rendering the HTML into a PDF document.
- The PDF can be output directly in the browser or saved as a file.
<?php
$grid = new ArtiGrid();
// Get PDF object
$pdf = $grid->getPDFObject();
// Sample invoice data
$invoiceData = [
"client_name" => "John Smith",
"invoice_number" => "INV20260316",
"invoice_date" => "2026-03-16",
"due_date" => "2026-03-30",
"notes" => "This is a sample invoice generated with ArtiGrid."
];
$items = [
["description" => "Product A", "quantity" => 2, "unit_price" => 1500],
["description" => "Product B", "quantity" => 1, "unit_price" => 3000],
["description" => "Service C", "quantity" => 3, "unit_price" => 1200]
];
// Calculate totals
$totalAmount = 0;
$rowsHtml = "";
foreach ($items as $index => $item) {
$amount = $item['quantity'] * $item['unit_price'];
$totalAmount += $amount;
$rowsHtml .= "<tr style='background-color:#f2f2f2;'>
<td style='width:5%;text-align:center;'>".($index+1)."</td>
<td style='width:50%;'>".$item['description']."</td>
<td style='width:15%;text-align:center;'>".$item['quantity']."</td>
<td style='width:15%;text-align:right;'>$ ".number_format($item['unit_price'],0,",",".")."</td>
<td style='width:15%;text-align:right;'>$ ".number_format($amount,0,",",".")."</td>
</tr>";
}
// Format total separately
$totalFormatted = number_format($totalAmount, 0, ".", ",");
// Full HTML invoice
$html = <<<EOD
<h1 style="text-align:center;">Invoice</h1>
<table style="width:100%;font-size:12px;margin-bottom:20px;">
<tr>
<td style="width:50%;">Client:<br><strong>{$invoiceData['client_name']}</strong></td>
<td style="width:50%;">Invoice No.:<br><strong>{$invoiceData['invoice_number']}</strong></td>
</tr>
<tr>
<td>Invoice Date:<br>{$invoiceData['invoice_date']}</td>
<td>Due Date:<br>{$invoiceData['due_date']}</td>
</tr>
</table>
<table style="width:100%;border-collapse: collapse;font-size:11px;" border="1">
<tr style="background-color:#d9d9d9;">
<th style="width:5%;">#</th>
<th style="width:50%;">Description</th>
<th style="width:15%;">Quantity</th>
<th style="width:15%;">Unit Price</th>
<th style="width:15%;">Amount</th>
</tr>
$rowsHtml
<tr style="background-color:#e1e1e1;">
<td colspan="4" style="text-align:right;"><strong>Total</strong></td>
<td style="text-align:right;"><strong>\$ $totalFormatted</strong></td>
</tr>
</table>
<br>
<p><strong>Notes:</strong><br>{$invoiceData['notes']}</p>
EOD;
// Set UTF-8 font and add a page
$pdf->SetFont('dejavusans', '', 12, '', true);
$pdf->AddPage();
// Write HTML
$pdf->WriteHTML($html);
// Output PDF directly in browser
$pdf->Output("invoice_demo.pdf", "I");
exit;
?>