bryank-ac/pdftohtml-php

使用 Poppler-utils 的 PHP PDF 到 HTML 转换器

v2.1.0 2019-05-31 13:42 UTC

This package is auto-updated.

Last update: 2024-09-29 05:28:30 UTC


README

Latest Stable Version Total Downloads Build Status Coverage Status

这是一个简单的类,可以将 PDF 文件转换为 HTML 文档。此包是从 原始维护者 分支出来的。由于它已经被遗弃,我决定迁移这个包并将其移植,以便在 php 7.2+ 环境中使用。

灵感来源于 garrensweet

PDF 到 HTML PHP 类

这个类可以让你使用 php 和 poppler-utils 将你的 PDF 文件转换为 HTML 文件

重要提示

请参阅下面的使用方法,因为这个包已经进行了很大的升级,包中的很多东西都已经改变。

安装

当你处于你的活动目录 apps 中时,你可以运行这个命令来将此包添加到你的应用中

	composer require bryank-ac/pdftohtml-php

或者将此包添加到你的 composer.json

{
	"bryank-ac/pdftohtml-php":"~2"
}

需求

  1. Poppler-Utils
    • Ubuntu 发行版,只需从 apt 安装它 sudo apt-get install poppler-utils
    • MacOS,使用 brew,参见下面的 OS X 注意事项 brew install poppler
  2. 启用 shell 访问的 PHP 配置

使用方法

以下是一个示例。

<?php
// if you are using composer, just use this
// not needed if your framework is already autoloading
include 'vendor/autoload.php';

// initiate
$pdf = new AccuCloud\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/paquettg/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');

// change pdftohtml bin location
\AccuCloud\PdfToHtml\Config::set('pdftohtml.bin', '/usr/local/bin/pdftohtml');

// change pdfinfo bin location
\AccuCloud\PdfToHtml\Config::set('pdfinfo.bin', '/usr/local/bin/pdfinfo');
?>

向 getDOM 传递选项

默认情况下,getDom() 提取所有图像并为每页创建一个 HTML 文件。您可以在提取 HTML 时传递选项

<?php
$pdfDom = $pdf->getDom(['ignoreImages' => true]);

可用选项

  • 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
// not needed if your framework is already autoloading
include 'vendor/autoload.php';

use AccuCloud\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 AccuCloud\PdfToHtml\Pdf('file.pdf');

// convert to html and return it as [Dom Object](https://github.com/paquettg/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. 无论路径如何,请使用 AccuCloud\PdfToHtml\Config::set 在你的 PHP 代码中设置它们。显然,使用与 which 命令给出的相同的路径;

<?php
// if you are using composer, just use this
include 'vendor/autoload.php';

// change pdftohtml bin location
\AccuCloud\PdfToHtml\Config::set('pdftohtml.bin', '/usr/local/bin/pdftohtml');

// change pdfinfo bin location
\AccuCloud\PdfToHtml\Config::set('pdfinfo.bin', '/usr/local/bin/pdfinfo');

// initiate
$pdf = new AccuCloud\PdfToHtml\Pdf('file.pdf');

// convert to html and return it as [Dom Object](https://github.com/paquettg/php-html-parser)
$html = $pdf->html();
?>

反馈 & 贡献

请发送一个改进或任何错误的问题给我。我喜欢帮助并解决他人的问题。谢谢 👍