From 6ad54a84345b3800106ca28c164b5efcb6276125 Mon Sep 17 00:00:00 2001 From: philippe Date: Tue, 20 Nov 2018 15:00:26 +0100 Subject: [PATCH 1/4] Replace a block with a picture --- src/PhpWord/TemplateProcessor.php | 117 ++++++++++++++++++++++++++++-- 1 file changed, 111 insertions(+), 6 deletions(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 7a5eaf55bb..1677c40f07 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -11,19 +11,20 @@ * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. * * @see https://github.com/PHPOffice/PHPWord - * @copyright 2010-2018 PHPWord contributors + * @copyright 2010-2017 PHPWord contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ namespace PhpOffice\PhpWord; -use PhpOffice\Common\Text; use PhpOffice\PhpWord\Escaper\RegExp; use PhpOffice\PhpWord\Escaper\Xml; use PhpOffice\PhpWord\Exception\CopyFileException; use PhpOffice\PhpWord\Exception\CreateTemporaryFileException; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\ZipArchive; +use Zend\Stdlib\StringUtils; +use PhpOffice\PhpWord\Settings; class TemplateProcessor { @@ -62,6 +63,10 @@ class TemplateProcessor */ protected $tempDocumentFooters = array(); + // AJOUT + protected $_rels; + protected $_types; + /** * @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception * @@ -101,6 +106,7 @@ public function __construct($documentTemplate) $index++; } $this->tempDocumentMainPart = $this->fixBrokenMacros($this->zipClass->getFromName($this->getMainPartName())); + $this->_countRels=100; } /** @@ -113,7 +119,6 @@ public function __construct($documentTemplate) */ protected function transformSingleXml($xml, $xsltProcessor) { - libxml_disable_entity_loader(true); $domDocument = new \DOMDocument(); if (false === $domDocument->loadXML($xml)) { throw new Exception('Could not load the given XML document.'); @@ -193,7 +198,7 @@ protected static function ensureMacroCompleted($macro) */ protected static function ensureUtf8Encoded($subject) { - if (!Text::isUTF8($subject)) { + if (!StringUtils::isValidUtf8($subject)) { $subject = utf8_encode($subject); } @@ -323,7 +328,7 @@ public function cloneBlock($blockname, $clones = 1, $replace = true) { $xmlBlock = null; preg_match( - '/(<\?xml.*)(\${' . $blockname . '}<\/w:.*?p>)(.*)()/is', + '/(<\?xml.*)(\${' . $blockname . '}<\/w:.*?p>)(.*)()/is', $this->tempDocumentMainPart, $matches ); @@ -394,6 +399,12 @@ public function save() } $this->zipClass->addFromString($this->getMainPartName(), $this->tempDocumentMainPart); + if($this->_rels!=""){ + $this->zipClass->addFromString('word/_rels/document.xml.rels', $this->_rels); + } + if($this->_types!=""){ + $this->zipClass->addFromString('[Content_Types].xml', $this->_types); + } foreach ($this->tempDocumentFooters as $index => $xml) { $this->zipClass->addFromString($this->getFooterName($index), $xml); @@ -406,6 +417,100 @@ public function save() return $this->tempDocumentFilename; } + + /** + * Replace a block with a picture. + * Code from https://github.com/PHPOffice/PHPWord/issues/708 + * + * @param string $strKey the string to replace + * @param array $img the informations about the image. Associative array : 'src' => 'path.png', 'size'=>array(0=>width, 1=>height) + * + * @return string + */ + public function setImg( $strKey, $img){ + $strKey = '${'.$strKey.'}'; + $relationTmpl = ''; + + $imgTmpl = ''; + + $toAdd = $toAddImg = $toAddType = ''; + $aSearch = array('RID', 'IMG'); + $aSearchType = array('IMG', 'EXT'); + $countrels=$this->_countRels++; + //I'm work for jpg files, if you are working with other images types -> Write conditions here + $imgExt = 'jpg'; + $imgName = 'img' . $countrels . '.' . $imgExt; + + $this->zipClass->deleteName('word/media/' . $imgName); + $this->zipClass->addFile($img['src'], 'word/media/' . $imgName); + + $typeTmpl = ''; + + + $rid = 'rId' . $countrels; + $countrels++; + list($w,$h) = getimagesize($img['src']); + + if(isset($img['swh'])) //Image proportionally larger side + { + if($w<=$h) + { + $ht=(int)$img['swh']; + $ot=$w/$h; + $wh=(int)$img['swh']*$ot; + $wh=round($wh); + } + if($w>=$h) + { + $wh=(int)$img['swh']; + $ot=$h/$w; + $ht=(int)$img['swh']*$ot; + $ht=round($ht); + } + $w=$wh; + $h=$ht; + } + + if(isset($img['size'])) + { + $w = $img['size'][0]; + $h = $img['size'][1]; + } + + + $toAddImg .= str_replace(array('RID', 'WID', 'HEI'), array($rid, $w, $h), $imgTmpl) ; + if(isset($img['dataImg'])) + { + $toAddImg.=''.$this->limpiarString($img['dataImg']).''; + } + + $aReplace = array($imgName, $imgExt); + $toAddType .= str_replace($aSearchType, $aReplace, $typeTmpl) ; + + $aReplace = array($rid, $imgName); + $toAdd .= str_replace($aSearch, $aReplace, $relationTmpl); + + + $this->tempDocumentMainPart=str_replace('' . $strKey . '', $toAddImg, $this->tempDocumentMainPart); + //print $this->tempDocumentMainPart; + + if($this->_rels=="") + { + $this->_rels=$this->zipClass->getFromName('word/_rels/document.xml.rels'); + $this->_types=$this->zipClass->getFromName('[Content_Types].xml'); + } + + $this->_types = str_replace('', $toAddType, $this->_types) . ''; + $this->_rels = str_replace('', $toAdd, $this->_rels) . ''; + } + + function limpiarString($str) { + return str_replace( + array('&', '<', '>', "\n"), + array('&', '<', '>', "\n" . ''), + $str + ); + } /** * Saves the result document to the user defined file. @@ -573,4 +678,4 @@ protected function getSlice($startPosition, $endPosition = 0) return substr($this->tempDocumentMainPart, $startPosition, ($endPosition - $startPosition)); } -} +} \ No newline at end of file From 50540ca4d3178de223e47b8e5ab745fca275c480 Mon Sep 17 00:00:00 2001 From: psohm Date: Tue, 20 Nov 2018 15:02:51 +0100 Subject: [PATCH 2/4] Update TemplateProcessor.php --- src/PhpWord/TemplateProcessor.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 1677c40f07..87e07dc299 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -11,7 +11,7 @@ * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. * * @see https://github.com/PHPOffice/PHPWord - * @copyright 2010-2017 PHPWord contributors + * @copyright 2010-2018 PHPWord contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ @@ -24,7 +24,6 @@ use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\ZipArchive; use Zend\Stdlib\StringUtils; -use PhpOffice\PhpWord\Settings; class TemplateProcessor { @@ -678,4 +677,4 @@ protected function getSlice($startPosition, $endPosition = 0) return substr($this->tempDocumentMainPart, $startPosition, ($endPosition - $startPosition)); } -} \ No newline at end of file +} From c2b4abd37ad618222fc2df6e272422c73d05748c Mon Sep 17 00:00:00 2001 From: psohm Date: Tue, 20 Nov 2018 15:08:22 +0100 Subject: [PATCH 3/4] =?UTF-8?q?mise=20=C3=A0=20jour=20par=20rapport=20?= =?UTF-8?q?=C3=A0=20la=20src?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/PhpWord/TemplateProcessor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 87e07dc299..2caa9c7264 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -17,13 +17,13 @@ namespace PhpOffice\PhpWord; +use PhpOffice\Common\Text; use PhpOffice\PhpWord\Escaper\RegExp; use PhpOffice\PhpWord\Escaper\Xml; use PhpOffice\PhpWord\Exception\CopyFileException; use PhpOffice\PhpWord\Exception\CreateTemporaryFileException; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\ZipArchive; -use Zend\Stdlib\StringUtils; class TemplateProcessor { @@ -62,7 +62,6 @@ class TemplateProcessor */ protected $tempDocumentFooters = array(); - // AJOUT protected $_rels; protected $_types; @@ -118,6 +117,7 @@ public function __construct($documentTemplate) */ protected function transformSingleXml($xml, $xsltProcessor) { + libxml_disable_entity_loader(true); $domDocument = new \DOMDocument(); if (false === $domDocument->loadXML($xml)) { throw new Exception('Could not load the given XML document.'); From 6680c91e618bfe783e4e4e0a4f500e4fa0d11b24 Mon Sep 17 00:00:00 2001 From: psohm Date: Tue, 20 Nov 2018 15:13:58 +0100 Subject: [PATCH 4/4] update of the doc --- docs/templates-processing.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/templates-processing.rst b/docs/templates-processing.rst index af03b24586..5e778ed810 100644 --- a/docs/templates-processing.rst +++ b/docs/templates-processing.rst @@ -23,3 +23,12 @@ multirow from a single row in a template by using ``TemplateProcessor::cloneRow` See ``Sample_23_TemplateBlock.php`` for example on how to clone a block of text using ``TemplateProcessor::cloneBlock`` and delete a block of text using ``TemplateProcessor::deleteBlock``. + +You can also replace text block template with a picture. + +Example: + +.. code-block:: php + + $templateProcessor = new TemplateProcessor('Template.docx'); + $templateProcessor->setImg('LOGO',array('src' => '../assets/picture.png','size'=>array(0=>300, 1=>150))); \ No newline at end of file