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::
- Open a template document via a URL in Word
- Access a REST Web Service via Ajax and recover data in Json format
- Merge the data with the document using some black magic through the Word API
- 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:
It would not be something so complex to implement in PHP, that is to say:
- Manipulation of XML
- File compression
But wait! Actually there are already several libraries that can do this:
- Phpword (free)
- docxgen (free)
- PHPDOCX (paid)
Conclusion and other alternatives
My recommendation is not to do this and consider the following alternatives:
- Use a second language like Java with an API that does not use Office, such as POI (free) or Aspose (paid)
- Generate PDF, which PHP can generate natively
- Generate XLS, which PHP can also generate
- 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.
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.
– blackblather
You want to open the word on the server or on the client’s computer?
– Felipe Avelar
On the client computer, I tried using shel_exec(); in PHP but, as the code runs on the server, word opens on the server.
– blackblather