<?php

require_once dirname(__DIR__) . '/dompdf/autoload.inc.php';

// Reference the Dompdf namespace
use Dompdf\Dompdf;

function generate_pdf($html, $options, $pdf_path) {
    /**
     * Dompdf :: html to pdf
     */
    //use Dompdf\Dompdf;    //Reference the Dompdf namespace
    //Instantiate and use the dompdf class
//    $dompdf = !is_null($dompdf) ? $dompdf->destroy():'';
    $dompdf = new Dompdf();
    //need it to be able to parse images, css,..  with url src 
    $dompdf->set_option('isRemoteEnabled', true);
    //Load HTML content
    $dompdf->loadHtml($html);
    //width in cm, so we must convert it to point: *28.346456695 
    $cm_to_point_ratio = 28.346456695;
    $orientation = ((!empty($options['orientation']) && strtoupper($options['orientation']) == 'L') ? 'landscape' : 'portrait');
    if (empty($options['width']) || empty($options['height']))
        $size = 'a4';
    else
        $size = array(0, 0, ($options['width'] * $cm_to_point_ratio), ($options['height'] * $cm_to_point_ratio));
    $dompdf->setPaper($size, $orientation);
    //$dompdf->set_option('dpi', 72);
    //below 2 options important to make generated pdf more look like browser output, for spaces, line-height, ..
    $dompdf->set_option('dpi', 96);
    $dompdf->set_option('font_height_ratio', 0.9);
//    //this prevent pdf large size, but when we enable it arabic problem appears since we use utf8Glyphs
//    $dompdf->set_option('enable_font_subsetting', true);
    //Render the HTML as PDF
    $dompdf->render();
    // Output the generated PDF to Browser
    //$dompdf->stream();
    //get pdf as output
    $output = $dompdf->output();
    file_put_contents($pdf_path, $output);
}
