imal-h/pdf-box

PDFlib当前版本提供了将PDF转换为图片以及将图片转换为PDF的方法。

1.3.1 2020-02-28 05:21 UTC

This package is auto-updated.

Last update: 2024-08-28 14:51:10 UTC


README

PDFlib是一个允许你与PDF文件交互的项目,当前版本提供了将PDF转换为图片以及将图片转换为PDF的方法,未来的版本将包含更多与PDF文件交互的功能。

该项目是斯里兰卡Treinetic (Pvt) Ltd的倡议,请联系我们info@treinetic.com,由专家完成你的项目。

Issues Software License Forks

需求

你应该安装并配置好Ghostscript >= 9.16。

安装

此包可以通过composer安装。

$ composer require imal-h/pdf-box

使用

将PDF转换为一系列图片。

$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->setImageFormat(\ImalH\PDFLib\PDFLib::$IMAGE_FORMAT_PNG);
$pdflib->setDPI(300);
$pdflib->setPageRange(1,$pdflib->getNumberOfPages());
$pdflib->setFilePrefix('page-'); // Optional
$pdflib->convert();

或者使用链式操作

$files = (new ImalH\PDFLib\PDFLib())
    ->setPdfPath($pdf_file_path)
    ->setOutputPath($folder_path_for_images)
    ->setImageFormat(\ImalH\PDFLib\PDFLib::$IMAGE_FORMAT_PNG)
    ->setDPI(300)
    ->setPageRange(1,$pdflib->getNumberOfPages())
    ->setFilePrefix('page-') // Optional
    ->convert();

从一系列图片创建PDF

$pdflib = new ImalH\PDFLib\PDFLib();
$imagePaths = ["images-1.jpg","images-2.jpg"];
$pdflib->makePDF($destination_pdf_file_path,$imagePaths);

如果在任何情况下代码抛出'**** Unable to open the initial device, quitting.'这样的错误,这意味着程序由于权限问题无法创建临时文件,这种错误仅在Linux或Mac操作系统中出现,所以请检查apache日志并提供必要的权限。

其他有用的方法

你可以获取PDF中的总页数。

$pdfBox->getNumberOfPages(); //returns the number of pages in the pdf
$pdfBox->setPageRange(1,2); // allows you to convert only few pages in the PDF Document
$pdfBox->setImageQuality(95); // allows you to tell the quality you expect in the output Jpg file (only jpg)
$pdfBox->setDPI(300); //setting the DPI (Dots per inch) of output files
$pdfLib->setImageFormat(\ImalH\PDFLib\PDFLib::$IMAGE_FORMAT_PNG,$dDownScaleFactor=null);   //this will set the output image format, default it is jpg, but I recommend using pdf to png because it seems it is faster
/*
$dDownScaleFactor=integer
This causes the internal rendering to be scaled down by the given (integer <= 8) factor before being output. For example, the following will produce a 200dpi output png from a 600dpi internal rendering:
    
    gs -sDEVICE=png16m -r600 -dDownScaleFactor=3 -o tiger.png\examples/tiger.png

Read More : http://ghostscript.com/doc/current/Devices.htm
*/

变更日志

请参阅CHANGELOG获取更多最近更改的信息。

贡献

请参阅CONTRIBUTING获取详细信息。

测试

  • 在提交pull请求之前,请确保运行所有测试并查看它们是否全部通过。

如何运行测试?

composer install
vendor/bin/phpunit

鸣谢

许可协议

MIT许可(MIT)。请参阅许可文件获取更多信息。