Sign in Registration
ruen

DOCX format device, how to open it in PHP for data output

A site today is not just a page, but in most cases a whole system for processing and generating various data. It is often necessary to display data from a site in some common format, for example, PDF, DOCX, CSV, etc. Let's look at how you can output data to a DOCX file in PHP .

A little about the DOCX format. It is a file extension for the Microsoft Office program, a Word product. Microsoft Word is a word processor that is designed to create, view and edit text documents. Previously, Word mainly used the binary file format, the DOC extension. But, it was replaced by a more perfect format - DOCX. How does DOCX work ?

DOCX is just an archive that contains all the necessary files for the document, the document itself is stored in XML format. This makes it easy to open a PHP archive and output the desired data to an XML file inside a DOCX container. To better understand what is contained in such a file, you can take any archiver and open the DOCX file with it. Inside you will see a certain structure of folders and files.

docx-structure

The main interest is the word folder , which contains the main content of the DOCX file. As you can see, they tried to make all files and settings in this format with the XML extension, this is not without reason, because the X at the end means the relation to XML. In the picture below you can see the contents of the word folder, the main file here is document.xml .

docx-structure-word

It is the document.xml file that contains all the main content of the DOCX document. Working with the XML format is quite simple using any means, because this format resembles ordinary text, only it is clearly structured, which allows you to easily access any point in the file and change any data.

To open DOCX in PHP and write the required data there, you can use a fairly simple code:

  $ docx = new ZipArchive ();

if ($ docx-> open ('path_to_file / file.docx') === true) {
$ xml = $ docx-> getFromName ('word / document.xml');

$ xml = str_replace ('[SEARCH TEXT]', 'REPLACEMENT TEXT', $ xml);

$ docx-> addFromString ('word / document.xml', $ xml);

$ docx-> close ();
}
 

First, a ZIP archive object is created, into which the DOCX content is read, and the document.xml content is retrieved from the DOCX. Then the data is written directly to the file, it is simply looking for a previously prepared place in the DOCX file in the form of the token [LOOKING FOR TEXT] and any data is written in its place. After performing this operation, the updated document.xml is written to the DOCX and the archive is closed.

It is important to maintain the integrity of the tokens if the text is replaced in this way. This can be done quite simply. First, open the required file in Word and arrange the tokens, it is advisable to write them in a simple notepad and then insert them in the required positions. After that, save the DOCX file and open it with an archiver, find and open document.xml in it - check the integrity of the tokens, they should be written together, without gaps. Only in this case, correct replacement of tokens with the required text is possible.

Thus, the DOCX format device was examined, and it was also shown how you can easily write data to DOCX in PHP .

Comments (0)
For commenting sign in or register.

Latest articles

Popular sections

Eqsash (Tools)

Android app - VK LAST USER ID, отучитель от зависимости и т.д.:
Available on Google Play

Amessage (Communication)

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to

YouTube

Books

IT notes - In simple language about the most necessary things (HTML, CSS, JavaScript, PHP, databases, Drupal, Bitrix, SEO, domains, security and more), PDF, 500 p.