Practical phpdocx

Creating a new document

Introduction

Once the phpdocx installation is completed, you can work with the library. To begin with, we are going to generate a document from scratch, that is, without external templates.

To do this, you'll create an example as simple as possible in which you'll add a text line and then, save the document with a name of your choice.

The version that supports the namespaces package is available for the Advanced and Premium licenses.

Whether it is necessary to create a single document or hundreds of thousands, phpdocx simply is the perfect tool.

Due to phpdocx internal performance, it is required to instance a new object for every file you generate. Otherwise, there may be unexpected results in the file content.

No-namespaces version

The full code for creating a document is the following:

First, include the main class of phpdocx CreateDocx:

This class loads automatically the rest of the available classes through an autoloader.

Then, create a new document:

You are going to add text, so add a string variable:

... and then add it to the document with the method addText:

To, finally generate the document:

With the file name tutorial_2.docx

Also, you could add the text directly without intermediate variables:

Namespaces version

The full code for creating a document is as follows:

The differences between this and the no-namespaces version are limited to how you include the library and classes.

Include the main class, as it manages the loading of the rest of the classes:

Now you can create the new document:

If you're using a framework based on Composer such as Symfony, Laravel..., you need the namespace package (available for Advanced and Premium licenses). You can also learn more about using phpdocx with frameworks and CMS in the Cookbook.

Destination path/route

When generating the document with the createDocx method, you can specify a relative or absolute path, to indicate the name and path where the file is going to be saved.

For example:

saves the document in the script execution directory.

generates the document in the sub-directory files, located one level above the current directory; or

saves it in the path /var/phpdocx/files.

Before setting a path for saving your files, it is necessary to check that the execution user has the required read and write access rights.

Downloading the document

phpdocx includes the createDocxAndDownload method to generate and download the document in just one step:

For example:

This download uses the following headers:

These headers work in most configurations, but in some cases might be insufficient. When this happens, it is necessary to create the document with the createDocx method and use your own download headers.

Documentation

With each package of phpdocx, full documentation comes in phpdocx file format, which is the main reference to know in depth the software library. All public methods go along with a description of each of the available parameters.

In addition, it is advisable to visit:

Other useful resources

For further information and to solve doubts about the use of the library, there are several resources, both internal and external:

In addition, it is advisable to visit:

  • phpdocx forum

    Forum of the great community of phpdocx users, to solve doubts and queries

  • Contact

    Contact form, for direct communication with the phpdocx team

  • Stack Overflow

    Reference forum for developers

Next - Working with templates