In a company where I once worked because of a customer's requirements, a team needed to implement a tremendous gambiarra customer-generated contract solution, 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 retrieve data in Json format
Merge the data with the document using some black magic through the Word API
Save the document to a location specified in the user's HD through the Word API
Obviously, only one big (coff! coff !!) browser with Internet Explorer provides an "API" for all that.
I'm talking about ActiveXObject
. Here's 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 you can use any of the Office Interoperability API .
Server-side solution with ActiveX
I knew some systems that implemented the above solution in Java on the server side.
The problem is that because the Office instance in Windows is unique, the system does not support concurrent calls.
In addition, if there were any problems 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 accounts of the person in charge of the solution was that Office often displayed update dialog boxes or something unexpected, and this prevented the document from being manipulated. The solution was to log in remotely on the server and click the dialog button that was locking the server.
In short: Office was not meant for this.
Server-side solution with XML manipulation.
The new Office format (from 2007) is nothing more than a ZIP made up of multiple files, within which documents are simply XML files. Hence the extension ending with "X" (docx, pptx, xlsx).
Another project I knew was having an unzipped document on the server. Then, when the user requested a contract for a particular client, a routine manipulated the file document.xml
(which is in the word
folder of a document's file structure). 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 bit about an office document structure :
ItwouldnotbesocomplextoimplementinPHP,thatis:
XMLHandlingFilecompressionButwait!Infacttherearealreadyseverallibrariesthatcandothis:
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 the POI (free) or Aspose (paid)
Generate PDF, which PHP can generate natively
Generate XLS, which PHP can also generate
Generate HTML with "docx" extension, because WORD can usually load the file ( looks at gambiarra!)
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.