Learn phpdocx in 5 minutes

Quickstart guide

Information

phpdocx is the most comprehensive library for working with DOCX documents. It comprises all the necessary methods to generate new documents, customize templates, insert and modify contents, define and apply styles, transform formats such as HTML and CSS, convert to PDF, encrypt, digitally sign and many other functionalities.

All available methods are very easy to use, and they allow to perform effortlessly any task, from the simplest to the most complex one.

Creating new documents

The CreateDocx class generates a new DOCX in which to add contents.

The following example creates a new document with a paragraph and a list and saves the DOCX to the file system:

Almost any MS Word content can be added with phpdocx. That includes images, tables, charts, headers, footers, comments, footnotes, endnotes, fields, sections, breaks, TOCs, headings, tags, settings...

When including the CreateDocx.php file, the phpdocx internal Autoloader is automatically loaded in order to access all the available classes.

Advanced and Premium licences come with a PHP namespaces package, not available in the Trial one.

Working with templates

Instead of generating a document from scratch, it is possible to do it with MS Word, LibreOffice, Google Docs or any other program in order to create a DOCX in which to include placeholders and other contents. phpdocx counts with the necessary methods to replace those placeholders with new contents using the CreateDocxFromTemplate class.

The following example opens a DOCX template, replaces the NAME placeholder with a new text and then saves the DOCX to the file system:

phpdocx offers methods to replace placeholders in lists, tables, images, WordFragments... and to insert blocks to be grouped and work with different contents.

By default, placeholders are defined by the $ symbol, which can be customized with others such as ${ }, |, @, { }, #{ }. [[ ]]...

WordFragments

In phpdocx, a WordFragment is an object that can contain one or more elements. This allows to add elements to headers, footers, footnotes, comments, endnotes and complex contents, and to perform advanced replacements.

The following example creates a WordFragment with three contents, two texts and one image, and then replaces a placeholder with it:

Knowing how to properly operate with WordFragments is essential in order to get the most of phpdocx.

HTML and CSS

One of the most widely used features of phpdocx is HTML and CSS to Word conversion. It is a really easy way of adding contents to new documents, as well as replacing placeholders, and it is compatible with many tags and styles.

E.g., in order to add two paragraphs and a table:

Premium licenses have HTML Extended and CSS Extended features, made for adding contents and applying styles not defined in the standard HTML, such as footnotes, headers, page numberings, charts and others.

DOCX to HTML/CSS transformation is done with the TransformDocAdvHTML class, available in Advanced and Premium licences.

PDF and other document formats

Advanced and Premium licenses include the conversion plugin to transform DOCX to PDF, DOC to DOCX, RTF to DOCX, and other conversions.

The transformDocument method is the responsible of the task. E.g., this transforms a DOCX created from scratch with phpdocx or with templates, to PDF:

phpdocx CLI command

Premium licenses include phpdocx CLI command to speed up developments with phpdocx by generating code skeletons automatically, check server settings, show recommendations, return information and other useful commands.

JavaScript API

Premium licenses include a JavaScript API to work with DOCX documents using nodejs and web browsers.

For more information

Besides this brief guide, we recommend visiting the following pages to understand phpdocx in depth: