<?php

namespace App\Libraries;

use Dompdf\Dompdf;
use Dompdf\Options;

class Pdf
{
    protected $dompdf;
    protected $filename;

    public function __construct()
    {
        // Inisialisasi DomPDF dengan opsi
        $options = new Options();
        $options->set('isHtml5ParserEnabled', true);
        $options->set('isPhpEnabled', true); // Jika Anda membutuhkan PHP di dalam HTML
        $options->set('isRemoteEnabled', true); // Enable remote files like images

        $this->dompdf = new Dompdf($options);
        $this->filename = "laporan.pdf";
    }

    /**
     * Load a CodeIgniter view into DomPDF
     *
     * @param string $view The view to load
     * @param array $data The view data
     * @return void
     */
    public function load_view(string $view, array $data = [])
    {
        // Memuat view dan mengambil HTML-nya
        $html = view($view, $data);

        // Muat HTML ke DomPDF
        $this->dompdf->loadHtml($html);

        // Set ukuran kertas (misalnya A4)
        $this->dompdf->setPaper('A4', 'landscape');

        // Render PDF
        $this->dompdf->render();

        // Output PDF ke browser
        $this->dompdf->stream($this->filename, ['Attachment' => false]);
    }
}
