mikehaertl\pdftk\FdfFile::__construct PHP Method

__construct() public method

Constructor
public __construct ( array $data, string | null $suffix = null, $prefix = null, string | null $directory = null, string | null $encoding = 'UTF-8' )
$data array the form data as name => value
$suffix string | null the optional suffix for the tmp file
$directory string | null directory where the file should be created. Autodetected if not provided.
$encoding string | null of the data. Default is 'UTF-8'.
    public function __construct($data, $suffix = null, $prefix = null, $directory = null, $encoding = 'UTF-8')
    {
        if ($directory === null) {
            $directory = self::getTempDir();
        }
        $suffix = '.fdf';
        $prefix = 'php_pdftk_fdf_';
        $this->_fileName = tempnam($directory, $prefix);
        $newName = $this->_fileName . $suffix;
        rename($this->_fileName, $newName);
        $this->_fileName = $newName;
        if (!function_exists('mb_convert_encoding')) {
            throw new \Exception('MB extension required.');
        }
        $fields = '';
        foreach ($data as $key => $value) {
            // Create UTF-16BE string encode as ASCII hex
            // See http://blog.tremily.us/posts/PDF_forms/
            $utf16Value = mb_convert_encoding($value, 'UTF-16BE', $encoding);
            /* Also create UTF-16BE encoded key, this allows field names containing
             * german umlauts and most likely many other "special" characters.
             * See issue #17 (https://github.com/mikehaertl/php-pdftk/issues/17)
             */
            $utf16Key = mb_convert_encoding($key, 'UTF-16BE', $encoding);
            // Escape parenthesis
            $utf16Value = strtr($utf16Value, array('(' => '\\(', ')' => '\\)'));
            $fields .= "<</T(" . chr(0xfe) . chr(0xff) . $utf16Key . ")/V(" . chr(0xfe) . chr(0xff) . $utf16Value . ")>>\n";
        }
        // Use fwrite, since file_put_contents() messes around with character encoding
        $fp = fopen($this->_fileName, 'w');
        fwrite($fp, self::FDF_HEADER);
        fwrite($fp, $fields);
        fwrite($fp, self::FDF_FOOTER);
        fclose($fp);
    }