Practical phpdocx

Adding content to a document

Introduction

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

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:

Paragraphs

The method for adding paragraphs is addText:

The first parameter can be a string or a string 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 font type is enabled in the second paragraph only.

Lists

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
  • $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:

Tables

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:

Links

For adding a link, use the addLink method:

The options array defines the link destination and the text properties, as well as the text used as a link in the first paragraph.

Here's a simple example:

This method links the text to the URL www.phpdocx.com with a color and no underline.

Tips and tricks

It's not necessary to call the addText method for each paragraph you want to add. Thanks to the option lineBreak you can make a line break before and/or after the chain.

For example:

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

Next - Adding content to a template