<?php
require 'vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

/* -------------------------
   SAMPLE DYNAMIC DATA
--------------------------*/

$invoice = [
    'invoice_no' => 'NEW/2026/0010',
    'client_name' => 'Pranab',
    'phone' => '9876543210',
    'address' => 'Champasari, Siliguri',
    'invoice_date' => '26-02-2026'
];

$items = [
    ['description'=>'22kt Gold Ring','price'=>45000],
    ['description'=>'Gold Chain','price'=>65000],
];

/* -------------------------
   BACKGROUND IMAGE (BASE64)
--------------------------*/

$imagePath = __DIR__ . '/invoice.jpg';  // apni JPG file ka naam
$imageData = base64_encode(file_get_contents($imagePath));
$imageSrc  = 'data:image/jpeg;base64,' . $imageData;

$total = 0;
foreach($items as $item){
    $total += $item['price'];
}

/* -------------------------
   HTML START
--------------------------*/

ob_start();
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>

@page { margin:0; }

body{
    margin:0;
    padding:0;
    font-family: DejaVu Sans, sans-serif;
}

.page{
    position:relative;
    width:100%;
    height:100%;
}

/* Background Full Page */
.bg{
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
}

/* Overlay Content */
.content{
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
    font-size:12px;
}

/* Customer Details Position (Adjust According to JPG) */
.customer{
    position:absolute;
    top:170px;
    left:120px;
    width:350px;
}

/* Items Position */
.items{
    position:absolute;
    top:300px;
    left:120px;
    width:70%;
}

.items table{
    width:100%;
}

.items td{
    padding:4px 0;
}

/* Total Position */
.total{
    position:absolute;
    bottom:200px;
    right:120px;
    font-weight:bold;
}

</style>
</head>

<body>

<div class="page">

    <!-- Background Image -->
    <img class="bg" src="<?= $imageSrc ?>">

    <!-- Data Over Image -->
    <div class="content">

        <!-- Customer Details -->
        <div class="customer">
            <strong>Invoice No:</strong> <?= $invoice['invoice_no'] ?><br>
            <strong>Name:</strong> <?= $invoice['client_name'] ?><br>
            <strong>Phone:</strong> <?= $invoice['phone'] ?><br>
            <strong>Address:</strong> <?= $invoice['address'] ?><br>
            <strong>Date:</strong> <?= $invoice['invoice_date'] ?><br>
        </div>

        <!-- Items -->
        <div class="items">
            <table>
                <?php foreach($items as $item): ?>
                <tr>
                    <td><?= $item['description'] ?></td>
                    <td align="right">₹ <?= number_format($item['price'],2) ?></td>
                </tr>
                <?php endforeach; ?>
            </table>
        </div>

        <!-- Total -->
        <div class="total">
            Total : ₹ <?= number_format($total,2) ?>
        </div>

    </div>

</div>

</body>
</html>

<?php
$html = ob_get_clean();

/* -------------------------
   DOMPDF GENERATE
--------------------------*/

$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isPhpEnabled', true);
$options->set('isRemoteEnabled', true);

$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4','portrait');
$dompdf->render();
$dompdf->stream("invoice.pdf", ["Attachment"=>1]);