Download the generated DOCX

The createDocx phpdocx method generates DOCX files in the file system, but does not download them on the spot, for example, when using a browser.

To generate and download the output files you can either choose the methods available in the library or work with some custom PHP code.

The createDocxAndDownload method

All phpdocx licenses include the createDocxAndDownload method to create and download the DOCX. A simple example of use of this method would be as follows:

The download of the files happens thanks to a set of headers for DOCX files:

Alternative download code

The headers of the previous method are the standard ones for DOCX files, and so, are compatible with the majority of servers. However, in case of not functioning correctly, it is possible to work with any other method and headers to download the files. In this case, the first thing to do is generating the document with the createdDocx method, and then typing a custom code for the download.

An alternative method for the download would be the PHP ob_ functions, e.g.:

Delete files after the download

The createDocxAndDownload method creates and downloads a document, but the default options don't delete it. If you want to do so after the download, you must set the second parameter as true.

E.g., to generate and download a DOCX, and delete it afterwards, write this PHP code:

Streams of files (Premium licenses only)

The package of the Premium licenses integrates the option of DOCX generation as a stream, that is, without saving the document in the system files, and thus obtaining it directly in the browser or getting the output in the CLI mode.

You can set up dinamically this stream way in a script, or force it for all documents in the configuration file config/phpdocxconfig.ini.

The following example sets up the stream mode of phpdocx:

The CLI mode with streams allows to redirect the output, e.g., to a file: