xmldocx tutorial

The best place for beginners to start

Introduction

xmldocx is a library for generating DOCX documents from scratch and with templates. With the help of xml, it is possible to run any programming language to work with xmldocx.

xmldocx runs in a decentralized way, apart from external servers. It doesn't connect to the internet and, as a consequence, the information is secure all along the process.

Besides creating new documents and working with templates, you can also export documents to PDF and HTML, and have access to advanced features like headers, footers, infographics, MathML, merge, encoding or signing, among others.

You can find the full list of available features, with its examples, on this URL:

http://www.xmldocx.com/documentation

Installation

Internally, xmldocx runs phpdocx (http://www.phpdocx.com) as a tool for document generation. The only requisite for using xmldocx is to install PHP 5.2 or higher in the server. The PHP ZipArchive, XSLT and Tidy extensions must also be installed.

It's not necessary to install any web server, as xmldocx executes PHP in command line mode.

It's possible to choose any PHP-supported operating system to execute xmldocx. The PHP set up in the most common OS is as follows:

  • Windows: Download the installer from the official website: http://windows.php.net/download. You can also use popular tools like XAMPP or WAMP, but apart from PHP, these tools include unnecessary programs as databases and web servers
  • Linux: All main distributions count with official packages so you can install PHP from their official repositories.
  • Mac OS X: Mac offers many installation methods. One of the easiest ones is http://php-osx.liip.ch, where you can download an installer.

There's one important fact: PHP must be a global executable of the operating system you are using.

After installing PHP, download xmldocx and uncompress the ZIP file in any folder you want. That ZIP package contains all the necessary files to run xmldocx, including phpdocx. You can find the classes to work with any programming language in the wrappers folder.

Executing the first example

xmldocx is very easy to manage, no matter the coding language you choose for your project. To illustrate how easy xmldocx is, let's create the classic “Hello world!” message in all the programming languages the library admits.

xmldocx uses three types of XML files:

  • config.xml: This is a set-up file. You can only use one config file for document. It sets properties like the output formats as DOCX or PDF and the template you want to manage.
  • settings.xml: This XML controls document properties as borders, page size or background image.
  • content.xml: This file runs the contents you may want to insert in the document, as well as the template functions. You can add sequentially as many files like this as you need.

You can rename these files if you want, just remember that you will have to type their names when you call xmldocx.

For the sake of the example, let's use these three XML:

config.xml

content.xml

settings.xml

The paths to the files must be absolute, that is, they have to include the complete directory and its name from the root or system unit.

You finally just need to use the language of your choice.