PHPStamp - honest generation of DOCX documents from a template

This is another attempt to implement a stable, full-fledged template for office documents based on XML, using the standard DOMDocument and XSL tools for PHP.

The task was precisely to generate a reusable template that would not have to be edited manually or resorted to third-party programs to refine it.

You can find the library here: github.com/shadz3rg/PHPStamp

Principle of operation


Despite all the charms of visual editors, the output is always far from the most optimal document code, and MS Word is no exception. It’s so easy to replace the tags added to the document with XSL logic, even despite the relatively simple structure of the DOCX document.

The main problem is divided into fragments (they are also “run”, represented by nodes) paragraphs, the contents of the Tags is divided into several nodes and in this form they cannot be processed.

First, let's try to simplify the markup.

1. Remove the language properties of the passages. Which is logical, when replacing a Tag, a value can be inserted in any language, besides punctuation marks do not belong to the language, which causes additional problems when searching for Tags.
2. Remove the empty nodes of the passage properties, a purely technical point.
3. Combine the adjacent passages using the deepEqual comparison (which is still “Not implemented” in the DOMDocument) property node.

If everything went according to plan, the library can only add the appropriate XSL logic. An opportunity is provided - some extensibility with options. So, for example, table rows and lists are currently implemented.
The resulting template is cached in the file system, so its subsequent calls will be as fast as possible (as much as XSL is capable of).

Support for different formats. The plans were to add support for the ODT format, but due to the excellent DOCX support in LibreOffice, there is no special need. Maybe XLSX generation might be a good idea, it should work out pretty quickly compared to PHPExcel.

Dependencies and Installation


You can install the library through Composer, on the server you will need PHP 5.3 with the Zip, DOM and XSL modules.

{
    "minimum-stability": "dev",
    "require": {
        "shadz3rg/php-stamp": "dev-master"
    }
}


Usage example


For example, use the template:



debug = true;
	$documentPath = 'path/to/document.docx';
	$document = new WordDocument($documentPath);
	$values = array(
		'library' => 'PHPStamp 0.1',
		'simpleValue' => 'I am simple value',
		'nested' => array(
			'firstValue' => 'First child value',
			'secondValue' => 'Second child value'
		),
		'header' => 'test of a table row',
		'students' => array(
			array('id' => 1, 'name' => 'Student 1', 'mark' => '10'),
			array('id' => 2, 'name' => 'Student 2', 'mark' => '4'),
			array('id' => 3, 'name' => 'Student 3', 'mark' => '7')
		),
		'maxMark' => 10,
		'todo' => array(
			'TODO 1',
			'TODO 2',
			'TODO 3'
		)
	);
	$result = $templator->render($document, $values);
	$result->download();


At the output, we get a document with the data we need:


Also popular now: