📜 ⬆️ ⬇️

Convert Word documents to php using COM (windows + apache)

Good day Habr!
I want to talk a little bit about what needs to be done when using a COM object to convert word files to any of the available formats . I myself faced a lot of problems, I had a lot of forums, there are practically no Russian manuals. I decided to write, more for myself, but suddenly someone else will come in handy?

If you have IIS, there is a possibility that you will not need it, the article is exclusively about setting up work under Apache

The first and main problem that caused brain rupture:
Call to undefined method variant::SaveAs()
when trying to save a document.
Experiments often led to errors in the absence of a property in an object or document parsing errors, the most common
Fatal error: Uncaught exception 'com_exception'
with message ' Source: Unknown<br/> Description: Unknown'


What you need to do to ensure that the process still earned:
  1. Need server screw
  2. MsOffice package installed on it (or not the whole package, but only the necessary parts of it)
  3. Setting permissions for COM
    1. Next, go to Start-> Administrative Tools-> Component Services
    2. Expand the menu Component Services-> Computers-> My Computer-> DCOM Config
    3. In the list we are looking for the COM applications we need (as a rule, they all begin with the words Microsoft ...)
    4. Click the right button for the item of interest, select Properties, go to the Securety tab in the Access Permissions section, put a dot in Customize and click on the Edit button
    5. In the window that appears, click add and add a user who has rights to start apache (enter the user’s login in the bottom field and click Check Names if everything is fine, then Windows will find it, click ok)
    6. In the list, select the added user and tick him Remote Acceess
    7. Confirm all changes (click ok 2 times)
    8. Repeat steps 4-7 for desired applications.

  4. Apache setup
    1. After the rights to COM are distributed all in the same window (Component Services) go to the Services section we find there apache click on it right click select Properties go to the Log On tab click on the Browse button enter the same user specified above for COM Objects, click OK, enter the password for the user account so that apache can start. Click OK.
    2. Restart apache, if everything is correct, then restart will be successful

  5. Configure php.ini
    1. Open php.ini
    2. We are looking for section [COM]
    3. Break the lines:
        com.allow_dcom = true com.autoregister_typelib = true com.autoregister_casesensitive = false com.autoregister_verbose = true 


      We are looking for the section Dynamic Extensions
      Add a line:
        extension=php com_dotnet.dll 

      ')
      Restart apache
    4. Important: after setting rights to COM objects, you need to start the word (at least I haven’t changed my permission without this permission)
    5. Profit


    PS Do not forget to kill the Word process in case of unsuccessful experiments, as it starts, opens the file for reading and hangs.

    Then you can use standard COM functions for working with an object, for example, like this:

     com_load_typelib('Word.Application'); $name = 'test.docx'; $path = dirname(__FILE__) . "\\tmp\\". $name ; $word = new COM("word.application") or die ("  COM "); $word->Documents->Open( $path ); mkdir(dirname(__FILE__) . "\\tmp\\{$name}"); $folder = dirname(__FILE__) . "\\tmp\\{$name}"; $word->ActiveDocument->SaveAs( $folder . "\\{$name}.html", 8); $word->Quit(); $word = null; 

Source: https://habr.com/ru/post/162473/


All Articles