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

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

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

    The first and main problem that caused a brain rupture:
    Call to undefined method variant::SaveAs()
    when trying to save a document.
    The experiments often led to errors of the absence of a property in the object or errors in parsing the document, the most common - What needs to be done in order for the process to work anyway:
    Fatal error: Uncaught exception 'com_exception'
    with message 'Source: Unknown
    Description: Unknown'

    1. Need a Windows server
    2. The MsOffice package installed on it (or not the whole package, but only the necessary parts)
    3. Resolution Setting 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 look for the COM applications we need (usually they all start with the words Microsoft ...)
      4. Right-click on the interesting one, select Properties, go to the Securety tab in the Access Permissions section, put a point in Customize and click on the Edit button
      5. In the window that appears, click add and add the user who has the rights to run apache (enter the username in the bottom field and click Check Names; if everything is ok, then Windows will find it, click ok)
      6. In the list, select the added user and check the Remote Acceess checkbox.
      7. Confirm all changes (click ok 2 times)
      8. Repeat steps 4-7 for desired applications

    4. Configure apache
      1. After the rights to COM are distributed all in the same window (Component Services), go to the Services section, find apache there, right-click on it, select Properties, go to the Log On tab, click on the Browse button, enter the same user as indicated 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 the restart will succeed

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

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

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

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

      Further, you can use standard COM functions to work with the object, for example, like this:

      $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 = null;

    Also popular now: