eloverde-sistemas/pdftohtml-php

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

1.0.6 2023-01-06 03:13 UTC

This package is auto-updated.

Last update: 2024-09-06 06:37:31 UTC


README

Build Status Coverage Status

PDF to HTML PHP 类

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

重要提示

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

安装

当您处于 active directory 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/bin/pdftohtml');

// change pdfinfo bin location
\Gufy\PdfToHtml\Config::set('pdfinfo.bin', '/usr/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/bin/pdfinfo

$ which pdftohtml
/usr/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/bin/pdftohtml');

// change pdfinfo bin location
\Gufy\PdfToHtml\Config::set('pdfinfo.bin', '/usr/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();
?>

反馈 & 贡献

请向我发送有关改进或任何错误的问题。我喜欢帮助并解决其他人的问题。谢谢 👍