Documentation

Template extends TemplateProcessor

Tags
deprecated
0.12.0

Use \PhpOffice\PhpWord\TemplateProcessor instead.

codeCoverageIgnore

Table of Contents

MAXIMUM_REPLACEMENTS_DEFAULT  = -1
$tempDocumentContentTypes  : string
Document content types (in XML format) of the temporary document.
$tempDocumentFilename  : string
$tempDocumentFooters  : array<string|int, string>
Content of footers (in XML format) of the temporary document
$tempDocumentHeaders  : array<string|int, string>
Content of headers (in XML format) of the temporary document
$tempDocumentMainPart  : string
Content of main document part (in XML format) of the temporary document
$tempDocumentNewImages  : array<string|int, string>
new inserted images list
$tempDocumentRelations  : array<string|int, string>
Document relations (in XML format) of the temporary document.
$tempDocumentSettingsPart  : string
Content of settings part (in XML format) of the temporary document
$zipClass  : mixed
ZipArchive object.
__construct()  : mixed
applyXslStyleSheet()  : mixed
Applies XSL style sheet to template's parts.
cloneBlock()  : string|null
Clone a block.
cloneRow()  : mixed
Clone a table row in a template document.
cloneRowAndSetValues()  : mixed
Clones a table row and populates it's values from a two-dimensional array in a template document.
deleteBlock()  : mixed
Delete a block of text.
getVariableCount()  : array<string|int, mixed>
Returns count of all variables in template.
getVariables()  : array<string|int, string>
Returns array of all variables in template.
replaceBlock()  : mixed
Replace a block.
replaceXmlBlock()  : TemplateProcessor
Replace an XML block surrounding a macro with a new block
save()  : string
Saves the result document.
saveAs()  : mixed
Saves the result document to the user defined file.
setChart()  : mixed
setComplexBlock()  : mixed
setComplexValue()  : mixed
setImageValue()  : mixed
setUpdateFields()  : mixed
Automatically Recalculate Fields on Open
setValue()  : mixed
setValues()  : mixed
Set values from a one-dimensional array of "variable => value"-pairs.
zip()  : ZipArchive
Expose zip class
ensureMacroCompleted()  : string
ensureUtf8Encoded()  : string
findContainingXmlBlockForMacro()  : bool|array<string|int, int>
Find start and end of XML block containing the given macro e.g. <w:p>...${macro}...</w:p>
findMacro()  : int
Find the position of (the start of) a macro
findRowEnd()  : int
Find the end position of the nearest table row after $offset.
findRowStart()  : int
Find the start position of the nearest table row before $offset.
findXmlBlockEnd()  : int
Find the nearest block end position after $offset
findXmlBlockStart()  : int
Find the start position of the nearest XML block start before $offset
fixBrokenMacros()  : string
Finds parts of broken macros and sticks them together.
getDocumentContentTypesName()  : string
getFooterName()  : string
Get the name of the footer file for $index.
getHeaderName()  : string
Get the name of the header file for $index.
getMainPartName()  : string
Usually, the name of main part document will be 'document.xml'. However, some .docx files (possibly those from Office 365, experienced also on documents from Word Online created from blank templates) have file 'document22.xml' in their zip archive instead of 'document.xml'. This method searches content types file to correctly determine the file name.
getNextRelationsIndex()  : mixed
getRelationsName()  : string
Get the name of the relations file for document part.
getSettingsPartName()  : string
The name of the file containing the Settings part
getSlice()  : string
Get a slice of a string.
getVariablesForPart()  : array<string|int, string>
Find all variables in $documentPartXML.
indexClonedVariables()  : string
Replaces variable names in cloned rows/blocks with indexed names
readPartWithRels()  : string
replaceClonedVariables()  : array<string|int, string>
Raplaces variables with values from array, array keys are the variable names
savePartWithRels()  : mixed
setValueForPart()  : string
Find and replace macros in the given XML section.
splitTextIntoTexts()  : string
Splits a w:r/w:t into a list of w:r where each ${macro} is in a separate w:r
textNeedsSplitting()  : bool
Returns true if string contains a macro that is not in it's own w:r
transformSingleXml()  : string
transformXml()  : mixed
addImageToRelations()  : mixed
chooseImageDimension()  : mixed
fixImageWidthHeightRatio()  : mixed
getImageArgs()  : mixed
prepareImageAttrs()  : mixed

