1024programmer PHP PHP realizes conversion of Word, excel, etc. to pdf

PHP realizes conversion of Word, excel, etc. to pdf

I recently worked on a project that required converting word and excel documents uploaded by users into PDF documents, saving and printing them. I found a lot of information on the Internet, but it was not comprehensive, so I wrote a relatively comprehensive tutorial to share.

The following are the steps:

1. To install the free openOffice software, please go to openoffice.org to download the latest version.

2. JDK support, please search and download the latest version of JDK.

3. After installing openOffice, enter Dcomcnfg in Start-Run to open the component service. In Component Services?Computer?My Computer?DCOMP Configuration, select

Right-click Properties on these two items to open the properties panel as shown below:

Select the Security tab, click Customize on startup and activation permissions and access permissions, and add permissions for Everyone.

Select the Identity tab and select Interactive User.

4. After installing openOffice, please open it once to confirm that the software can run normally, then exit and run the following commands from the command line.

First go to the installation directory, for example: C:\Program Files\OpenOffice 4\program\

Execute the command:

soffice -headless-accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard

The software will be run in the background after success.

5. If it is a version before php5.4.5, you need to turn on com.allow_dcom = true in php.ini, that is, remove the preceding semicolon. If it is a later version, you need to add a line extension=php_com_dotnet.dll to php.ini, and then check whether the dll file exists in the ext directory of php. If not, please download the corresponding version of dll yourself. Then restart apache or IIS server.

6. Code implementation

/** * Convert office documents to PDF class* @author jinzhonghao  created 2015-04-23 */class office2pdf{ private $osm; public function __construct() { $this->osm = new COM("com.sun.star.ServiceManager")or die ("  Please be sure that OpenOffice.org is installed.n"); } public function MakePropertyValue($name,$value) { $oStruct = $this->osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue")  ; $oStruct->Name = $name; $oStruct->Value = $value; return $oStruct; } public function transform($input_url, $output_url) { $args = array($this->MakePropertyValue("Hidden",  true)); $oDesktop = $this->osm->createInstance("com.sun.star.frame.Desktop"); $oWriterDoc = $oDesktop->loadComponentFromURL($input_url,"_blank", 0, $args)  ; $export_args = array($this->MakePropertyValue("FilterName","writer_pdf_Export")); $oWriterDoc->storeToURL($output_url,$export_args); $oWriterDoc->close(true); return $this->getPdfPages  ($output_url); } public function run($input,$output) { $input = "file:///" . str_replace("\\","/",$input); $output = "file:/  //" . str_replace("\\","/",$output); return $this->transform($input, $output); } /** * Function to get the number of pages in a PDF file * The file should be  Readable by the current user (under Linux) * @param [string] $path [file path] * @return int */ public function getPdfPages($path) { if(!file_exists($path)) return 0; if(!is_readable  ($path)) return 0; // Open the file $fp=@fopen($path,"r"); if (!$fp) { return 0; } else { $max=0; while(!feof($  fp)) { $line = fgets($fp,255); if (preg_match('/\/Count [0-9]+/', $line, $matches)) { preg_match('/[0-9]  +/',$matches[0], $matches2); if ($max<$matches2[0]) $max=$matches2[0]; } } fclose($fp); // Return the number of pages return $max  ; } }}

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/php-realizes-conversion-of-word-excel-etc-to-pdf/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索