News

Working with templates: inserting multiline text with PHPDocX

  • May 03, 2011

Time to play with templates! PhPDocX allows to modify an existing Word template.


You may create a working template by copying and pasting the following text in a docx document:
You may, for example, populate a table like this:
                  Weight (Kg)               Price (€)
Article 1     $WEIGHT1$              $PRICE1$
Article 2     $WEIGHT2$              $PRICE2$
Article 3     $WEIGHT3$              $PRICE3$
Total         $TOTALWEIGHT$      $TOTALPRICE$
Or you may simply change who sign the document.
Best regards,
$NAME$
And the code will be:


require_once '../../classes/CreateDocx.inc';

$docx = new CreateDocx();

$docx->addTemplate('../files/TemplateText.docx');

$docx->addTemplateVariable('WEIGHT1', '10');
$docx->addTemplateVariable('WEIGHT2', '20');
$docx->addTemplateVariable('WEIGHT3', '25');

$docx->addTemplateVariable('PRICE1', '5');
$docx->addTemplateVariable('PRICE2', '30');
$docx->addTemplateVariable('PRICE3', '7');

$docx->addTemplateVariable('TOTALWEIGHT', '55');

$docx->addTemplateVariable('TOTALPRICE', '42');

$text = array(
'David Hume',
'-----------',
'Bye'
);

$docx->addTemplateVariable('NAME', $text);

$docx->createDocx('template_text.docx');