cloudbluedigital / pdftohtml
使用 Poppler-utils 的 PHP PDF 转 HTML 转换器
Requires
- illuminate/config: ~5
- symfony/filesystem: ^4.2
- symfony/process: ^4.2
- thesoftwarefanatics/php-html-parser: ^1.8.0
Requires (Dev)
- phpunit/phpunit: ~4
- satooshi/php-coveralls: ^1.0
This package is not auto-updated.
Last update: 2024-09-30 18:02:09 UTC
README
PDF 转 HTML PHP 类
一个简单的类,用于将 PDF 文件转换为 HTML 文档。此包是从原始维护者分叉而来。由于它已经被遗弃,我决定迁移此包并将其移植,以便在 php 7.1+ 环境中使用。
安装
composer require garrensweet/pdftohtml-php
或者将此包添加到您的 composer.json
{
"garrensweet/pdftohtml-php": "^2.1.0"
}
要求
您必须在您的系统上安装
poppler-utils
包。您还必须确保拥有poppler-utils
的用户与您的Nginx
用户相匹配,否则您将无法访问此包。在实例化
Pdf
类之前,您需要告诉库您的二进制文件的路径。如果没有这样做,将使用默认回退(这可能是大多数人都不正确的),您将收到一个通用错误。您可以通过使用此类的Config::set
方法来这样做。
注意:此
Config
方法与 Laravel 使用的相同存储库实现。
\Gswits\PdfToHtml\Config::set('pdftohtml.bin', '/usr/local/bin/pdftohtml');
\Gswits\PdfToHtml\Config::set('pdfinfo.bin', '/usr/local/bin/pdfinfo');
使用方法
在设置好 poll-utils 包并提供了库的路径后,您可以继续以下操作
警告!如果您不在自动从 composer 加载供应商列表的环境中工作,您将需要手动这样做,通过在文件顶部添加
include /vendor/autoload.php
。如果您在使用 Laravel,则不需要此步骤。
以下是一个示例用例
<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
// initiate
$pdf = new Gswits\PdfToHtml\Pdf('file.pdf');
// convert to html string
$html = $pdf->html();
// convert a specific page to html string
$page = $pdf->html(3);
// convert to html and return it as [Dom Object](https://github.com/thesoftwarefanatics/php-html-parser)
$dom = $pdf->getDom();
// check if your pdf has more than one pages
$total_pages = $pdf->getPages();
// Your pdf happen to have more than one pages and you want to go another page? Got it. use this command to change the current page to page 3
$dom->goToPage(3);
// and then you can do as you please with that dom, you can find any element you want
$paragraphs = $dom->find('body > p');
?>
将选项传递给 getDOM
默认情况下,getDom()
将提取 PDF 中包含的所有图像。如果您不希望保留图像,可以在调用 `\$pdf->html()` 生成您的 HTML 文档之前指定此属性。
<?php
$pdfDom = $pdf->getDom(['ignoreImages' => true]);
可用选项
您还可以向 Pdf
构造函数传递多个参数。这些参数作为标志传递给底层的 pdftohtml
二进制文件。您可以查看选项的完整列表
- singlePage, 默认: false
- imageJpeg, 默认: false
- ignoreImages, 默认: false
- zoom, 默认: 1.5
- noFrames, 默认: true
Windows 用户的使用说明
对于那些需要在 Windows 上使用此包的人,有一种方法。首先在这里下载 poppler-utils for windows http://blog.alivate.com.au/poppler-windows/。并下载最新二进制文件。
下载后,解压它。将会有一个名为 bin
的目录。我们需要这个。然后更改您的代码如下
<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
use Gswits\PdfToHtml\Config;
// change pdftohtml bin location
Config::set('pdftohtml.bin', 'C:/poppler-0.37/bin/pdftohtml.exe');
// change pdfinfo bin location
Config::set('pdfinfo.bin', 'C:/poppler-0.37/bin/pdfinfo.exe');
// initiate
$pdf = new Gswits\PdfToHtml\Pdf('file.pdf');
// convert to html and return it as [Dom Object](hhttps://github.com/thesoftwarefanatics/php-html-parser)
$html = $pdf->html();
// check if your pdf has more than one pages
$total_pages = $pdf->getPages();
// Your pdf happen to have more than one pages and you want to go another page? Got it. use this command to change the current page to page 3
$html->goToPage(3);
// and then you can do as you please with that dom, you can find any element you want
$paragraphs = $html->find('body > p');
?>
OS/X 用户的使用说明
感谢 @kaleidoscopique 尝试使其在此包上在 OS/X 上运行
1. 安装 brew
Brew 是 OS/X 上著名的包管理器:https://brew.sh.cn/(aptitude 风格)。
2. 安装 poppler
brew install poppler
3. 验证 pdfinfo 和 pdftohtml 的路径
$ which pdfinfo
/usr/local/bin/pdfinfo
$ which pdftohtml
/usr/local/bin/pdfinfo
4. 无论路径是什么,都使用 Gswits\PdfToHtml\Config::set
在您的 PHP 代码中设置它们。显然,使用与 which
命令提供的相同路径;
<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
// change pdftohtml bin location
\Gswits\PdfToHtml\Config::set('pdftohtml.bin', '/usr/local/bin/pdftohtml');
// change pdfinfo bin location
\Gswits\PdfToHtml\Config::set('pdfinfo.bin', '/usr/local/bin/pdfinfo');
// initiate
$pdf = new Gswits\PdfToHtml\Pdf('file.pdf');
// convert to html and return it as [Dom Object](https://github.com/thesoftwarefanatics/php-html-parser)
$html = $pdf->html();
?>
反馈 & 贡献
发送给我一个改进或任何有问题的 issue。我喜欢帮助并解决其他人的问题。谢谢 :+1