<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

// Charger l'autoloader
require_once APPPATH . 'third_party/vendor/autoload.php';

// Charger manuellement mPDF depuis le bon chemin
require_once APPPATH . 'third_party/vendor/mpdf/mpdf/Mpdf.php';

class Mpdf_gen {
    
    protected $mpdf;
    
    public function __construct($params = array()) {
        $defaultConfig = [
            'tempDir' => sys_get_temp_dir(), // Utiliser le dossier temp système
            'mode' => 'utf-8',
            'format' => 'A4',
            'default_font_size' => 12,
            'default_font' => 'Arial',
            'margin_left' => 15,
            'margin_right' => 15,
            'margin_top' => 16,
            'margin_bottom' => 16
        ];
        
        $config = array_merge($defaultConfig, $params);
        
        try {
            $this->mpdf = new \Mpdf\Mpdf($config);
        } catch (Exception $e) {
            show_error('Erreur mPDF: ' . $e->getMessage());
        }
    }
    
    public function writeHTML($html) {
        try {
            $this->mpdf->WriteHTML($html);
        } catch (Exception $e) {
            show_error('Erreur WriteHTML: ' . $e->getMessage());
        }
    }
    
    public function download($filename = 'document.pdf') {
        try {
            $this->mpdf->Output($filename, 'D');
        } catch (Exception $e) {
            show_error('Erreur téléchargement: ' . $e->getMessage());
        }
    }
    
    public function output($filename = 'document.pdf', $dest = 'I') {
        $this->mpdf->Output($filename, $dest);
    }
}