首页 > 解决方案 > 如何在 PHP 中将非 UTF-8 编码转换为更具可读性/ UTF-8 编码?

问题描述

我在 Laravel 5.4 项目中使用“PDF Parser”来获取 pdf 文件的内容。但是当我转储内容时,我会得到一些编码文本,当将其悬停在上面时,会显示“5868 个二进制或非 UTF-8 字符”。

这是应该读取文件内容的代码。

$file = $request->file('file');
$parser = new \Smalot\PdfParser\Parser();
$pdf    = $parser->parseFile($file);
$scannedText = $pdf->getText();
dd($scannedText);

它转储了这样的东西:

x00i\x00n\x00g\x00ª\x00t\x00h\x00i

mb_detect_encoding()当“编码类型”设置为“自动”时, PHP 的函数返回 false。utf8_encode()也不起作用,所以我知道它不是 ISO-8859-1 编码。

这就是我现在卡住的地方。任何帮助表示赞赏。

标签: phplaravelphp-7

解决方案


在我的项目中,我遇到了与“PDF Parser”类似的错误。

但是我不再使用此错误:https ://github.com/spatie/pdf-to-text 。

您可以使用 composer 来安装它:

composer require spatie/pdf-to-text

它是这样工作的:

use Spatie\PdfToText\Pdf;

$pdf = (new Pdf())->setPdf('sample.pdf');

$pdf->setOptions(['layout']);

dump($pdf->text());

推荐阅读