gufron/pdftohtml-php

此包已被弃用,不再维护。未建议替代包。

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

v2.0.8 2016-10-11 21:43 UTC

This package is auto-updated.

Last update: 2020-08-13 09:38:37 UTC


README

Build Status Coverage Status

PDF to HTML PHP 类

此类提供给您,以便您可以使用 PHP 和 poppler-utils 将 PDF 文件转换为 HTML 文件

重要提示

请参阅以下使用说明,因为它已经进行了升级,此包中的内容已经发生了变化。

安装

当您处于活动目录的 apps 中时,您只需运行此命令即可将此包添加到您的应用程序中

	composer require gufy/pdftohtml-php:~2

或将此包添加到您的 composer.json

{
	"gufy/pdftohtml-php":"~2"
}

要求

  1. Poppler-Utils(如果您使用的是 Ubuntu 发行版,只需从 apt 安装即可) sudo apt-get install poppler-utils
  2. 启用 shell 访问的 PHP 配置

使用方法

以下是一个示例。

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

// initiate
$pdf = new Gufy\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
\Gufy\PdfToHtml\Config::set('pdftohtml.bin', '/usr/local/bin/pdftohtml');

// change pdfinfo bin location
\Gufy\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
include 'vendor/autoload.php';
use Gufy\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 Gufy\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. 无论路径如何,都使用 Gufy\PdfToHtml\Config::set 在您的 PHP 代码中设置它们。显然,使用与 which 命令给出的相同路径

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

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

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

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

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

反馈 & 贡献

发送给我一个问题以改进或任何有问题的东西。我喜欢帮助并解决其他人的问题。谢谢 👍