Constants

Properties

$tempDocumentContentTypes

Document content types (in XML format) of the temporary document.

protected string $tempDocumentContentTypes = ''

$tempDocumentFooters

Content of footers (in XML format) of the temporary document

protected array<string|int, string> $tempDocumentFooters = array()

$tempDocumentHeaders

Content of headers (in XML format) of the temporary document

protected array<string|int, string> $tempDocumentHeaders = array()

$tempDocumentMainPart

Content of main document part (in XML format) of the temporary document

protected string $tempDocumentMainPart

$tempDocumentNewImages

new inserted images list

protected array<string|int, string> $tempDocumentNewImages = array()

$tempDocumentRelations

Document relations (in XML format) of the temporary document.

protected array<string|int, string> $tempDocumentRelations = array()

$tempDocumentSettingsPart

Content of settings part (in XML format) of the temporary document

protected string $tempDocumentSettingsPart

Methods

__construct()

public __construct(string $documentTemplate) : mixed
Parameters
$documentTemplate : string

The fully qualified template filename

Tags
since
0.12.0

Throws CreateTemporaryFileException and CopyFileException instead of Exception

throws
CreateTemporaryFileException
throws
CopyFileException
Return values
mixed

applyXslStyleSheet()

Applies XSL style sheet to template's parts.

public applyXslStyleSheet(DOMDocument $xslDomDocument[, array<string|int, mixed> $xslOptions = array() ][, string $xslOptionsUri = '' ]) : mixed

Note: since the method doesn't make any guess on logic of the provided XSL style sheet, make sure that output is correctly escaped. Otherwise you may get broken document.

Parameters
$xslDomDocument : DOMDocument
$xslOptions : array<string|int, mixed> = array()
$xslOptionsUri : string = ''
Tags
throws
Exception
Return values
mixed

cloneBlock()

Clone a block.

public cloneBlock(string $blockname[, int $clones = 1 ][, bool $replace = true ][, bool $indexVariables = false ][, array<string|int, mixed> $variableReplacements = null ]) : string|null
Parameters
$blockname : string
$clones : int = 1

How many time the block should be cloned

$replace : bool = true
$indexVariables : bool = false

