divido/pdf-to-img

此包的最新版本(0.0.4)没有可用的许可信息。

库,用于将PDF转换为图片。

此包的官方存储库似乎已不存在,因此该包已被冻结。

0.0.4 2019-06-26 14:32 UTC

README

此库可以帮助将PDF文档转换为图片。

目录

依赖项

为了支持ImageMagick(convert工具),您需要安装ImageMagick和GhostScript。在macOS上,您可以使用以下命令最简单地安装它们:

$ brew install imagemagick gs

为了支持PDFtoPPM(Poppler),您需要安装Poppler套件。在macOS上,您可以使用以下命令最简单地安装它:

$ brew install poppler

这些说明截至编写日期(2019-08-05)有效;如果它们对您不起作用,请更新此文档并提交一个PR。

您还需要使用Composer安装PHP依赖项

$ composer install

测试

此库有一个测试套件。您需要安装上述讨论中提到的适当依赖项,然后可以使用以下命令运行测试:

$ vendor/bin/phpunit

如果您尚未安装依赖项,可能会遇到一些奇怪的测试失败。在报告任何问题之前,请确保您已正确安装它们。

PDF源

源对象包含以某种形式存在的PDF数据。所有源构造函数都需要PDF源(以下列出的各种形式)和用于保存PDF/图片前缀的文件名。

转换器的内置源选项包括:

1. 缓冲区

当整个PDF已读入字符串变量时,此源可用。例如:

$pdf_source = file_get_contents('example.pdf');
$source = new Buffer($pdf_source, 'example.pdf');

2. 缓冲区Base64

当整个PDF以Base 64编码读入字符串变量时,此源可用。例如:

$pdf_source = base64_encode(file_get_contents('example.pdf'));
$source = new BufferBase64($pdf_source, 'example.pdf');

3. 流

当PDF可以从PSR-7流中获取时,此源可用,如果PDF使用Guzzle下载,则非常有用。例如:

// Assume file has been downloaded from S3.
$response = $s3->getObject([
  "Bucket" => 'your-bucket',
  "Key" => '/folder/example.pdf'
]);

// $response->Body is now a Guzzle Stream which implements PSR-7 StreamInterface
$source = new Stream($response->Body, 'example.pdf');

4. 文件资源

当PDF可以从文件指针中获取时,此源可用。

$fp = fopen('example.pdf', 'r');
$source = new FileResource($fp, 'example.pdf');

转换引擎。

转换引擎执行PDF到一系列图片的转换。所有引擎都允许设置选项。

内置的转换包括:

1. ConvertBinaryEngine

ConvertBinaryEngine使用您系统上的convert二进制(由ImageMagick提供)进行转换。

请注意,某些选项可能取决于您安装的ImageMagick/convert版本而不可用。

$engine = EngineFactory::GetEngine('convert-binary');

// Optionally set arguments. @see https://imagemagick.org.cn/script/convert.php for CLI options.
$engine->withOptions([
    '-quality' => '100',
]);

2. PpmToPdfBinaryEngine

PpmToPdfBinaryEngine使用您系统上的pdftoppm二进制(由Poppler提供)进行转换。根据您的版本,某些参数可能不可用。

请注意,某些选项可能取决于您安装的poppler/pdftoppm版本而不可用。另外请注意,您不应该在参数中设置图像类型,因为这是在包装器中更高处处理的。

$engine = EngineFactory::GetEngine('pdftoppm-binary');

// Optionally set arguments. @see http://manpages.ubuntu.com/manpages/yakkety/man1/pdftoppm.1.html for CLI options.
$engine->withOptions([
    'r' => '150', 
]);

// Do not set the image type...
$engine->withOptions([
  '-png': '', // Don't do this...!
]);

输出

转换器将返回一个Output对象,该对象具有以下方法:

getPath()
此方法返回图像和原始PDF保存到磁盘上的路径。例如:

echo $output->getPath();
// string(14) /tmp/j2io0caMA

getOriginalPdf($withPath = false)
此方法返回原始PDF文件名。可选地返回文件的完整路径。例如:

echo $output->getOriginalPdf();
// string(11) example.pdf

// Or with full path
echo $output->getOriginalPdf(true);
// string(26) /tmp/j2io0caMA/example.pdf

getSubsetPdf($withPath = false)
此方法返回子集PDF文件名。如果没有创建子集PDF,则此值可能为null。只有在对页面子集执行转换时才会创建子集PDF。可选地返回文件的完整路径。例如:

echo $output->getSubsetPdf();
// string(11) example-subset.pdf

// Or with full path
echo $output->getSubsetPdf(true);
// string(26) /tmp/j2io0caMA/example-subset.pdf


// Or with full path
echo $output->getGeneratedImages(true);
// array(2) [
//     string(28) /tmp/j2io0caMA/example-1.jpg 
//     string(28) /tmp/j2io0caMA/example-2.jpg
// ]

getGeneratedImages($withPath = false)
此方法返回所有生成的图像文件名。可选地返回图像的完整路径。例如:

echo $output->getGeneratedImages();
// array(2) [
//     string(13) example-1.jpg 
//     string(13) example-2.jpg
// ]


// Or with full path
echo $output->getGeneratedImages(true);
// array(2) [
//     string(28) /tmp/j2io0caMA/example-1.jpg 
//     string(28) /tmp/j2io0caMA/example-2.jpg
// ]

转换器

Converter类是将所有内容组合在一起并开始转换过程的类。

此类将执行将PDF转换为图像的操作,并且如果设置了页面规范,它将创建另一个仅包含指定页面的PDF。以下方法可用:

process($imageType, $pages = [])
处理转换并保存为传递的文件类型。可选地指定要保存的页面。例如:

$converter = new Converter($source, $engine); // $source and $engine are desribed above.

// This will convert the entire PDF into JPEG's
$output = $converter->process("jpg")

// This will convert only pages 2 & 4 of the PDF into PNG's
// Note that this will also create a subset PDF with just pages 2 & 4
$output = $converter->process("png", [2,4]);

整合一切。

以下示例使用了以下内容:

将PDF读取到变量中作为一个原始字符串,从本地保存的PDF(example.pdf)中 我们使用pdftoppm二进制文件进行转换 我们希望将PDF保存为credit-agreement.pdf 我们希望我们的图像是JPEG格式 我们希望我们的图像保存为credit-agreement-<page_num>.jpg

use DividoFinancialServices\PdfToImg\EngineFactory;
use DividoFinancialServices\PdfToImg\Converter;
use DividoFinancialServices\PdfToImg\Sources\Buffer;

// Load a PDF into a string
$buffer = new Buffer(file_get_contents('example.pdf'), 'credit-agreement.pdf');

// Create the conversion engine type. In this example we are using the pdftoppm binary.
$engine = EngineFactory::GetEngine('pdftoppm-binary');

// Create a Converter with the source PDF and conversion engine. 
$converter = new Converter($buffer, $engine);

// Do the conversion (saving images to JPEG)
$output = $converter->process("jpg", [2,4,]);

// 2 images (pages 2 & 4) are now saved in a temp folder on the disk. 

// Get the list of image filenames on disk
$images = $output->getGeneratedImages();

// A subset PDF has been created because the pages were specified. THe 2 pages PDF:
$subsetPdf = $output->getSubsetPdf();

// Do something with your images (upload to S3, etc..)
// When finished, perform a clean up to free up the disk space

$converter->cleanUp();