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 ; } }}