Compiled mode

Compiled mode


Compiled mode is a feature available since phpdocx 13 in Premium licenses.

This feature uses an executable file and JSON contents. It allows to obtain the existing styles and placeholders, replace text, list, table and checbox placeholders, and delete placeholders.

Instead of using PHP, which is an interpreted language, Compiled mode uses an executable, and that means better performance. Thanks to its flexibility, it is possible to apply to the same DOCX the phpdocx classes, along with the functions included in this Compiled mode.

Supported OS

Compiled mode includes executables for the following operating systems and architectures:

  • Linux 64-bit
  • Linux ARM64
  • Windows 64-bit
  • macOS 64-bit
Using the Compiled mode

Compiled mode can be used by calling the executable standalone using CLI or a PHP wrapper that calls the executable. phpdocx includes the CompiledMode class to call Compiled mode from PHP.

In order to work with the Compiled mode from CLI, it is enough to execute the available command in the compiled-mode directory for the corresponding OS. E.g., this shows how to replace placeholders of a template with the data.json information:

Or, in its short version:

You can invoke the Compiled mode with PHP instead of CLI, just using the CompiledMode class. For example:

It is also possible to call the executable directly with PHP functions like exec or system.

The "-h" parameter shows the complete help of the command:

The available actions are the following: default (replace and remove placeholders), get_styles and get_variables.

JSON structures

The default action replaces placeholders with a JSON content. This JSON can be generated from PHP or any other coding language, even as plain text.

The following examples illustrate the structure of the JSON that replaces the existing placeholders.

Remove placeholders:

Replace text placeholders:

Replace list placeholders:

Replace table placeholders:

Replace checkbox placeholders:

In the JSON content there is also an "options" feature, with an additional set of options:

  • content_type: document, comments, endnotes, footers, footnotes, headers. All as default.
  • max_replacements: limit the number of replacements.

JSON for replacing text and table placeholders:

JSON for replacing text placeholders on headers and footers only:

JSON for replacing just the first occurrence of a placeholder:

  • The placeholders to be replaced must be wrapped using ${ } in the DOCX template.
  • In order to call the executable in Windows from PHP it is advisable to indicate the absolute path.
  • The compiled-mode directory has the executables of the Compiled mode (bin folder) and use cases in CLI and PHP for every functionality (samples folder).