PHP Implementation Word,excel Convert PDF

Source: Internet
Author: User

Recently, to do a project, you need to convert the user upload Word,excel document into a PDF document to save and print, find a lot of information on the Internet, not comprehensive, so I wrote a more comprehensive tutorial to share.

Here are the steps to proceed:

1, install the free OpenOffice software, please go to OpenOffice.org download the latest version.

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

3, after the installation of OpenOffice, in the start-run input DCOMCNFG open Component Services. In the Component Services-computer-My Computer-dcomp configuration, select

On these two items, click the Right button property, open the properties panel such as:

Select the Security tab, and then click Customize on the start and Activate permissions and access permissions to add the permissions for everyone.

Select the Identity tab and select the Interactive user.

4, after the installation of OpenOffice, please first confirm that you can run the software properly, and then exit and then run the following command with the command line.

First to the installation directory, for example: C:\Program files\openoffice 4\program\

Execute command:

Soffice-headless-accept= "SOCKET,HOST=127.0.0.1,PORT=8100;URP;"-nofirststartwizard

After success, the software is run in the background.

5, if it is php5.4.5 previous version, need to php.ini com.allow_dcom = True Open, that is, remove the preceding semicolon. If it is a later version, you need to add a line extension Extension=php_com_dotnet.dll in the php.ini, and then check the PHP ext directory for the DLL file, if not please download the corresponding version of the DLL. Then restart the Apache or IIS server.

6. Code implementation

/** * Office documents converted to PDF class * @author Jinzhonghao <[email protected]> created 2015-04-23 */class office2pdf{private $OSM;p ublic function __construct () {$this->osm = new COM ("Com.sun.star.ServiceManager") or Die ("Please be sure that Ope Noffice.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);} /** * Get the number of pages in a PDF file The file should be readable for the current user (Linux) * @param [string] $path [File path] * @return int */public function getpdfpages ($pa TH) {if (!file_exists ($path)) return 0;if (!is_readable ($path)) return 0;//open file [email protected] ($path, "R"); $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], $matches 2), if ($max < $matches 2[0]) $max = $matches 2[0];}} Fclose ($FP);//Returns the page return $max;}}}



PHP Implementation Word,excel Convert PDF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.