Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
7553b4c
setImageValue() + fix adding files via ZipArchive
SailorMax Oct 25, 2017
76bc755
tab to spaces
SailorMax Oct 25, 2017
8e8fd8e
tabs to spaces
SailorMax Oct 25, 2017
25f24d1
syntax fixes
SailorMax Oct 25, 2017
879a9b4
syntax fixes
SailorMax Oct 25, 2017
8a9293a
tabs to spaces
SailorMax Oct 25, 2017
869a363
syntax fixes
SailorMax Oct 25, 2017
5179074
php 5.3 compatibility fix
SailorMax Oct 25, 2017
aaf59ac
syntax fixes
SailorMax Oct 25, 2017
a37f60f
php 5.3 compatibility fix
SailorMax Oct 25, 2017
8e28879
Merge branch 'develop' into template_processor__set_image_value
SailorMax Nov 10, 2017
9137251
Merge branch 'develop' into template_processor__set_image_value
SailorMax Nov 10, 2017
1e2ef76
syntax fix
SailorMax Nov 11, 2017
dcca596
Merge branch 'template_processor__set_image_value' of https://github.…
SailorMax Nov 11, 2017
3109939
fix phpdoc variable name
SailorMax Nov 11, 2017
ae27499
Merge branch 'develop' into template_processor__set_image_value
SailorMax Dec 7, 2017
6f63982
Changed logic that determines extension image file extension for docu…
gatis-ozols Jan 5, 2018
ff512f4
Merge pull request #1 from gatis-ozols/template_processor__set_image_…
SailorMax Jan 18, 2018
04dd86f
syntax fixes
SailorMax Jan 18, 2018
dbcbbeb
support <w:t> tags with arguments
SailorMax Feb 16, 2018
ef50aeb
simpler replace inline variables + test
SailorMax Mar 16, 2018
65ebf4a
spaces fix
SailorMax Mar 16, 2018
9fcf064
allow setup size of image into template variable
SailorMax May 25, 2018
de25054
restore using mime types
SailorMax May 25, 2018
a6b2600
better variable names
SailorMax May 25, 2018
2031ccc
fix variable name
SailorMax May 25, 2018
28fd8a0
reduce size of methods
SailorMax May 25, 2018
a4f8153
fix broken xml structure after replace variables
SailorMax May 25, 2018
3f87352
more reduce functions size
SailorMax May 25, 2018
5038b3b
more reduce methods size
SailorMax May 25, 2018
7959651
support of 'ratio' replace attribute + documentation
SailorMax May 25, 2018
c6515db
update unit test
SailorMax May 25, 2018
58f52e3
Update CHANGELOG.md
troosan Dec 23, 2018
d8dae9e
Merge branch 'develop' into template_processor__set_image_value
troosan Dec 23, 2018
495227c
fix check style error
troosan Dec 23, 2018
6bedbd8
php 7.3 should not fail anymore
troosan Dec 23, 2018
3b31a65
Revert "php 7.3 should not fail anymore"
troosan Dec 23, 2018
3673dd5
ignore dependencies for php 7.3
troosan Dec 23, 2018
ef40717
rewrite test to use phpunit assertions
troosan Dec 24, 2018
8ba5bfd
improve code coverage
troosan Dec 24, 2018
d771461
use old phpunit compatible assertions
troosan Dec 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ matrix:
- php: 5.3
- php: 7.0
- php: 7.3
allow_failures:
- php: 7.3

cache:
directories:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ v0.16.0 (xx dec 2018)
- Add setting Chart Title and Legend visibility @Tom-Magill #1433
- Add ability to pass a Style object in Section constructor @ndench #1416
- Add support for hidden text @Alexmg86 #1527
- Add support for setting images in TemplateProcessor @SailorMax #1170

### Fixed
- Fix regex in `cloneBlock` function @nicoder #1269
Expand Down
18 changes: 18 additions & 0 deletions docs/templates-processing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,32 @@ You can create an OOXML document template with included search-patterns (macros)

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}``.

The search-pattern model for images can be like:
- ``${search-image-pattern}``
- ``${search-image-pattern:[width]:[height]:[ratio]}``
- ``${search-image-pattern:[width]x[height]}``
- ``${search-image-pattern:size=[width]x[height]}``
- ``${search-image-pattern:width=[width]:height=[height]:ratio=false}``
Where:
- [width] and [height] can be just numbers or numbers with measure, which supported by Word (cm|mm|in|pt|pc|px|%|em|ex)
- [ratio] uses only for ``false``, ``-`` or ``f`` to turn off respect aspect ration of image. By default template image size uses as 'container' size.

Example:

.. code-block:: doc

${CompanyLogo}
${UserLogo:50:50} ${Name} - ${City} - ${Street}

.. code-block:: php

$templateProcessor = new TemplateProcessor('Template.docx');
$templateProcessor->setValue('Name', 'John Doe');
$templateProcessor->setValue(array('City', 'Street'), array('Detroit', '12th Street'));

$templateProcessor->setImageValue('CompanyLogo', 'path/to/company/logo.png');
$templateProcessor->setImageValue('UserLogo', array('path' => 'path/to/logo.png', 'width' => 100, 'height' => 100, 'ratio' => false));

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``).

See ``Sample_07_TemplateCloneRow.php`` for example on how to create
Expand Down
10 changes: 8 additions & 2 deletions src/PhpWord/Shared/ZipArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public function open($filename, $flags = null)
{
$result = true;
$this->filename = $filename;
$this->tempDir = Settings::getTempDir();

if (!$this->usePclzip) {
$zip = new \ZipArchive();
Expand All @@ -139,7 +140,6 @@ public function open($filename, $flags = null)
$this->numFiles = $zip->numFiles;
} else {
$zip = new \PclZip($this->filename);
$this->tempDir = Settings::getTempDir();
$zipContent = $zip->listContent();
$this->numFiles = is_array($zipContent) ? count($zipContent) : 0;
}
Expand Down Expand Up @@ -245,7 +245,13 @@ public function pclzipAddFile($filename, $localname = null)
$pathRemoved = $filenameParts['dirname'];
$pathAdded = $localnameParts['dirname'];

$res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
if (!$this->usePclzip) {
$pathAdded = $pathAdded . '/' . ltrim(str_replace('\\', '/', substr($filename, strlen($pathRemoved))), '/');
//$res = $zip->addFile($filename, $pathAdded);
$res = $zip->addFromString($pathAdded, file_get_contents($filename)); // addFile can't use subfolders in some cases
} else {
$res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
}

if ($tempFile) {
// Remove temp file, if created
Expand Down
Loading