`__
29 |
--------------------------------------------------------------------------------
/docs/templates-processing.rst:
--------------------------------------------------------------------------------
1 | .. _templates-processing:
2 |
3 | Templates processing
4 | ====================
5 |
6 | You can create an OOXML document template with included search-patterns (macros) which can be replaced by any value you wish. Only single-line values can be replaced.
7 |
8 | To deal with a template file, use ``new TemplateProcessor`` statement. After TemplateProcessor instance creation the document template is copied into the temporary directory. Then you can use ``TemplateProcessor::setValue`` method to change the value of a search pattern. The search-pattern model is: ``${search-pattern}``.
9 |
10 | Example:
11 |
12 | .. code-block:: php
13 |
14 | $templateProcessor = new TemplateProcessor('Template.docx');
15 | $templateProcessor->setValue('Name', 'John Doe');
16 | $templateProcessor->setValue(array('City', 'Street'), array('Detroit', '12th Street'));
17 |
18 | It is not possible to directly add new OOXML elements to the template file being processed, but it is possible to transform headers, main document part, and footers of the template using XSLT (see ``TemplateProcessor::applyXslStyleSheet``).
19 |
20 | See ``Sample_07_TemplateCloneRow.php`` for example on how to create
21 | multirow from a single row in a template by using ``TemplateProcessor::cloneRow``.
22 |
23 | See ``Sample_23_TemplateBlock.php`` for example on how to clone a block
24 | of text using ``TemplateProcessor::cloneBlock`` and delete a block of text using
25 | ``TemplateProcessor::deleteBlock``.
26 |
--------------------------------------------------------------------------------
/phpmd.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
11 |
12 |
13 | ./tests/PhpWord
14 |
15 |
16 |
17 |
18 | ./src
19 |
20 | ./src/PhpWord/Shared/PCLZip
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/phpword.ini.dist:
--------------------------------------------------------------------------------
1 | ; Default config file for PHPWord
2 | ; Copy this file into phpword.ini and use Settings::loadConfig to load
3 |
4 | [General]
5 |
6 | compatibility = true
7 | zipClass = ZipArchive
8 | pdfRendererName = DomPDF
9 | pdfRendererPath =
10 | ; tempDir = "C:\PhpWordTemp"
11 | outputEscapingEnabled = false
12 |
13 | [Font]
14 |
15 | defaultFontName = Arial
16 | defaultFontSize = 10
17 |
--------------------------------------------------------------------------------
/samples/Sample_02_TabStops.php:
--------------------------------------------------------------------------------
1 | addParagraphStyle(
11 | $multipleTabsStyleName,
12 | array(
13 | 'tabs' => array(
14 | new \PhpOffice\PhpWord\Style\Tab('left', 1550),
15 | new \PhpOffice\PhpWord\Style\Tab('center', 3200),
16 | new \PhpOffice\PhpWord\Style\Tab('right', 5300),
17 | )
18 | )
19 | );
20 |
21 | $rightTabStyleName = 'rightTab';
22 | $phpWord->addParagraphStyle($rightTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('right', 9090))));
23 |
24 | $leftTabStyleName = 'centerTab';
25 | $phpWord->addParagraphStyle($leftTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680))));
26 |
27 | // New portrait section
28 | $section = $phpWord->addSection();
29 |
30 | // Add listitem elements
31 | $section->addText("Multiple Tabs:\tOne\tTwo\tThree", null, $multipleTabsStyleName);
32 | $section->addText("Left Aligned\tRight Aligned", null, $rightTabStyleName);
33 | $section->addText("\tCenter Aligned", null, $leftTabStyleName);
34 |
35 | // Save file
36 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
37 | if (!CLI) {
38 | include_once 'Sample_Footer.php';
39 | }
40 |
--------------------------------------------------------------------------------
/samples/Sample_03_Sections.php:
--------------------------------------------------------------------------------
1 | addSection(array('borderColor' => '00FF00', 'borderSize' => 12));
10 | $section->addText('I am placed on a default section.');
11 |
12 | // New landscape section
13 | $section = $phpWord->addSection(array('orientation' => 'landscape'));
14 | $section->addText('I am placed on a landscape section. Every page starting from this section will be landscape style.');
15 | $section->addPageBreak();
16 | $section->addPageBreak();
17 |
18 | // New portrait section
19 | $section = $phpWord->addSection(
20 | array('paperSize' => 'Folio', 'marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600)
21 | );
22 | $section->addText('This section uses other margins with folio papersize.');
23 |
24 | // New portrait section with Header & Footer
25 | $section = $phpWord->addSection(
26 | array(
27 | 'marginLeft' => 200,
28 | 'marginRight' => 200,
29 | 'marginTop' => 200,
30 | 'marginBottom' => 200,
31 | 'headerHeight' => 50,
32 | 'footerHeight' => 50,
33 | )
34 | );
35 | $section->addText('This section and we play with header/footer height.');
36 | $section->addHeader()->addText('Header');
37 | $section->addFooter()->addText('Footer');
38 |
39 | // Save file
40 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
41 | if (!CLI) {
42 | include_once 'Sample_Footer.php';
43 | }
44 |
--------------------------------------------------------------------------------
/samples/Sample_05_Multicolumn.php:
--------------------------------------------------------------------------------
1 | addSection();
14 | $section->addText("Normal paragraph. {$filler}");
15 |
16 | // Two columns
17 | $section = $phpWord->addSection(
18 | array(
19 | 'colsNum' => 2,
20 | 'colsSpace' => 1440,
21 | 'breakType' => 'continuous',
22 | )
23 | );
24 | $section->addText("Two columns, one inch (1440 twips) spacing. {$filler}");
25 |
26 | // Normal
27 | $section = $phpWord->addSection(array('breakType' => 'continuous'));
28 | $section->addText("Normal paragraph again. {$filler}");
29 |
30 | // Three columns
31 | $section = $phpWord->addSection(
32 | array(
33 | 'colsNum' => 3,
34 | 'colsSpace' => 720,
35 | 'breakType' => 'continuous',
36 | )
37 | );
38 | $section->addText("Three columns, half inch (720 twips) spacing. {$filler}");
39 |
40 | // Normal
41 | $section = $phpWord->addSection(array('breakType' => 'continuous'));
42 | $section->addText("Normal paragraph again. {$filler}");
43 |
44 | // Save file
45 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
46 | if (!CLI) {
47 | include_once 'Sample_Footer.php';
48 | }
49 |
--------------------------------------------------------------------------------
/samples/Sample_10_EastAsianFontStyle.php:
--------------------------------------------------------------------------------
1 | addSection();
8 | $header = array('size' => 16, 'bold' => true);
9 | //1.Use EastAisa FontStyle
10 | $section->addText('中文楷体样式测试', array('name' => '楷体', 'size' => 16, 'color' => '1B2232'));
11 |
12 | // Save file
13 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
14 | if (!CLI) {
15 | include_once 'Sample_Footer.php';
16 | }
17 |
--------------------------------------------------------------------------------
/samples/Sample_11_ReadWord2007.php:
--------------------------------------------------------------------------------
1 | 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
12 | foreach ($writers as $writer => $extension) {
13 | echo date('H:i:s'), " Write to {$writer} format", EOL;
14 | $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, $writer);
15 | $xmlWriter->save("{$name}.{$extension}");
16 | rename("{$name}.{$extension}", "results/{$name}.{$extension}");
17 | }
18 |
19 | include_once 'Sample_Footer.php';
20 |
--------------------------------------------------------------------------------
/samples/Sample_15_Link.php:
--------------------------------------------------------------------------------
1 | addLinkStyle($linkFontStyleName, array('bold' => true, 'color' => '808000'));
11 |
12 | // New section
13 | $section = $phpWord->addSection();
14 |
15 | // Add hyperlink elements
16 | $section->addLink(
17 | 'https://github.com/PHPOffice/PHPWord',
18 | 'PHPWord on GitHub',
19 | array('color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE)
20 | );
21 | $section->addTextBreak(2);
22 | $section->addLink('http://www.bing.com', null, $linkFontStyleName);
23 | $section->addLink('http://www.yahoo.com', null, $linkFontStyleName);
24 |
25 | // Save file
26 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
27 | if (!CLI) {
28 | include_once 'Sample_Footer.php';
29 | }
30 |
--------------------------------------------------------------------------------
/samples/Sample_16_Object.php:
--------------------------------------------------------------------------------
1 | addSection();
10 | $section->addText('You can open this OLE object by double clicking on the icon:');
11 | $section->addTextBreak(2);
12 | $section->addObject('resources/_sheet.xls');
13 |
14 | // Save file
15 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
16 | if (!CLI) {
17 | include_once 'Sample_Footer.php';
18 | }
19 |
--------------------------------------------------------------------------------
/samples/Sample_18_Watermark.php:
--------------------------------------------------------------------------------
1 | addSection();
10 | $header = $section->addHeader();
11 | $header->addWatermark('resources/_earth.jpg', array('marginTop' => 200, 'marginLeft' => 55));
12 | $section->addText('The header reference to the current section includes a watermark image.');
13 |
14 | // Save file
15 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
16 | if (!CLI) {
17 | include_once 'Sample_Footer.php';
18 | }
19 |
--------------------------------------------------------------------------------
/samples/Sample_19_TextBreak.php:
--------------------------------------------------------------------------------
1 | 24);
10 |
11 | $paragraphStyle24 = array('spacing' => 240, 'size' => 24);
12 |
13 | $fontStyleName = 'fontStyle';
14 | $phpWord->addFontStyle($fontStyleName, array('size' => 9));
15 |
16 | $paragraphStyleName = 'paragraphStyle';
17 | $phpWord->addParagraphStyle($paragraphStyleName, array('spacing' => 480));
18 |
19 | // New section
20 | $section = $phpWord->addSection();
21 |
22 | $section->addText('Text break with no style:');
23 | $section->addTextBreak();
24 | $section->addText('Text break with defined font style:');
25 | $section->addTextBreak(1, $fontStyleName);
26 | $section->addText('Text break with defined paragraph style:');
27 | $section->addTextBreak(1, null, $paragraphStyleName);
28 | $section->addText('Text break with inline font style:');
29 | $section->addTextBreak(1, $fontStyle24);
30 | $section->addText('Text break with inline paragraph style:');
31 | $section->addTextBreak(1, null, $paragraphStyle24);
32 | $section->addText('Done.');
33 |
34 | // Save file
35 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
36 | if (!CLI) {
37 | include_once 'Sample_Footer.php';
38 | }
39 |
--------------------------------------------------------------------------------
/samples/Sample_20_BGColor.php:
--------------------------------------------------------------------------------
1 | addSection();
10 |
11 | $section->addText(
12 | 'This is some text highlighted using fgColor (limited to 15 colors)',
13 | array('fgColor' => \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW)
14 | );
15 | $section->addText('This one uses bgColor and is using hex value (0xfbbb10)', array('bgColor' => 'fbbb10'));
16 | $section->addText('Compatible with font colors', array('color' => '0000ff', 'bgColor' => 'fbbb10'));
17 |
18 | // Save file
19 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
20 | if (!CLI) {
21 | include_once 'Sample_Footer.php';
22 | }
23 |
--------------------------------------------------------------------------------
/samples/Sample_22_CheckBox.php:
--------------------------------------------------------------------------------
1 | addSection();
10 |
11 | $section->addText('Check box in section');
12 | $section->addCheckBox('chkBox1', 'Checkbox 1');
13 | $section->addText('Check box in table cell');
14 | $table = $section->addTable();
15 | $table->addRow();
16 | $cell = $table->addCell();
17 | $cell->addCheckBox('chkBox2', 'Checkbox 2');
18 |
19 | // Save file
20 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
21 | if (!CLI) {
22 | include_once 'Sample_Footer.php';
23 | }
24 |
--------------------------------------------------------------------------------
/samples/Sample_23_TemplateBlock.php:
--------------------------------------------------------------------------------
1 | cloneBlock('CLONEME', 3);
10 |
11 | // Everything between ${tag} and ${/tag}, will be deleted/erased.
12 | $templateProcessor->deleteBlock('DELETEME');
13 |
14 | echo date('H:i:s'), ' Saving the result document...', EOL;
15 | $templateProcessor->saveAs('results/Sample_23_TemplateBlock.docx');
16 |
17 | echo getEndingNotes(array('Word2007' => 'docx'));
18 | if (!CLI) {
19 | include_once 'Sample_Footer.php';
20 | }
21 |
--------------------------------------------------------------------------------
/samples/Sample_24_ReadODText.php:
--------------------------------------------------------------------------------
1 | addSection();
10 |
11 | // In section
12 | $textbox = $section->addTextBox(
13 | array(
14 | 'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER,
15 | 'width' => 400,
16 | 'height' => 150,
17 | 'borderSize' => 1,
18 | 'borderColor' => '#FF0000',
19 | )
20 | );
21 | $textbox->addText('Text box content in section.');
22 | $textbox->addText('Another line.');
23 | $cell = $textbox->addTable()->addRow()->addCell();
24 | $cell->addText('Table inside textbox');
25 |
26 | // Inside table
27 | $section->addTextBreak(2);
28 | $cell = $section->addTable()->addRow()->addCell(300);
29 | $textbox = $cell->addTextBox(array('borderSize' => 1, 'borderColor' => '#0000FF', 'innerMargin' => 100));
30 | $textbox->addText('Textbox inside table');
31 |
32 | // Inside header with textrun
33 | $header = $section->addHeader();
34 | $textbox = $header->addTextBox(array('width' => 600, 'borderSize' => 1, 'borderColor' => '#00FF00'));
35 | $textrun = $textbox->addTextRun();
36 | $textrun->addText('TextBox in header. TextBox can contain a TextRun ');
37 | $textrun->addText('with bold text', array('bold' => true));
38 | $textrun->addText(', ');
39 | $textrun->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
40 | $textrun->addText(', and image ');
41 | $textrun->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18));
42 | $textrun->addText('.');
43 |
44 | // Save file
45 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
46 | if (!CLI) {
47 | include_once 'Sample_Footer.php';
48 | }
49 |
--------------------------------------------------------------------------------
/samples/Sample_26_Html.php:
--------------------------------------------------------------------------------
1 | addSection();
9 | $html = 'Adding element via HTML
';
10 | $html .= 'Some well formed HTML snippet needs to be used
';
11 | $html .= 'With for example some1 inline formatting1
';
12 | $html .= 'Unordered (bulleted) list:
';
13 | $html .= '';
14 | $html .= 'Ordered (numbered) list:
';
15 | $html .= '- Item 1
- Item 2
';
16 |
17 | \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
18 |
19 | // Save file
20 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
21 | if (!CLI) {
22 | include_once 'Sample_Footer.php';
23 | }
24 |
--------------------------------------------------------------------------------
/samples/Sample_27_Field.php:
--------------------------------------------------------------------------------
1 | addSection();
10 |
11 | // Add Field elements
12 | // See Element/Field.php for all options
13 | $section->addText('Date field:');
14 | $section->addField('DATE', array('dateformat' => 'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat'));
15 |
16 | $section->addText('Page field:');
17 | $section->addField('PAGE', array('format' => 'ArabicDash'));
18 |
19 | $section->addText('Number of pages field:');
20 | $section->addField('NUMPAGES', array('format' => 'Arabic', 'numformat' => '0,00'), array('PreserveFormat'));
21 |
22 | $textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
23 | $textrun->addText('This is the date of lunar calendar ');
24 | $textrun->addField('DATE', array('dateformat' => 'd-M-yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar'));
25 | $textrun->addText(' written in a textrun.');
26 |
27 | // Save file
28 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
29 | if (!CLI) {
30 | include_once 'Sample_Footer.php';
31 | }
32 |
--------------------------------------------------------------------------------
/samples/Sample_28_ReadRTF.php:
--------------------------------------------------------------------------------
1 | getProtection()->setEditing('forms');
8 |
9 | // New section
10 | $section = $phpWord->addSection();
11 |
12 | $textrun = $section->addTextRun();
13 | $textrun->addText('Form fields can be added in a text run and can be in form of textinput ');
14 | $textrun->addFormField('textinput')->setName('MyTextBox');
15 | $textrun->addText(', checkbox ');
16 | $textrun->addFormField('checkbox')->setDefault(true);
17 | $textrun->addText(', or dropdown ');
18 | $textrun->addFormField('dropdown')->setEntries(array('Choice 1', 'Choice 2', 'Choice 3'));
19 | $textrun->addText('. You have to set document protection to "forms" to enable dropdown.');
20 |
21 | $section->addText('They can also be added as a stand alone paragraph.');
22 | $section->addFormField('textinput')->setValue('Your name');
23 |
24 | // Save file
25 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
26 | if (!CLI) {
27 | include_once 'Sample_Footer.php';
28 | }
29 |
--------------------------------------------------------------------------------
/samples/Sample_34_SDT.php:
--------------------------------------------------------------------------------
1 | addSection();
10 |
11 | $textrun = $section->addTextRun();
12 | $textrun->addText('Combobox: ');
13 | $textrun->addSDT('comboBox')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2'));
14 |
15 | $textrun = $section->addTextRun();
16 | $textrun->addText('Date: ');
17 | $textrun->addSDT('date');
18 |
19 | $textrun = $section->addTextRun();
20 | $textrun->addText('Drop down list: ');
21 | $textrun->addSDT('dropDownList')->setListItems(array('1' => 'Choice 1', '2' => 'Choice 2'));
22 |
23 | // Save file
24 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
25 | if (!CLI) {
26 | include_once 'Sample_Footer.php';
27 | }
28 |
--------------------------------------------------------------------------------
/samples/Sample_35_InternalLink.php:
--------------------------------------------------------------------------------
1 | addSection();
9 | $section->addTitle('This is page 1', 1);
10 | $linkIsInternal = true;
11 | $section->addLink('MyBookmark', 'Take me to page 3', null, null, $linkIsInternal);
12 | $section->addPageBreak();
13 | $section->addTitle('This is page 2', 1);
14 | $section->addPageBreak();
15 | $section->addTitle('This is page 3', 1);
16 | $section->addBookmark('MyBookmark');
17 |
18 | // Save file
19 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
20 | if (!CLI) {
21 | include_once 'Sample_Footer.php';
22 | }
23 |
--------------------------------------------------------------------------------
/samples/Sample_36_RTL.php:
--------------------------------------------------------------------------------
1 | addSection();
10 |
11 | $textrun = $section->addTextRun();
12 | $textrun->addText('This is a Left to Right paragraph.');
13 |
14 | $textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::END));
15 | $textrun->addText('سلام این یک پاراگراف راست به چپ است', array('rtl' => true));
16 |
17 | // Save file
18 | echo write($phpWord, basename(__FILE__, '.php'), $writers);
19 | if (!CLI) {
20 | include_once 'Sample_Footer.php';
21 | }
22 |
--------------------------------------------------------------------------------
/samples/Sample_Footer.php:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |