使用PHP将Word doc,docx和Excel xls,xlsx转换为PDF [英] Convert Word doc, docx and Excel xls, xlsx to PDF with PHP

查看:282
本文介绍了使用PHP将Word doc,docx和Excel xls,xlsx转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用PHP将Word和Excel文件转换为PDF的方式。

I am looking for a way to convert Word and Excel files to PDF using PHP.

这样做的原因是,我需要能够将文件各种格式成一个文件。我知道如果我能够将所有内容转换成PDF,我可以使用PDFMerger(使用fpdf)将PDF合并到一个文件中。

The reason for this, is I need to be able to combine files of various formats into one document. I know that if I am able to convert everything to PDF I can then merge the PDFs into one file using PDFMerger (which uses fpdf).

我已经能够创建来自其他文件类型/图像的PDF,但是卡在Word文档中。 (我想我可以使用我已经用来从html代码创建Excel文件的PHPExcel库转换Excel文件。)

I am already able to create PDFs from other file types / images, but am stuck with Word Docs. (I think I would possibly be able to convert the Excel files using the PHPExcel library that I already use to create Excel files from html code).

我不使用Zend框架,所以我希望有人能指出我正确的方向。

I do not use the Zend Framework, so am hoping that someone will be able to point me in the right direction.

或者,如果有办法创建图像(jpg)文件从

Alternatively, if there is a way to create image (jpg) files from the Word documents, that would be workable.

感谢任何帮助!

推荐答案

我找到了一个解决问题的方案,并在请求之后,将其发布到此处以帮助他人。抱歉,如果我错过任何细节,这是一直以来,我从事这个解决方案。

I found a solution to my issue and after a request, will post it here to help others. Apologies if I missed any details, it's been a while since I worked on this solution.

需要的第一件事是安装 Openoffice.org 在服务器上。我要求我的托管提供商在我的VPS上安装公开的办公室RPM。这可以通过WHM直接完成。

The first thing that is required is to install Openoffice.org on the server. I requested my hosting provider to install the open office RPM on my VPS. This can be done through WHM directly.

现在,服务器有能力处理MS Office文件,您可以通过PHP执行命令行指令来转换文件。为了处理这个问题,我发现了 PyODConverter https://github.com/mirkonasato/pyodconverter

Now that the server has the capability to handle MS Office files you are able to convert the files by executing command line instructions via PHP. To handle this, I found PyODConverter: https://github.com/mirkonasato/pyodconverter

我在服务器上创建了一个目录,并将PyODConverter python文件放在其中。我还在Web根上创建了一个纯文本文件(我将其命名为adocpdf),其中包含以下命令行说明:

I created a directory on the server and placed the PyODConverter python file within it. I also created a plain text file above the web root (I named it "adocpdf"), with the following command line instructions in it:

directory=$1
filename=$2
extension=$3
SERVICE='soffice'
if [ "`ps ax|grep -v grep|grep -c $SERVICE`" -lt 1 ]; then 
unset DISPLAY
/usr/bin/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & 
sleep 5s
fi
python /home/website/python/DocumentConverter.py /home/website/$directory$filename$extension /home/website/$directory$filename.pdf

这检查openoffice.org库是否正在运行,然后调用PyODConverter脚本来处理该文件并输出作为PDF。当脚本从PHP文件执行时,前三行中的3个变量被提供。延迟(sleep 5s)用于确保openoffice.org有足够的时间启动,如果需要的话。我已经使用了这几个月了,5秒的差距似乎给了足够的喘息空间。

This checks that the openoffice.org libraries are running and then calls the PyODConverter script to process the file and output it as a PDF. The 3 variables on the first three lines are provided when the script is executed from with a PHP file. The delay ("sleep 5s") is used to ensure that openoffice.org has enough to time to initiate if required. I have used this for months now and the 5s gap seems to give enough breathing room.

该脚本将在与原始文件相同的目录中创建一个PDF版本的文档

The script will create a PDF version of the document in the same directory as the original.

最后,启动从PHP中转换Word / Excel文件(我在一个函数内部检查我们正在处理的文件是否是单词/ excel文件)...

Finally, initiating the conversion of a Word / Excel file from within PHP (I have it within a function that checks if the file we are dealing with is a word / excel document)...

//use openoffice.org
$output = array();
$return_var = 0;
exec("/opt/adocpdf {$directory} {$filename} {$extension}", $output, $return_var);

一旦将Word / Excel文件上传到服务器,就会调用此PHP函数。 exec()调用中的3个变量直接与上面的纯文本脚本开头的3相关联。请注意,如果转换文件在Web根目录下,则$ directory变量不需要前导斜杠。

This PHP function is called once the Word / Excel file has been uploaded to the server. The 3 variables in the exec() call relate directly to the 3 at the start of the plain text script above. Note that the $directory variable requires no leading forward slash if the file for conversion is within the web root.

可以,就是这样!希望这对一个人来说是有用的,并保存他们遇到的困难和学习曲线。

OK, that's it! Hopefully this will be useful to someone and save them the difficulties and learning curve I faced.

这篇关于使用PHP将Word doc,docx和Excel xls,xlsx转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