How to open a file in MS-Office with Javascript or PHP?

Asked

Viewed 1,504 times

2

I have a website that works only on a local network, where I have several files stored on the web application server, and I would like to somehow use PHP or Javascript to open it directly with MS-Office, from the client side.

This way I could open the file with the Word application, and make the necessary changes and, while saving, I would update the server.

I don’t know if it’s possible or not, but I’d like to know.

  • I have read that, for security reasons, it was not possible, and I have also read methods in which create a new instance of the active class.

  • 1

    You want to open the word on the server or on the client’s computer?

  • On the client computer, I tried using shel_exec(); in PHP but, as the code runs on the server, word opens on the server.

1 answer

8

In a company I worked for once because of a client’s requirements, a team needed to implement a tremendous gambit contract generation solution in client, more specifically, in the browser via Javascript.

Client-side solution with Activex

This solution consisted of the following procedures::

  1. Open a template document via a URL in Word
  2. Access a REST Web Service via Ajax and recover data in Json format
  3. Merge the data with the document using some black magic through the Word API
  4. Save the document to a specified location on the user’s hard drive via the Word API

Obviously, only one great (coff! coff!!) Internet Explorer browser provides an "API" for all this.

I’m talking about the ActiveXObject. See a basic example of how to "open word":

var oApplication = new ActiveXObject("Word.Application");
oApplication.Visible = true; //Deixa o Word no modo invisível
oApplication.Documents.Open("template_contrato.docx");
var oDocument = oApplication.ActiveDocument;

From there it is possible to use any methods of the Office Interoperability API.

Server-side solution with Activex

I met some systems that implemented the above solution in Java on the server side.

The problem is that as the Office instance in Windows is unique, the system did not support concurrent calls.

In addition, if there was any problem the routine was blocked and the server had to be manually accessed, for example, to terminate the instance of Office that was having problems.

One of the reports by the person responsible for the solution was that Office often showed dialog boxes about updating or anything else unexpected and this prevented the document from being manipulated. The solution was to remotely log into the server and click on the dialog button that locked the server.

In short: Office is not made for this.

Server-side solution with XML manipulation.

The new Office format (as of 2007) is nothing more than a ZIP composed of several files, within which documents are simply XML files. Hence the extension ending with "X" (docx, pptx, xlsx).

Another project I knew was to have an uncompressed document on the server. So when the user asked for a contract for a particular client, a routine manipulated the file document.xml (which is in the folder word the file structure of a document). The client data was replaced in places marked by "tags" and then the folder was compressed into a new DOCX and made available to the user via download.

See a little about the structure of an office document:

Estrutura Interna de um Documento do Office

It would not be something so complex to implement in PHP, that is to say:

  1. Manipulation of XML
  2. File compression

But wait! Actually there are already several libraries that can do this:

  1. Phpword (free)
  2. docxgen (free)
  3. PHPDOCX (paid)

Conclusion and other alternatives

My recommendation is not to do this and consider the following alternatives:

  1. Use a second language like Java with an API that does not use Office, such as POI (free) or Aspose (paid)
  2. Generate PDF, which PHP can generate natively
  3. Generate XLS, which PHP can also generate
  4. Generate an HTML with extension "docx", as WORD can usually load the file (look at the girl!)

Anyway, your creativity is the limit.

Just be aware that every choice has its consequences. Some will have new requirements on the server side, others for the client.

  • 1

    Or it can "do" docx as well by compressing all files in required format.

  • 1

    @Felipeavelar Yes, of course! How could I forget...

  • @blackblather Open Activex, as explained in the reply, and simply keep the document open. You can even configure clients' machines to receive a remote command, but this would only work on an intranet if there is no firewall or proxy in the way. Another alternative is to make a Java Applet just for this. Still another alternative is to configure the clients' browser to open the word directly when downloading the DOC file, just like it happens with PDF in some cases. Then you can point to the file URL and ready.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.