<commit_before><?php

/**
 * This class brings PDF Generation functionality to Nails
 *
 * @package     Nails
 * @subpackage  module-pdf
 * @category    Library
 * @author      Nails Dev Team
 * @link
 */

namespace Nails\Pdf\Library;

use Dompdf\Dompdf;
use Dompdf\Options;
use Nails\Common\Traits\Caching;
use Nails\Common\Traits\ErrorHandling;
use Nails\Factory;
use Nails\Pdf\Exception\PdfException;

class Pdf
{
    use ErrorHandling;
    use Caching;

    // --------------------------------------------------------------------------

    /**
     * The default name to give documents
     */
    const DEFAULT_FILE_NAME = 'document.pdf';

    /**
     * The default options, loaded on construction
     */
    const DEFAULT_OPTIONS = [
        'defaultPaperSize'     => 'A4',
        'isRemoteEnabled'      => true,
        'fontDir'              => DEPLOY_CACHE_DIR . 'dompdf/font/dir/',
        'fontCache'            => DEPLOY_CACHE_DIR . 'dompdf/font/cache/',
        'isHtml5ParserEnabled' => true,
    ];

    /**
     * The default paper size
     */
    const DEFAULT_PAPER_SIZE = 'A4';

    /**
     * The default paper orientaton
     */
    const DEFAULT_PAPER_ORIENTATION = 'landscape';

    // --------------------------------------------------------------------------

    /**
     * Holds a reference to DomPDF
     * @var Dompdf
     */
    protected $oDomPdf;

    /**
     * Stores any custom options as applied by the user so that they are maintained across instances
     * @var array
     */
    protected $aCustomOptions = [];

    /**
     * Stores the current paper size
     * @var string
     */
    protected $sPaperSize = '';

    /**
     * Stores the current paper orientation
     * @var string
     */
    protected $sPaperOrientation = '';

    // --------------------------------------------------------------------------

    /**
     * Construct the PDF library
     */
    public function __construct()
    {
        $this->instantiate();
    }

    // --------------------------------------------------------------------------

    /**
     * Instantiate a new instance of DomPDF
     * @return $this
     */
    protected function instantiate()
    {
        $oOptions = new Options();
        $aOptions = [];

        //  Default options
        foreach (self::DEFAULT_OPTIONS as $sOption => $mValue) {
            $aOptions[$sOption] = $mValue;
        }

        //  Custom options override default options
        foreach ($this->aCustomOptions as $sOption => $mValue) {
            $aOptions[$sOption] = $mValue;
        }

        //  Set the options
        foreach ($aOptions as $sOption => $mValue) {
            $oOptions->set($sOption, $mValue);
        }

        $this->oDomPdf = new Dompdf($oOptions);
        $this->setPaperSize($this->sPaperSize, $this->sPaperOrientation);

        return $this;
    }

    // --------------------------------------------------------------------------

    /**
     * Unset the instance of DomPDF and re-instantiate
     * @return $this
     */
    public function reset()
    {
        unset($this->oDomPdf);
        $this->instantiate();
        return $this;
    }

    // --------------------------------------------------------------------------

    /**
     * Defines a DomPDF constant, if not already defined
     *
     * @param  string $sOption The name of the option
     * @param  string $mValue  The value to give the option
     *
     * @return $this;
     */
    public function setOption($sOption, $mValue)
    {
        $this->aCustomOptions[$sOption] = $mValue;
        $this->oDomPdf->set_option($sOption, $mValue);
        return $this;
    }

    // --------------------------------------------------------------------------

    /**
     * Alias to setOption()
     *
     * @param  string $sOption The name of the option
     * @param  string $mValue  The value to give the option
     *
     * @return $this
     * @deprecated
     */
    public function setConfig($sOption, $mValue)
    {
        return $this->setOption($sOption, $mValue);
    }

    // --------------------------------------------------------------------------

    /**
     * Alias for setting the paper size
     *
     * @param  string $sSize        The paper size to use
     * @param  string $sOrientation The paper orientation
     *
     * @return $this
     */
    public function setPaperSize($sSize = null, $sOrientation = null)
    {
        $this->sPaperSize        = $sSize ?: static::DEFAULT_PAPER_SIZE;
        $this->sPaperOrientation = $sOrientation ?: static::DEFAULT_PAPER_ORIENTATION;
        $this->oDomPdf->setPaper($sSize, $sOrientation);
        return $this;
    }

    // --------------------------------------------------------------------------

    /**
     * Alias for enabling or disabling remote resources in PDFs
     *
     * @param  boolean $bValue whether to enable remote resources
     *
     * @return $this
     */
    public function enableRemote($bValue = true)
    {
        $this->setOption('isRemoteEnabled', $bValue);
        return $this;
    }

    // --------------------------------------------------------------------------

    /**<commit_msg>Fixed a bug where the apper size was not being respected<commit_after><?php

/**
 * This class brings PDF Generation functionality to Nails
 *
 * @package     Nails
 * @subpackage  module-pdf
 * @category    Library
 * @author      Nails Dev Team
 * @link
 */

namespace Nails\Pdf\Library;

use Dompdf\Dompdf;
use Dompdf\Options;
use Nails\Common\Traits\Caching;
use Nails\Common\Traits\ErrorHandling;
use Nails\Factory;
use Nails\Pdf\Exception\PdfException;

class Pdf
{
    use ErrorHandling;
    use Caching;

    // --------------------------------------------------------------------------

    /**
     * The default name to give documents
     */
    const DEFAULT_FILE_NAME = 'document.pdf';

    /**
     * The default options, loaded on construction
     */
    const DEFAULT_OPTIONS = [
        'defaultPaperSize'     => 'A4',
        'isRemoteEnabled'      => true,
        'fontDir'              => DEPLOY_CACHE_DIR . 'dompdf/font/dir/',
        'fontCache'            => DEPLOY_CACHE_DIR . 'dompdf/font/cache/',
        'isHtml5ParserEnabled' => true,
    ];

    /**
     * The default paper size
     */
    const DEFAULT_PAPER_SIZE = 'A4';

    /**
     * The default paper orientation
     */
    const DEFAULT_PAPER_ORIENTATION = 'portrait';

    // --------------------------------------------------------------------------

    /**
     * Holds a reference to DomPDF
     * @var Dompdf
     */
    protected $oDomPdf;

    /**
     * Stores any custom options as applied by the user so that they are maintained across instances
     * @var array
     */
    protected $aCustomOptions = [];

    /**
     * Stores the current paper size
     * @var string
     */
    protected $sPaperSize = '';

    /**
     * Stores the current paper orientation
     * @var string
     */
    protected $sPaperOrientation = '';

    // --------------------------------------------------------------------------

    /**
     * Construct the PDF library
     */
    public function __construct()
    {
        $this->instantiate();
    }

    // --------------------------------------------------------------------------

    /**
     * Instantiate a new instance of DomPDF
     * @return $this
     */
    protected function instantiate()
    {
        $oOptions = new Options();
        $aOptions = [];

        //  Default options
        foreach (self::DEFAULT_OPTIONS as $sOption => $mValue) {
            $aOptions[$sOption] = $mValue;
        }

        //  Custom options override default options
        foreach ($this->aCustomOptions as $sOption => $mValue) {
            $aOptions[$sOption] = $mValue;
        }

        //  Set the options
        foreach ($aOptions as $sOption => $mValue) {
            $oOptions->set($sOption, $mValue);
        }

        $this->oDomPdf = new Dompdf($oOptions);
        $this->setPaperSize($this->sPaperSize, $this->sPaperOrientation);

        return $this;
    }

    // --------------------------------------------------------------------------

    /**
     * Unset the instance of DomPDF and re-instantiate
     * @return $this
     */
    public function reset()
    {
        unset($this->oDomPdf);
        $this->instantiate();
        return $this;
    }

    // --------------------------------------------------------------------------

    /**
     * Defines a DomPDF constant, if not already defined
     *
     * @param  string $sOption The name of the option
     * @param  string $mValue  The value to give the option
     *
     * @return $this;
     */
    public function setOption($sOption, $mValue)
    {
        $this->aCustomOptions[$sOption] = $mValue;
        $this->oDomPdf->set_option($sOption, $mValue);
        return $this;
    }

    // --------------------------------------------------------------------------

    /**
     * Alias to setOption()
     *
     * @param  string $sOption The name of the option
     * @param  string $mValue  The value to give the option
     *
     * @return $this
     * @deprecated
     */
    public function setConfig($sOption, $mValue)
    {
        return $this->setOption($sOption, $mValue);
    }

    // --------------------------------------------------------------------------

    /**
     * Alias for setting the paper size
     *
     * @param  string $sSize        The paper size to use
     * @param  string $sOrientation The paper orientation
     *
     * @return $this
     */
    public function setPaperSize($sSize = null, $sOrientation = null)
    {
        $this->sPaperSize        = $sSize ?: static::DEFAULT_PAPER_SIZE;
        $this->sPaperOrientation = $sOrientation ?: static::DEFAULT_PAPER_ORIENTATION;
        $this->oDomPdf->setPaper($this->sPaperSize, $this->sPaperOrientation);
        return $this;
    }

    // --------------------------------------------------------------------------

    /**
     * Alias for enabling or disabling remote resources in PDFs
     *
     * @param  boolean $bValue whether to enable remote resources
     *
     * @return $this
     */
    public function enableRemote($bValue = true)
    {
        $this->setOption('isRemoteEnabled', $bValue);
        return $this;
    }

    // --------------------------------------------------------------------------

    /**
{'fraction_non_alphanumeric': 0.24279007377598927, 'fraction_numerical': 0.0005748778384593274, 'mean_word_length': 1.2945702352165311, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3263746', 'n_tokens_mistral': 2811, 'n_tokens_neox': 2698, 'n_words': 688}