php pdf 文件的编码问题

php encoding issue with pdf files

使用 Ubuntuphp 我遇到了一个常见问题,但我还没有解决这个问题没有找到任何解决方案。 我正在上传一个 pdf 文件,我将其转换为文本文件(使用 ImgMagick + Tesseract)。

    $output = shell_exec('convert -density 300 ' . $fichier . ' ' . $fichier_noExt . '.png');
    $output = shell_exec('tesseract ' . $fichier_noExt . '.png ' . $fichier_noExt . '.txt');

当我这样做时:

$file = fopen($fichier_txt.'.txt', 'r+');
echo $file;

我得到一些 '°' 而不是 '°','€' 而不是 '€' 和 'É' 而不是 'é'。 我知道这是一个编码问题,但我找不到它。

如果你想打印一个 UTF-8 字符串结果,你可以试试这个:

$file = fopen($fichier_txt.'.txt', 'r+');
while(!feof($file)){
 echo mb_convert_encoding(fread($file, 1024), 'UTF-8', mb_detect_encoding($file));
}
fclose($file);

文档:

http://php.net/manual/fr/function.mb-convert-encoding.php

http://php.net/manual/fr/function.mb-detect-encoding.php

您也可以使用 dos2unix 和 mac2unix 来转换文件,使用这个自定义函数:

function convertFiles($file) { // pass complete path to file
    if (shell_exec("dos2unix $file") !== FALSE) {
        if (shell_exec("mac2unix $file") !== FALSE) {
            return TRUE;
        }
        else {
            return FALSE;
        }
    }
    else {
        return FALSE;
    }
}

你可以使用 apt-get install 安装这些命令 http://xmodulo.com/how-to-install-dos2unix-on-linux.html

最后,如果你在网页上显示它,不要忘记设置元字符集内容类型:

header('Content-Type: text/html; charset=utf-8');

或html版本

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

哦亲爱的...

我只是忘了在我的文件顶部添加这个:

header('Content-Type: text/html; charset=utf-8');

它现在可以用了,很抱歉耽误你的时间,但我需要一些新的外观 :)。

祝你有愉快的一天和 cya !