If true, any variables inside the block will be indexed (postfixed with #1, #2, ...)

$variableReplacements : array<string|int, mixed> = null

Array containing replacements for macros found inside the block to clone

Return values
string|null

cloneRow()

Clone a table row in a template document.

public cloneRow(string $search, int $numberOfClones) : mixed
Parameters
$search : string
$numberOfClones : int
Tags
throws
Exception
Return values
mixed

cloneRowAndSetValues()

Clones a table row and populates it's values from a two-dimensional array in a template document.

public cloneRowAndSetValues(string $search, array<string|int, mixed> $values) : mixed
Parameters
$search : string
$values : array<string|int, mixed>
Return values
mixed

deleteBlock()

Delete a block of text.

public deleteBlock(string $blockname) : mixed
Parameters
$blockname : string
Return values
mixed

getVariableCount()

Returns count of all variables in template.

public getVariableCount() : array<string|int, mixed>
Return values
array<string|int, mixed>

getVariables()

Returns array of all variables in template.

public getVariables() : array<string|int, string>
Return values
array<string|int, string>

replaceBlock()

Replace a block.

public replaceBlock(string $blockname, string $replacement) : mixed
Parameters
$blockname : string
$replacement : string
Return values
mixed

replaceXmlBlock()

Replace an XML block surrounding a macro with a new block

public replaceXmlBlock(string $macro, string $block[, string $blockType = 'w:p' ]) : TemplateProcessor
Parameters
$macro : string

Name of macro

$block : string

New block content

$blockType : string = 'w:p'

XML tag type of block

Return values
TemplateProcessor

Fluent interface

saveAs()

Saves the result document to the user defined file.

public saveAs(string $fileName) : mixed
Parameters
$fileName : string
Tags
since
0.8.0
Return values
mixed

setImageValue()

public setImageValue(mixed $search, mixed $replace[, int $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT ]) : mixed
Parameters
$search : mixed
$replace : mixed

Path to image, or array("path" => xx, "width" => yy, "height" => zz)

$limit : int = self::MAXIMUM_REPLACEMENTS_DEFAULT
Return values
mixed

setUpdateFields()

Automatically Recalculate Fields on Open

public setUpdateFields([bool $update = true ]) : mixed
Parameters
$update : bool = true
Return values
mixed

setValue()

public setValue(mixed $search, mixed $replace[, int $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT ]) : mixed
Parameters
$search : mixed
$replace : mixed
$limit : int = self::MAXIMUM_REPLACEMENTS_DEFAULT
Return values
mixed

setValues()

Set values from a one-dimensional array of "variable => value"-pairs.

public setValues(array<string|int, mixed> $values) : mixed
Parameters
$values : array<string|int, mixed>
Return values
mixed

zip()

Expose zip class

public zip() : ZipArchive

To replace an image: $templateProcessor->zip()->AddFromString("word/media/image1.jpg", file_get_contents($file));
To read a file: $templateProcessor->zip()->getFromName("word/media/image1.jpg");

Return values
ZipArchive

ensureMacroCompleted()

protected static ensureMacroCompleted(string $macro) : string
Parameters
$macro : string
Return values
string

ensureUtf8Encoded()

protected static ensureUtf8Encoded(string $subject) : string
Parameters
$subject : string
Return values
string

findContainingXmlBlockForMacro()

Find start and end of XML block containing the given macro e.g. <w:p>...${macro}...</w:p>

protected findContainingXmlBlockForMacro(string $macro[, string $blockType = 'w:p' ]) : bool|array<string|int, int>

Note that only the first instance of the macro will be found

Parameters
$macro : string

Name of macro

$blockType : string = 'w:p'

XML tag for block

Return values
bool|array<string|int, int>

FALSE if not found, otherwise array with start and end

findMacro()

Find the position of (the start of) a macro

protected findMacro(string $search, int $offset) : int

Returns -1 if not found, otherwise position of opening $

Note that only the first instance of the macro will be found

Parameters
$search : string

Macro name

$offset : int

Offset from which to start searching

Return values
int

-1 if macro not found

findRowEnd()

Find the end position of the nearest table row after $offset.

protected findRowEnd(int $offset) : int
Parameters
$offset : int
Return values
int

findRowStart()

Find the start position of the nearest table row before $offset.

protected findRowStart(int $offset) : int
Parameters
$offset : int
Tags
throws
Exception
Return values
int

findXmlBlockEnd()

Find the nearest block end position after $offset

protected findXmlBlockEnd(int $offset, string $blockType) : int
Parameters
$offset : int

Search position

$blockType : string

XML Block tag

Return values
int

-1 if block end not found

findXmlBlockStart()

Find the start position of the nearest XML block start before $offset

protected findXmlBlockStart(int $offset, string $blockType) : int
Parameters
$offset : int

Search position

$blockType : string

XML Block tag

Return values
int

-1 if block start not found

fixBrokenMacros()

Finds parts of broken macros and sticks them together.

protected fixBrokenMacros(string $documentPart) : string

Macros, while being edited, could be implicitly broken by some of the word processors.

Parameters
$documentPart : string

The document part in XML representation

Return values
string

getDocumentContentTypesName()

protected getDocumentContentTypesName() : string
Return values
string

getFooterName()

Get the name of the footer file for $index.

protected getFooterName(int $index) : string
Parameters
$index : int
Return values
string

getHeaderName()

Get the name of the header file for $index.

protected getHeaderName(int $index) : string
Parameters
$index : int
Return values
string

getMainPartName()

Usually, the name of main part document will be 'document.xml'. However, some .docx files (possibly those from Office 365, experienced also on documents from Word Online created from blank templates) have file 'document22.xml' in their zip archive instead of 'document.xml'. This method searches content types file to correctly determine the file name.

protected getMainPartName() : string
Return values
string

getNextRelationsIndex()

protected getNextRelationsIndex(mixed $documentPartName) : mixed
Parameters
$documentPartName : mixed
Return values
mixed

getRelationsName()

Get the name of the relations file for document part.

protected getRelationsName(string $documentPartName) : string
Parameters
$documentPartName : string
Return values
string

getSettingsPartName()

The name of the file containing the Settings part

protected getSettingsPartName() : string
Return values
string

getSlice()

Get a slice of a string.

protected getSlice(int $startPosition, int $endPosition) : string
Parameters
$startPosition : int
$endPosition : int
Return values
string

getVariablesForPart()

Find all variables in $documentPartXML.

protected getVariablesForPart(string $documentPartXML) : array<string|int, string>
Parameters
$documentPartXML : string
Return values
array<string|int, string>

indexClonedVariables()

Replaces variable names in cloned rows/blocks with indexed names

protected indexClonedVariables(int $count, string $xmlBlock) : string
Parameters
$count : int
$xmlBlock : string
Return values
string

readPartWithRels()

protected readPartWithRels(string $fileName) : string
Parameters
$fileName : string
Return values
string

replaceClonedVariables()

Raplaces variables with values from array, array keys are the variable names

protected replaceClonedVariables(array<string|int, mixed> $variableReplacements, string $xmlBlock) : array<string|int, string>
Parameters
$variableReplacements : array<string|int, mixed>
$xmlBlock : string
Return values
array<string|int, string>

savePartWithRels()

protected savePartWithRels(string $fileName, string $xml) : mixed
Parameters
$fileName : string
$xml : string
Return values
mixed

setValueForPart()

Find and replace macros in the given XML section.

protected setValueForPart(mixed $search, mixed $replace, string $documentPartXML, int $limit) : string
Parameters
$search : mixed
$replace : mixed
$documentPartXML : string
$limit : int
Return values
string

splitTextIntoTexts()

Splits a w:r/w:t into a list of w:r where each ${macro} is in a separate w:r

protected splitTextIntoTexts(string $text) : string
Parameters
$text : string
Return values
string

textNeedsSplitting()

Returns true if string contains a macro that is not in it's own w:r

protected textNeedsSplitting(string $text) : bool
Parameters
$text : string
Return values
bool

transformSingleXml()

protected transformSingleXml(string $xml, XSLTProcessor $xsltProcessor) : string
Parameters
$xml : string
$xsltProcessor : XSLTProcessor
Tags
throws
Exception
Return values
string

transformXml()

protected transformXml(mixed $xml, XSLTProcessor $xsltProcessor) : mixed
Parameters
$xml : mixed
$xsltProcessor : XSLTProcessor
Return values
mixed

addImageToRelations()

private addImageToRelations(mixed $partFileName, mixed $rid, mixed $imgPath, mixed $imageMimeType) : mixed
Parameters
$partFileName : mixed
$rid : mixed
$imgPath : mixed
$imageMimeType : mixed
Return values
mixed

chooseImageDimension()

private chooseImageDimension(mixed $baseValue, mixed $inlineValue, mixed $defaultValue) : mixed
Parameters
$baseValue : mixed
$inlineValue : mixed
$defaultValue : mixed
Return values
mixed

fixImageWidthHeightRatio()

private fixImageWidthHeightRatio(mixed &$width, mixed &$height, mixed $actualWidth, mixed $actualHeight) : mixed
Parameters
$width : mixed
$height : mixed
$actualWidth : mixed
$actualHeight : mixed
Return values
mixed

getImageArgs()

private getImageArgs(mixed $varNameWithArgs) : mixed
Parameters
$varNameWithArgs : mixed
Return values
mixed

prepareImageAttrs()

private prepareImageAttrs(mixed $replaceImage, mixed $varInlineArgs) : mixed
Parameters
$replaceImage : mixed
$varInlineArgs : mixed
Return values
mixed

Search results