Practical phpdocx

Adding content to a document


To explain how the library works, let's create a new empty file and add different types of content to it: heading, paragraphs, lists, tables and page breaks.

New document

As a first step, create the document, instantiating the class CreateDocx:

Now, add the new contents for this $docx object, always following the visualization order.


Headings are strings highlighted in the document. To add a heading, call addHeading:

$text is the string, $level is the heading level and $options is the text properties as color, spacing or bold properties, underline, and so on.

For this document insert a text line with color and font size 24:


The method for adding paragraphs is addText:

The first parameter can be a string or an array. In this array, you can add keys and values to modify the text, color, font type, bold style or size.

The second parameter is optional. It defines the properties of the paragraph, like background color, border, alignment and spacing.

Now, add to the document a string with background and text color:

If you prefer to add two or more strings, create a new array with the texts. For example:

The ‘text’ key is used in every position of the array for adding the text. The bold style is enabled in the second string only.


addList is the method for adding lists to a document:

This method includes the following parameters:

  • $data, array with the information to add to the list
  • $styleType, for list types: '0', list without styles; '1', unordered lists; '2', ordered lists; or a custom name to use other list types
  • $options, for colors and other text properties

Let's add an ordered list of Internet browsers to the document:

To nest lists, just add new arrays in the values array. For example:


Including tables is very similar to adding lists, but you must work with nested arrays to define the text and its properties.

You need to use addTable:

The $taleData parameter is an array which can contain strings or WordFragment objects and the cell specific properties. The other two parameters define the table global properties and particular row properties such as borders, sizes and spans.

Now, insert the usage statistics for the browsers previously defined for the list in the document:

Page and line breaks

To make page or line breaks, use addBreak:

You just need to indicate the type of break, ‘line’ or ‘page’, and the number of breaks to apply.

For example, to make a page break:


For adding a link, use the addLink method:

The options array defines the link destination and the text properties.

Here's a simple example:

This code links the text to the URL with a color and no underline.

Comments, endnotes and footnotes

Adding comments, endnotes and footnotes require generating a WordFragment with the contents and using addComment, addEndnote or addFootnote.

For example, to add a new comment in the document:

Tips and tricks

It's not necessary to call the addText method for each line you want to add. Thanks to the option parseLineBreaks you can make line breaks using "\n":

Adding WordFragments is possible both for tables and lists. It allows to create and insert values with many styles and contents.

RTL contents are supported: Right to left languages.

Next - Adding content to a template