PHP implementation Word/excel/ppt converted to PDF

Source: Internet
Author: User

The previous period was responsible for the design of the company's internal document platform, one of which was to be able to browse online user-uploaded Office files.

My idea is to convert office to PDF first, and then parse the PDF file through the Pdf.js plugin so that it can be viewed under any browser.

You can use the COM component of PHP to invoke other applications that can handle Office files and convert PDF files using the provided interface.

OpenOffice

OpenOffice is a set of open-source, cross-platform office software that is maintained by many free software professionals, so that you can get free office Access outside of Microsoft office.

OpenOffice is compatible with Microsoft's Office software suite to convert files such as doc, XLS, PPT, and so on to PDF format, with absolutely no difference in functionality than Microsoft Office.

OpenOffice Official website:http://www.openoffice.org/

OpenOffice Download:http://www.openoffice.org/download/index.html

OpenOffice requires Java support, make sure that the JDK is installed and that the JRE environment variable is configured.

1. Configuring Component Services

OpenOffice after the installation is complete, press win+r shortcut key to enter the Run menu and enter DCOMCNFG to open the component service.

[ Component Services ] >> [ computer ] >> [ My Computer ] >> [DCOM config ] >> [OpenOffice Service Manager]

Right-click on the Properties panel, select the Security tab, and tick customize on launch and activation permissions and access permissions to add Everyone's permissions.

↑ Startup and Activation permissions and access are all used with custom configuration

↑ Add the Everyone user group, remember to check the name before confirming

↑ Two custom configurations are the same, allowing everyone to have all permissions

Select the Identity tab, tick the Interactive user , and then exit after saving the settings.

2. Running the software in the background

After you install OpenOffice, you need to start a confirmation that the software is working, and then open the command line to run the following command:

Switch to the installation directory: CD C:\Program files\openoffice 4\program

Run the software in the background: soffice-headless-accept="socket,host=127.0.0.1,port=8100;urp; " -nofirststartwizard

PS: This command only needs to be executed once, it can keep the software running in the background, even if restarting the server is not affected.

3. Configure PHP Extensions

If it is a previous version of PHP5.4, you need to open com.allow_dcom = True in php.ini (i.e. remove the preceding semicolon).

If it is a PHP5.4 version, add a line extension extension = Php_com_dotnet.dll in php.ini.

Restart the Apache or IIS server, print the phpinfo () information, and check that the com_dotnet extension is turned on.

↑ Check PHP ext directory for Com_dotnet.dll files, if not please download the corresponding version of the DLL yourself

4. Implement file Conversion

PDF Conversion Tool (support for Doc, docx, xls, xlsx, PPT, pptx and other formats)

classpdfconverter{Private $com; /** * need to install OpenOffice and run in the background * soffice-headless-accept= "Socket,host=127.0.0.1,por t=8100;urp; "-nofirststartwizard*/     Public function__construct () {Try {            $this->com =NewCOM (' Com.sun.star.ServiceManager '); } Catch(Exception $e) {             die(' Please be sure this openoffice.org is installed. '); }    }    /** * Execute PDF file (absolute path) conversion * @param $source [source file] * @param $export [Export fil E]*/     Public functionExecute$source,$export)    {        $source= ' file:///'.Str_replace(‘\\‘, ‘/‘,$source); $export= ' file:///'.Str_replace(‘\\‘, ‘/‘,$export); $this->convertprocess ($source,$export); }    /** Get the PDF pages * @param $pdf _path [Absolute Path] * @return int*/     Public functionGetPages ($pdf _path)    {        if(!file_exists($pdf _path))return0; if(!is_readable($pdf _path))return0; if($fp=fopen($pdf _path, ' R ')) {            $page= 0;  while(!feof($fp)) {                $line=fgets($fp, 255); if(Preg_match('/\/count [0-9]+/',$line,$matches)) {                    Preg_match('/[0-9]+/',$matches[0],$matches 2); $page= ($page<$matches 2[0])?$matches 2[0]:$page; }            }            fclose($fp); return $page; }        return0; }    Private functionSetProperty ($name,$value)    {        $struct=$this->com->bridge_getstruct (' Com.sun.star.beans.PropertyValue '); $struct->name =$name; $struct->value =$value; return $struct; }    Private functionConvertprocess ($source,$export)    {        $desktop _args=Array($this->setproperty (' Hidden ',true)); $desktop=$this->com->createinstance (' Com.sun.star.frame.Desktop '); $export _args=Array($this->setproperty (' filtername ', ' Writer_pdf_export ')); $program=$desktop->loadcomponentfromurl ($source, ' _blank ', 0,$desktop _args); $program->storetourl ($export,$export _args); $program->close (true); }}
pdfconverter.php

Use Pdfconverter (absolute path must be passed in)

$arr=Array(' Doc ', ' docx ', ' xls ', ' xlsx ', ' ppt ', ' pptx ');$converter=NewPdfconverter ();foreach($arr  as $ext) {    $source= __dir__. '/office/test. '$ext; $export= __dir__. '/pdf/test. '$ext. '. pdf '; $converter->execute ($source,$export); Echo' <p> '.$ext. ' Done</p> ';}

5. View PDF document

Finally share a HTML5-based PDF Reader plugin pdf.js, a JS library of Mozilla Labs ' open source on GitHub, designed to read PDF files.

Because it is Mozilla's product, it is very good in Firefox and can be used as long as it is a browser that supports HTML5.

Project address:https://github.com/mozilla/pdf.js

Plugin Download:http://mozilla.github.io/pdf.js/

↑pdf.js cannot open a local PDF file, but you can open a file on the server via a URL, and do not support cross-domain browsing of PDFs

Usage: 1) Unzip the plugin, place it in the root directory of the website, 2) Add the file parameter to specify the PDF path via the URL access viewer.html;3).

For example: http://localhost/pdfjs/web/viewer.php? file=/office/example.pdf

PHP implementation Word/excel/ppt converted to 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.