Class kartik\wordreport\TemplateReport

Inheritancekartik\wordreport\TemplateReport » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Uses Traitskartik\wordreport\utils\LogTrait
Available since version1.0

TemplateReport class is a library for generating reports in Word/PDF from Microsoft Word Templates. It uses the PhpOffice\PhpWord library to parse the word templates and generate the report.

For example,

// initialize report
$report = new TemplateReport([
   'format' => TemplateReport::FORMAT_BOTH,
   'inputFile' => 'Invoice_Template_01.docx',
   'outputFile' => 'Invoice_Report_' . date('Y-m-d'),
   'values' => ['invoice_no' => 2001, 'invoice_date' => '2020-02-21'],
   'images' => ['company_logo' => '@webroot/images/company.jpg', 'customer_logo' => '@webroot/images/company.jpg'],
   'rows' => [
     'item' => [
         ['item' => 1, 'name' => 'Potato', 'price' => '$10.00'],
         ['item' => 2, 'name' => 'Tomato', 'price' => '$20.00'],
     ]
   ],
   'blocks' => [
     'customer_block' => [
         ['customer_name' => 'John', 'customer_address' => 'Address for John'],
         ['customer_name' => 'Bill', 'customer_address' => 'Address for Bill'],
     ],
   ]
]);
// Generate the report
$report->generate();

See also https://github.com/PHPOffice/PHPWord.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$behaviors yii\base\Behavior[] List of behaviors attached to this component. yii\base\Component
$blocks array Repeating block of values - similar to tabular values but with enhanced control. kartik\wordreport\TemplateReport
$charts array Key value pairs of template variable chart names and their values which will be replaced. kartik\wordreport\TemplateReport
$complexBlocks array Key value pairs of template variable block names and their values which will be replaced. kartik\wordreport\TemplateReport
$complexValues array Key value pairs of template variable names and their values which will be replaced. kartik\wordreport\TemplateReport
$format string The output format whether 'docx' or 'pdf' or 'both' kartik\wordreport\TemplateReport
$images array Key value pairs of template image variable names and their values which will be replaced. kartik\wordreport\TemplateReport
$inputFile string The name of the template file with the extension (docx, odt, rtf) kartik\wordreport\TemplateReport
$inputPath string The path where the Microsoft Word template files exist kartik\wordreport\TemplateReport
$outputFile string The name of the output file without the PDF or DOCX extension kartik\wordreport\TemplateReport
$outputPath string The path where the Microsoft Word output files will be saved kartik\wordreport\TemplateReport
$pdfConverter kartik\wordreport\utils\Converter The pdf converter class to use. kartik\wordreport\TemplateReport
$rows array Key value pairs of template variable names in a table in the document and their values which will be replaced. kartik\wordreport\TemplateReport
$template \PhpOffice\PhpWord\TemplateProcessor the template processor object kartik\wordreport\TemplateReport
$values array Key value pairs of template variable names and their values which will be replaced. kartik\wordreport\TemplateReport
$xslStyleSheet string Path to any custom XSL style sheet to apply if applicable kartik\wordreport\TemplateReport

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
generate() Generates the word report output kartik\wordreport\TemplateReport
generatePdf() Generate a PDF file from input document file using libreoffice host command. kartik\wordreport\TemplateReport
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getTemplate() Get the template processor object kartik\wordreport\TemplateReport
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the object. yii\base\BaseObject
log() Log / report a message. kartik\wordreport\utils\LogTrait
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
process() Processes output by executing appropriate template method for the selected property. kartik\wordreport\TemplateReport
setTemplate() Set the template processor object kartik\wordreport\TemplateReport
trigger() Triggers an event. yii\base\Component

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
FORMAT_BOTH 'both' kartik\wordreport\TemplateReport
FORMAT_PDF 'pdf' kartik\wordreport\TemplateReport
FORMAT_WORD 'docx' kartik\wordreport\TemplateReport

Property Details

$blocks public property

Repeating block of values - similar to tabular values but with enhanced control.

For example,

// template setting
// ${customer_block}
// Customer: ${customer_name}
// Address: ${customer_address}
// ${/customer_block}
// ${block_2}
// Some text inside the block
// @{/block_2}
// ${block_3}
// @{/block_3}

'blocks' => [
     // loops array and replaces variables inside the block
     'customer_block' => [
         [
              'customer_name' => 'John',
              'customer_address' => 'Address for John',
         ],
         [
              'customer_name' => 'Bill',
              'customer_address' => 'Address for Bill',
         ],
     ],

     // replaces the block with the block text mentioned
     'block_2' => 'Replace this text inside the block',

     // deletes the block if the block variable is set to false
     'block_3' => false
]
public array $blocks null
$charts public property

Key value pairs of template variable chart names and their values which will be replaced. This must be a two dimensional array of chart variable names as keys and the chart object as the value.

For example,

// template setting
// CHART 1:
// ${chart1}
// CHART 2:
// ${chart2}

