uccello/document-designer

Convert docx files templates into PDF.

v1.0.2 2019-10-23 14:14 UTC

This package is auto-updated.

Last update: 2020-12-18 17:16:02 UTC


README

Document Designer is a Laravel/Ucello Package allowing to generate documents from a template and filling it with some user data. The current version of PHPWord supports Microsoft Office Open XML (OOXML or OpenXML)

Document Designer is based on PHPWord. LibreOffce need to be installed on the server for PDF export.

Features

  • Template processing from .docx files
  • Export in .docx & .pdf fomat
  • Text variable replacement
  • Table row repetition
  • Recursive blocks repetition

Installation

Package

composer require uccello/document-designer

LibreOffice

You can refer to the oficial documentation from LibreOffice for the installation on your server OS.

Getting Started

You just need to specify a template file, a out file name and the dataset to be used to parse and populate the template:

DocumentIO::process($templateFile, $outFile, $data);

The $data should be an associative array containing all the variables and the corresponding data to be replaced.

Depending the extension (.docx or .pdf) given in the $outFile, the export format will be DOCX or PDF.

Variables

$data = [
    'variableName' => 'Content of the variable',
    'otherVariable' => 'Other content',
]

And in the template document, you need to declare the variables with the syntax : ${variableName}

Tables

Table keys in the associative array needs a t: prefix.

$data = [
    't:variableName' => [
        [
            'variableName' => 'dolor',
            'otherVariable' => 'elit',
        ],
        [
            'variableName' => 'amet',
            'otherVariable' => 'elit',
        ],
    ],
]

In the template document, you need to declare the variables with the syntax : ${variableName}

The first row of the all template document containing a variable with the the same name as the table key will be repeated and the content of the other variables will be replaced.

Images

Image keys in the associative array needs a i: prefix.

$data = [
    'i:imgVariable' => 'path/image.jpg',
]

And in the template document, you need to declare the variables with the syntax : ${imgVariable:[width]:[height]:[ratio]}

Blocks

Block keys in the associative array needs a b: prefix and with capital letters.

$data = [
    'b:BLOCK_NAME' => [
        [
            'variableName' => 'dolor',
            'otherVariable' => 'elit',
        ],
        [
            'variableName' => 'amet',
            'otherVariable' => 'elit',
        ],
    ]
]

In the template document, you need to declare the blocks start and end with flags :

${BLOCK_NAME}

Block content...

${/BLOCK_NAME}

The blocks behave recursively, witch means they can contain variables, tables and others block.

If a block contain a variable with a name in conflict with another variable in a parent block, the deeper block variables will be replaced in priority.

Exemple:

Php
use Uccello\DocumentDesigner\Support\DocumentIO;

$templateFile = "path/template.docx";
$outFile = "path/out.pdf";

$data = [
    'var1' => 'lorem',
    'var2' => 'ipsum',
    'img1' => 'path/image.jpg',
    't:tVar1' => [
        [
            'tVar1' => 'dolor',
            'tVar2' => 'sit',
        ],
        [
            'tVar1' => 'amet',
            'tVar2' => 'consectetur',
        ],
    ],
    'b:BLOCK' => [
        [
            'var1' => 'adipiscing',
            'var2' => 'elit',
        ],
        [
            'var1' => 'sed',
            'var2' => 'do',
        ],
        [
            'var1' => 'eiusmod',
            'var2' => 'tempor',
        ],
    ]
];

DocumentIO::process($templateFile, $outFile, $data);
Template.docx

This is a simple test template content with two variables ${var1} and ${var2}

With an image:

${img1:300:200}

With a table:

Table variable 1 Table variable 2
${tVar1} ${tVar2}

And a block:

${BLOCK}

This is a block content with two variables ${var1} and ${var2}

${/BLOCK}