<?php

  //error_reporting(E_ALL);
  //ini_set('display_errors', 1);
  error_reporting(0);

  $Position = '../../../';

  require_once $Position.'dompdf/lib/html5lib/Parser.php';
  require_once $Position.'dompdf/lib/php-font-lib/src/FontLib/Autoloader.php';
  require_once $Position.'dompdf/lib/php-svg-lib/src/autoload.php';
  require_once $Position.'dompdf/src/Autoloader.php';
  Dompdf\Autoloader::register();
  // reference the Dompdf namespace
  use Dompdf\Dompdf;
  //$pdf = new Dompdf(array('enable_remote' => true));

  // instantiate and use the dompdf class
  $pdf = new Dompdf();
  //$options->set('isJavascriptEnabled', TRUE);
  //$pdf = new Dompdf($options);
  //$pdf->loadHtml('hello world');

  //$html = file_get_contents("test2.php");
  //$html = include("test2.php");
  $Id = $_GET[Id];
  $PdfFile = $Position."facturacion/facturas/pdf/pdf-html.php";
  ob_start();
  include($PdfFile);
  $html = ob_get_clean();

  $pdf->loadHtml($html);
  //$pdf->setOption('enable-javascript', true);
  //$pdf->setOption('isPhpEnabled', true);
  //$pdf->load_html($html);
  //$options->set('isPhpEnabled', true);
  //$pdf->setOptions($options);

  // (Optional) Setup the paper size and orientation
  $pdf->setPaper('Letter', 'portrait');

  // Render the HTML as PDF
  $pdf->render();
  injectPageCount($pdf);

  // Output the generated PDF to Browser
  //$pdf->stream();
  $pdf->stream("Factura.pdf", array("Attachment" => false));

  function injectPageCount(Dompdf $pdf): void
  {
      /** @var CPDF $canvas */
      $canvas = $pdf->getCanvas();
      $pdf = $canvas->get_cpdf();

      foreach ($pdf->objects as &$o) {
          if ($o['t'] === 'contents') {
              $o['c'] = str_replace('DOMPDF_PAGE_COUNT_PLACEHOLDER', $canvas->get_page_count(), $o['c']);
          }
      }
  }

  exit(0);

?>
