dompdf在生成PDF时不处理特殊字符

时间:2018-06-28 09:56:30

标签: php dompdf

我知道这是重复的问题,但是我找不到现有问题的解决方案。所以,我要再次提交相同的问题。

我正在使用具有0.7.0版本的dompdf库。生成PDF时,会有类似的代码,

 $html = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body>' . $html . '</body></html>';

    if (get_magic_quotes_gpc())
    {
      $html = stripslashes($html);
    }

    $dompdf    = new DOMPDF;
    $dompdf->loadHTML($html, 'UTF-8');
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();

$output = $dompdf->output();
$dompdf->stream("test.pdf", array("Attachment" => 1));

在$ html变量中,我有包含包含数据的特殊字符的内容,例如“Wimić”。

但是在生成pdf后,它显示出类似“ Wimi?”的字样。我不知道该如何解决。有什么解决办法吗?

这是流功能:

function stream($options = '')
    {
        if (!is_array($options)) {
            $options = array();
        }

        if (headers_sent()) {
            die("Unable to stream pdf: headers already sent");
        }

        $debug = empty($options['compression']);
        $tmp = ltrim($this->output($debug));

        header("Cache-Control: private");
        header("Content-type: application/pdf");

        header("Content-Length: " . mb_strlen($tmp, '8bit'));
        $filename = (isset($options['Content-Disposition']) ? $options['Content-Disposition'] : 'document.pdf');

        $filename = str_replace(array("\n", "'"), "", basename($filename, ".pdf")) . ".pdf";

        if (!isset($options["Attachment"])) {
            $options["Attachment"] = true;
        }

        $attachment = $options["Attachment"] ? "attachment" : "inline";

        $encoding = mb_detect_encoding($filename);
        $fallbackfilename = mb_convert_encoding($filename, "ISO-8859-1", $encoding);
        $encodedfallbackfilename = rawurlencode($fallbackfilename);
        $encodedfilename = rawurlencode($filename);

        header(
            "Content-Disposition: $attachment; filename=" . $encodedfallbackfilename . "; filename*=UTF-8''$encodedfilename"
        );

        if (isset($options['Accept-Ranges']) && $options['Accept-Ranges'] == 1) {
            header("Accept-Ranges: " . mb_strlen($tmp, '8bit'));
        }

        echo $tmp;
        flush();
    }

0 个答案:

没有答案