use PhpOffice\PhpWord\Element\Chart;
$categories = array('A', 'B', 'C', 'D', 'E');
$series1 = array(1, 3, 2, 5, 4);
$series2 = array(3, 1, 7, 2, 6);
$series3 = array(8, 3, 2, 5, 4);

$chart1 = new Chart('pie', $categories, $series1);
$chart2 = new Chart('bar', $categories, $series1);
$chart2->addSeries($series2);
$chart3->addSeries($series2);

'charts' => [
     'chart1' => $chart1,
     'chart2' => $chart2,
]
public array $charts null
$complexBlocks public property

Key value pairs of template variable block names and their values which will be replaced.

This must be a two dimensional array of block variable names as keys and the complex object as the value.

For example,

// template setting
// ${table}
// ${/table}

$table = new \PhpOffice\PhpWord\Element\Table(['borderSize' => 12, 'borderColor' => 'green', 'width' => 6000,
    'unit' => TblWidth::TWIP]);
$table->addRow();
$table->addCell(150)->addText('Cell A1');
$table->addCell(150)->addText('Cell A2');
$table->addRow();
$table->addCell(150)->addText('Cell B1');
$table->addCell(150)->addText('Cell B2');

'complexBlocks' => [
     'table' => $table,
]
public array $complexBlocks null
$complexValues public property

Key value pairs of template variable names and their values which will be replaced.

This must be a two dimensional array of variable names as keys and the complex object as the value.

For example,

// template setting
// Content: ${inline}

 $object = new \PhpOffice\PhpWord\Element\TextRun();
 $object->addText('by a red italic text', ['italic' => true, 'color' => 'red']);
'complexValues' => [
     'inline' => $object,
]
public array $complexValues null
$format public property

The output format whether 'docx' or 'pdf' or 'both'

public string $format self::FORMAT_WORD
$images public property

Key value pairs of template image variable names and their values which will be replaced. The value must be full path to the image on the server.

// template setting
// ${company_logo}
// Company Name

'images' => [
     'company_logo' => '@webroot/uploads/company/logo.jpg'
]
public array $images null
$inputFile public property

The name of the template file with the extension (docx, odt, rtf)

public string $inputFile null
$inputPath public property

The path where the Microsoft Word template files exist

public string $inputPath '@webroot/templates'
$outputFile public property

The name of the output file without the PDF or DOCX extension

public string $outputFile null
$outputPath public property

The path where the Microsoft Word output files will be saved

public string $outputPath '@webroot/reports'
$pdfConverter public property

The pdf converter class to use. Defaults to kartik\wordreport\pdf\LibreOffice if not provided.

$rows public property

Key value pairs of template variable names in a table in the document and their values which will be replaced.

Each value must be an array of $values which will be repeated for number of items in the array. The key should typically match the first table column's variable name.

For example,

// template setting
// ----------+-------------+-----------
// ${item}   |    {$name}  | ${price}
// ----------+-------------+-----------
'rows' => [
     'item' => [
         [
              'item' => 1,
              'name' => 'Potato',
              'price' => '$10.00',
         ],
         [
              'item' => 2,
              'name' => 'Tomato',
              'price' => '$20.00',
         ],
     ]
]
public array $rows null
$template public property

the template processor object

public \PhpOffice\PhpWord\TemplateProcessor $template null
$values public property

Key value pairs of template variable names and their values which will be replaced. This must be a two dimensional array (which means a variable value must be string/int and cannot be an array).

// template setting
// Invoice No.: ${invoice_number}
// Invoice Dt: ${invoice_date}

'values' => [
     'invoice_number' => '1000',
     'invoice_date' => '22-Feb-2021',
]
public array $values null
$xslStyleSheet public property

Path to any custom XSL style sheet to apply if applicable

public string $xslStyleSheet null

Method Details

generate() public method

Generates the word report output

public void generate ( )
throws \PhpOffice\PhpWord\Exception\CopyFileException|\PhpOffice\PhpWord\Exception\CreateTemporaryFileException|\yii\base\InvalidConfigException|\PhpOffice\PhpWord\Exception\Exception|\kartik\wordreport\utils\ConverterException
generatePdf() public method

Generate a PDF file from input document file using libreoffice host command.

public void generatePdf ( $input, $output )
$input string

The input document file

$output string

The output document file

throws kartik\wordreport\utils\ConverterException
getTemplate() public method

Get the template processor object

public \PhpOffice\PhpWord\TemplateProcessor getTemplate ( )
process() public method

Processes output by executing appropriate template method for the selected property.

public void process ( $prop, $method null )
$prop string

The kartik\wordreport\TemplateReport property name

$method string

The template processor method to run

setTemplate() public method

Set the template processor object

public void setTemplate ( $template null )
$template \PhpOffice\PhpWord\TemplateProcessor
throws \PhpOffice\PhpWord\Exception\CopyFileException|\PhpOffice\PhpWord\Exception\CreateTemporaryFileException|\yii\base\InvalidConfigException|\PhpOffice\PhpWord\Exception\Exception