How to Render Product Images in Invoice PDF Using mPDF with Dynamic Image Paths in PHP

I’m using mPDF in my PHP application to generate invoices. When I use a direct live URL for product images, they don’t show up in the PDF. However, when I use base64 encoding, the first product’s image is displayed correctly, but the images for the remaining products don’t show. Interestingly, the images are rendered correctly in the PDF before it’s printed, but only the first product image shows after printing. I’ve tested the URLs and base64 images, and they work fine during PDF rendering. Any suggestions for fixing this?

Invoice controller:

public function printinvoice()
    {
        if (!$this->input->get()) {
            exit();
        }
        $tid = intval($this->input->get('id'));
        $token = $this->input->get('token');
        $validtoken = hash_hmac('ripemd160', $tid, $this->config->item('encryption_key'));
        if (hash_equals($token, $validtoken)) {
            $data['id'] = $tid;
            $data['invoice'] = $this->invocies->invoice_details($tid);
            $data['title'] = "Invoice " . $data['invoice']['tid'];
            $data['products'] = $this->invocies->invoice_products($tid);
            $data['employee'] = $this->invocies->employee($data['invoice']['eid']);
            if (CUSTOM) {
                $data['c_custom_fields'] = $this->custom->view_fields_data($data['invoice']['cid'], 1, 1);
                $data['i_custom_fields'] = $this->custom->view_fields_data($tid, 2, 1);
            }


            $data['round_off'] = $this->custom->api_config(4);
            if ($data['invoice']['i_class'] == 1) {
                $pref = prefix(7);
            } elseif ($data['invoice']['i_class'] > 1) {
                $pref = prefix(3);
            } else {
                $pref = $this->config->item('prefix');
            }
            $data['general'] = array('title' => $this->lang->line('Invoice'), 'person' => $this->lang->line('Customer'), 'prefix' => $pref, 't_type' => 0);
            ini_set('memory_limit', '2048M');
            ini_set('max_execution_time', 120);
            if ($data['invoice']['taxstatus'] == 'cgst' || $data['invoice']['taxstatus'] == 'igst') {
                $html = $this->load->view('print_files/invoice-a4-gst_v' . INVV, $data, true);
            } else {
                $html = $this->load->view('print_files/invoice-a4_v' . INVV, $data, true);
                //    $html=str_replace("strong","span",$html);
                //     $html=str_replace("<h","<span",$html);
            }
            //PDF Rendering
            $this->load->library('pdf');
            if (INVV == 1) {
                $header = $this->load->view('print_files/invoice-header_v' . INVV, $data, true);
                $pdf = $this->pdf->load_split(array('margin_top' => 40));
                $pdf->SetHTMLHeader($header);
            }
            if (INVV == 2) {
                $pdf = $this->pdf->load_split(array('margin_top' => 5));
            }
            $pdf->SetHTMLFooter('<div style="text-align: right;font-family: serif; font-size: 8pt; color: #5C5C5C; font-style: italic;margin-top:-6pt;">{PAGENO}/{nbpg} #' . $data['invoice']['tid'] . '</div>');
            $pdf->WriteHTML($html);
            //  dd($html);            

            if ($this->input->get('d')) {
                $pdf->Output('Invoice_#' . $data['invoice']['tid'] . '.pdf', 'D');
            } else {
                $pdf->Output('Invoice_#' . $data['invoice']['tid'] . '.pdf', 'I');
            }
        }
    }

View invoice pdf page:

<table id="items">
        <tr>
            <th><?php echo $this->lang->line('Products') ?></th>
            <th> <?php echo $this->lang->line('Description') ?></th>
            <th><?php echo $this->lang->line('Price') ?></th>
            <th><?php echo $this->lang->line('Qty') ?></th>
            <th><?php echo $this->lang->line('Amount') ?></th>
        </tr>

        <?php
        $sub_t = 0;

        foreach ($products as $row) {
            $sub_t += $row['price'] * $row['qty'];

            if ($row['serial']) {
                $row['product_des'] .= ' - ' . $row['serial'];
            }

            echo '
        <tr class="item-row">
             <td class="item-name">' . '<div style="display: flex; align-items: center; justify-content: center;">';
            if (!empty($row['product_image'])) {
                try {
                    $imagePath = FCPATH . 'userfiles/product/thumbnail/' . $row['product_image'];
                    if (file_exists($imagePath)) {
                        $imageData = base64_encode(file_get_contents($imagePath));
                        $imageMime = mime_content_type($imagePath);
                        $base64Image = 'data:' . $imageMime . ';base64,' . $imageData;
                        echo '<img src="' . $base64Image . '" alt="' . $row['product'] . '" style="max-width: 200px; margin-right: 5px;">';

                    } else {
                        if ($index === array_key_last($products)) {
                            throw new Exception("Image file not found for the last product: " . $row['product']);
                        }
                        echo "<p>No image found</p>";
                    }
                } catch (Exception $e) {
                    echo '<p>Error: ' . $e->getMessage() . '</p>';
                }
            } else {
                echo "<p>No image</p>";
            }
            echo '</span>&nbsp;' . $row['product'] . '</div></td>
            <td class="description">' . $row['product_des'] . '</td>
            <td>' . amountExchange($row['price'], $invoice['multi'], $invoice['loc']) . '</td>
            <td>' . $row['qty'] . ' ' . $row['unit'] . '</td>
            <td>' . amountExchange($row['subtotal'], $invoice['multi'], $invoice['loc']) . '</td>
        </tr>';
        }
        ?>
</table>