Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to TemplateProcessor for rendering HTML content.Include Image #2547

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
120 changes: 120 additions & 0 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
use PhpOffice\PhpWord\Exception\CopyFileException;
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\Shared\Text;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Shared\ZipArchive;
use PhpOffice\PhpWord\Writer\Word2007;
use ReflectionClass;
use Throwable;
use XSLTProcessor;

Expand Down Expand Up @@ -315,6 +318,123 @@ public function setComplexBlock($search, Element\AbstractElement $complexType):
$this->replaceXmlBlock($search, $xmlWriter->getData(), 'w:p');
}

/**
* @param string $search
* @param string $htmlContent
* @param bool $fullHtml
*/
public function setHtmlBlock($search, $htmlContent, $fullHtml = false): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
//deal remote load Image
$pattern = '/<img[^>]+src\s*=\s*["\']([^"\']+)["\'][^>]*>/i';
preg_match_all($pattern, $htmlContent, $matches);
$imageSrcList = $matches[1];
if (!empty($imageSrcList)) {
foreach ($imageSrcList as $imageSrc) {
try {
$content = file_get_contents($imageSrc);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section of the code is quite clever, but (a) I'm not really sure it's needed, and (b) I don't think you've coded it correctly. I would personally eliminate all the code after addSection and before addHtml and just let it fail later if the file isn't available; your changes to your test member are sufficient to address my original concern by making the files local rather than external. But, I can see that this checking might be perceived as a benefit, so let's discuss (b). file_get_contents does not normally throw an exception (I am not expert in the Php internals, so I suppose there might be some edge case where it throws); it normally just returns false if it fails and issues some warning messages describing the failure. So, I think what you want to do is:

foreach ... {
    $content = @file_get_contents(...); // suppress warning messages
    if ($content === false) {
        $localImg = ...
        $htmlContent = ...
    }
}

Copy link
Author

@Maybe-U Maybe-U Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks you for your Suggestion,I wasn't thoughtful enough ,I reviewed the html::add html method,Use @ to suppress errors when processing images in this method
image
so i eliminate all the code after addSection and before addHtml Perhaps the abnormal picture should be handled by the user itself.

In addition, this is my first time to submit pr. Could you please help me merge this pr
@oleibman

} catch (\Exception $e) {
$localImg = __DIR__ . '/resources/doc.png';
$htmlContent = str_replace($imageSrc, $localImg, $htmlContent);
}
}
}
Html::addHtml($section, $htmlContent, $fullHtml);
$zip = $this->zip();
$obj = new Word2007($phpWord);
$refClass = new ReflectionClass(Word2007::class);
$addFilesToPackage = $refClass->getMethod('addFilesToPackage');
$addFilesToPackage->setAccessible(true);
$sectionMedia = Media::getElements('section');
//add image to zip
if (!empty($sectionMedia)) {
//insert image to zip
$res = $addFilesToPackage->invoke($obj, $zip, $sectionMedia);
$registerContentTypes = $refClass->getMethod('registerContentTypes');
$registerContentTypes->setAccessible(true);
$registerContentTypes->invoke($obj, $sectionMedia);

$relationships = $refClass->getProperty('relationships');
$relationships->setAccessible(true);
$tmpRelationships = [];
foreach ($sectionMedia as $element) {
$tmpRelationships[] = $element;
}
$relationships->setValue($obj, $tmpRelationships);
}
$documentWriterPart = $obj->getWriterPart('Document');
$relsDocumentWriterPart = $obj->getWriterPart('RelsDocument');
$documentXml = $documentWriterPart->write();
$relsDocumentXml = $relsDocumentWriterPart->write();
// Load the XML string into a SimpleXMLElement
$xml = simplexml_load_string($documentXml);
// Extract content between <w:body> tags
if ($xml === false) {
return;
}
$bodyContent = $xml->xpath('//w:body/*');
// Output the extracted content
$documentBodyStr = '';
if ($bodyContent) {
foreach ($bodyContent as $element) {
$documentBodyStr .= $element->asXML();
}
}

//replace html content r:id vaule avoid rid conflict
$rIdsElement = $xml->xpath('//*[@r:id]');
$rIdValuesMap = [];
if ($rIdsElement) {
foreach ($rIdsElement as $idEle) {
$rid = (string) $idEle->attributes('r', true)->id;
$rIdValuesMap[$rid] = $rid;
}
}
if (!empty($rIdValuesMap)) {
foreach ($rIdValuesMap as $rid => $value) {
$replactVulue = $rid . '-1';
$rIdValuesMap[$rid] = $replactVulue;
$documentBodyStr = str_replace($rid, $replactVulue, $documentBodyStr);
}
}
//replace document.xml
$this->replaceXmlBlock($search, $documentBodyStr, 'w:p');

$xml = simplexml_load_string($relsDocumentXml);
if ($xml === false) {
return;
}
// Register the namespace
$xml->registerXPathNamespace('ns', 'http://schemas.openxmlformats.org/package/2006/relationships');
// Use XPath to find all Relationship nodes
$RelationshipXmls = $xml->xpath('//ns:Relationship');
$RelationshipStr = '';
if ($RelationshipXmls) {
foreach ($RelationshipXmls as $relationshipXml) {
$rid = (string) $relationshipXml->attributes();
if (isset($rIdValuesMap[$rid])) {
$tmpStr = $relationshipXml->asXML();
if ($tmpStr != false) {
$tmpStr = str_replace($rid, $rIdValuesMap[$rid], $tmpStr);
$RelationshipStr .= $tmpStr;
}
}
}
}

//add relation to document.xml.rels
if ($RelationshipStr) {
$relsFileName = $this->getRelationsName($this->getMainPartName());
$content = $this->tempDocumentRelations[$this->getMainPartName()];
$endStr = '</Relationships>';
$replaceValue = $RelationshipStr . $endStr;
$content = str_replace($endStr, $replaceValue, $content);
$this->tempDocumentRelations[$this->getMainPartName()] = $content;
}
}

/**
* @param mixed $search
* @param mixed $replace
Expand Down
17 changes: 17 additions & 0 deletions tests/PhpWordTests/TemplateProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1630,4 +1630,21 @@ public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro(): void
$templateProcessor->setUpdateFields(false);
self::assertStringContainsString('<w:updateFields w:val="false"/>', $templateProcessor->getSettingsPart());
}

public function testSetHtml(): void
{
Settings::setOutputEscapingEnabled(true);
$image1 = __DIR__ . '/_files/images/earth.jpg';
$image2 = __DIR__ . '/_files/images/mars.jpg';
$content = '<p><img src="' . $image1 . '" /></p>
<p><img src="' . $image2 . '" /></p>
<p>HPJ LDAP(Lightweight Directory Access Protocol),轻量级目录访问协议,是一种在线目录访问协议,主要用于目录中资源的搜索和查询。如果在用户可控制的输入中没有对 LDAP 语法进行除去或引用,那么生成的 LDAP 查询可能会导致</p>';
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/template_to_html.docx');
$templateProcessor->setHtmlBlock('html_content', $content);
$docName = 'html-to-template-test.docx';
$templateProcessor->saveAs($docName);
$docFound = file_exists($docName);
unlink($docName);
self::assertTrue($docFound);
}
}
Binary file not shown.
Loading