php - 如何在 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 编码。
这就是我现在卡住的地方。任何帮助表示赞赏。
解决方案
在我的项目中,我遇到了与“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());
推荐阅读
- excel - 将焦点从 Outlook 更改为 Excel
- latex - 带标签的文本(很像带标签的方程式)
- python - 将多列从行旋转到列
- reactjs - 使用 TypeScript 将具有类型字段的数据对象映射到特定的 React 组件
- python - 如何将文本文件解析为python中的列表
- python - 如何将空格分隔的数据读入numpy数组?
- postgresql - 如何在 PostgreSQL 中进行 DML 操作时获取索引是否更新的统计信息?
- applescript - 获取文件别名的原始项目的路径,当它丢失时
- java - 哪个 Jakarta ee 版本与 Java 10 兼容?
- javascript - 为什么 startOfMonth 会产生与 endOfMonth 不同的时区