Search code examples
phppdfwindows-server-2008docx

Word Doc File to PDF on Windows Server


I am trying to convert .docx files to pdf using PHP on a Windows server. I tried several of the solutions from other posts, including phpdocx (which does a very poor conversion that doesn't keep any formatting) and php's Com object. I only have Office 2003, so there is no pdf converter available using Com.

I thought of using OpenOffice/LibreOffice, but haven't found any information about installing and using Com for these on a windows server (I know it can be installed, but I can't figure out how to set up Com for it).

Using a webservice is not an option due to the data on the forms (they have to remain on our server). This means that Zend Framework cannot be used.

Any suggestions would be helpful, or information about using Com with Open Office.


Solution

  • I was finally able to get this working. OUr problem was that Word 2003 didn't have a PDF converter in it. We ended up using a trial version of Office 2010 for now (assuming that everything works correctly, we will purchase the full version). Word 2007 would have worked also. Below is the code I used to get this working:

                    //Word Doc to PDF using Com
                ini_set("com.allow_dcom","true");
    
                try{
                    $word = new com('word.application') or die('MS Word could not be loaded');
                }
                catch (com_exception $e)
                {
                        $nl = "<br />";
                        echo $e->getMessage() . $nl;
                        echo $e->getCode() . $nl;
                        echo $e->getTraceAsString();
                        echo $e->getFile() . " LINE: " . $e->getLine();
                        $word->Quit();
                        $word = null;
                        die;
    
                }
    
                $word->Visible = 0;
                $word->DisplayAlerts = 0;
    
    
    
    
    
                try{
                $doc = $word->Documents->Open(DOC_LOCATION. 'test_image.docx');
                }
                catch (com_exception $e)
                {
                    $nl = "<br />";
                    echo $e->getMessage() . $nl;
                    echo $e->getCode() . $nl;
                    echo $e->getFile() . " LINE: " . $e->getLine();
                    $word->Quit();
                    $word = null;
                    die;
                }
                echo "doc opened";
                try{
                    $doc->ExportAsFixedFormat(DOC_LOCATION . "test_image.pdf", 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
    
                }
                catch (com_exception $e)
                {
                    $nl = "<br />";
                    echo $e->getMessage() . $nl;
                    echo $e->getCode() . $nl;
                    echo $e->getTraceAsString();
                    echo $e->getFile() . " LINE: " . $e->getLine();
                    $word->Quit();
                    $word = null;
                    die;
                }
    
                echo "created pdf";
                $word->Quit();
                $word = null;