<?php

class Pdf extends Model
{
  public bool $Checked = false;
  public string $BorderTop = '';
  public string $BorderLeft = '';
  public string $BorderRight = '';
  public string $BorderBottom = '';
  public int $FontSize = 0;
  public string $OutputFormat = 'pdf';
  public string $Charset = '8859-2';

  function GenerateHTML(): string
  {
    return '';
  }

  function SaveToFile(string $FileName): void
  {
    $PdfData = $this->HtmlToPdf($this->GenerateHTML());
    file_put_contents($FileName, $PdfData);
  }

  function HtmlToPdf(string $HtmlCode): string
  {
    $Encoding = new Encoding();
    if ($this->Checked == false) {
      if (CommandExist('htmldoc')) {
        $this->Checked = true;
      } else throw new Exception('htmldoc is not installed.');
    }
    $Parameters = '--no-numbered --footer . --webpage --no-embedfonts --charset '.$this->Charset.' -t '.$this->OutputFormat;
    if ($this->FontSize > 0) $Parameters .= ' --fontsize '.$this->FontSize;
    if ($this->BorderLeft != '') $Parameters .= ' --left '.$this->BorderLeft;
    if ($this->BorderTop != '') $Parameters .= ' --top '.$this->BorderTop;
    if ($this->BorderRight != '') $Parameters .= ' --right '.$this->BorderRight;
    if ($this->BorderBottom != '') $Parameters .= ' --bottom '.$this->BorderBottom;
    $Output = shell_exec('echo "'.addslashes($Encoding->FromUTF8($HtmlCode)).
      '"|htmldoc '.$Parameters.' -');
    return $Output;
  }
}
