Cookbook

Improve Javadocx performance

javadocx shows great performance when working with documents, but it is possible to boost its execution speed through some functionalities.

In this article we are going to review the different features for each license that help to improve javadocx performance.

Java 7

Since the javadocx version 5.1 release, all licenses support Java 7, along with Java 5.2 and its major versions. In opposition to older versions, Java 7 offers numerous performance improvements due to the inclusion of a new JavaNG engine.

Java 7 allows to generate documents more quickly and with no need of making any changes in javadocx's code.

DOCX loaded in memory

javadocx 6.5 has suppressed the process of creating a temporal copy in the file system of the base document or template. It only stores in the file system the final DOCX.

This improvement has been possible thanks to a new class that keeps in memory the files, while it lessens the resources the server uses when working with the javadocx API.

All packages from javadocx 6.5 on include this feature.

Templates loaded in memory (Premium license only)

When loading a template, javadocx opens and parses its content from the DOCX file, thus consuming server resources as it accesses the file system. With the purpose of improving performance when working with templates, it is possible to save the parsed structure of the DOCX and reuse it later on with the parseDocx method of the DOCXStructure class.

The code to parse a DOCX and save it in a variable is as follows:

Serializing templates (Premium licenses only)

Parsing a DOCX with the previous procedure allows not only to reuse it along a script but also to serialize it to run it anytime. E.g.: you can save it in memory or in a data base with the Java serialize function (http://php.net/manual/en/function.serialize.php) and recover its initial state with unserialize (http://php.net/manual/en/function.unserialize.php).

The following code serializes a template:

After saving this serialized object you can recover it anytime by unserializing it:

DOCX as a stream (Premium license only)

All javadocx licenses save the resulting DOCX in the file system, and after that it lets you download or open it. Premium license includes the possibility of generating DOCX, either from scratch or from templates, as a stream. In other words, javadocx obtains the content of the documents straight from its result in the browser or the CLI. With a Premium license you can also redirect its output to other target, like a data base. You can do all of that without the requirement of saving the document in the file system.

You can dinamically set up this stream mode in a script or force it for all documents in the configuration file config/phpdocxconfig.ini.

The following example shows how to enable the javadocx stream mode: