<?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;
  //$dompdf = new Dompdf(array('enable_remote' => true));

  // instantiate and use the dompdf class
  $dompdf = new Dompdf();
  //$dompdf->loadHtml('hello world');

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

  $dompdf->loadHtml($html);
  //$dompdf->setOption('isPhpEnabled', true);
  //$dompdf->load_html($html);

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

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

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

  function injectPageCount(Dompdf $dompdf): void
  {
      /** @var CPDF $canvas */
      $canvas = $dompdf->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);

?>
