ngekoding / office-to-pdf
将任何办公文件转换为PDF格式
v1.0.4
2024-05-31 02:42 UTC
Requires
README
将任何办公文件转换为PDF格式。
此库使用LibreOffice的功率将任何办公文件转换为PDF格式。因此,请确保您已在其环境中安装了LibreOffice!
要求
- PHP 5.6或更高版本
- LibreOffice
安装
composer require ngekoding/office-to-pdf
使用方法
简单使用
<?php // Include autoloader require 'vendor/autoload.php'; use Ngekoding\OfficeToPdf\Converter; $converter = new Converter(); $converter->convert('path/to/file.docx');
您还可以转换其他文件类型(不仅限于.docx),自由转换任何办公文件,如.pptx
、.xlsx
等...
设置进程超时
默认情况下,进程的超时时间为60秒,但您可以通过传递不同的超时时间(以秒为单位)到setTimeout()
方法来更改它
$converter = new Converter(); $converter->setTimeout(3600);
文档:Symfony进程超时
手动设置LibreOffice可执行文件路径
默认情况下,库将尝试根据当前操作系统查找LibreOffice可执行文件。
$converter = new Converter('path/to/libreoffice');
设置目标文件夹
默认情况下,PDF结果将生成在源文件相同的目录中。您可以通过传递第二个参数来定义目标文件夹。
$converter = new Converter(); $converter->convert('path/to/file.docx', './pdf-outputs');
默认情况下,PDF文件名将与源文件相同,您可以通过设置目标文件夹来定义输出文件名。
// The result filename will be result.pdf in the same directory as the source file $converter->convert('path/to/file.docx', 'result.pdf'); // Or save to spesific directory $converter->convert('path/to/file.docx', './pdf-outputs/result.pdf');