Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions samples/resources/Sample_30_ReadHTML.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ <h1>Adding element via HTML</h1>
<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</li></ul></ul>
<p>Ordered (numbered) list:</p>
<ol><li>Item 1</li><li>Item 2</li></ol>


<p style="line-height:2">Double height</p>

<h2>Includes images</h2>
<img src="https://phpword.readthedocs.io/en/latest/_images/phpword.png" alt=""/>
</body>
</html>
44 changes: 43 additions & 1 deletion src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PhpOffice\PhpWord\Element\AbstractContainer;
use PhpOffice\PhpWord\Element\Row;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\NumberFormat;

Expand All @@ -32,6 +33,7 @@ class Html
{
private static $listIndex = 0;
private static $xpath;
private static $options;

/**
* Add HTML parts.
Expand All @@ -44,13 +46,17 @@ class Html
* @param string $html The code to parse
* @param bool $fullHTML If it's a full HTML, no need to add 'body' tag
* @param bool $preserveWhiteSpace If false, the whitespaces between nodes will be removed
* @param array $options:
* + IMG_SRC_SEARCH: optional to speed up images loading from remote url when files can be found locally
* + IMG_SRC_REPLACE: optional to speed up images loading from remote url when files can be found locally
*/
public static function addHtml($element, $html, $fullHTML = false, $preserveWhiteSpace = true)
public static function addHtml($element, $html, $fullHTML = false, $preserveWhiteSpace = true, $options = null)
{
/*
* @todo parse $stylesheet for default styles. Should result in an array based on id, class and element,
* which could be applied when such an element occurs in the parseNode function.
*/
self::$options = $options;

// Preprocess: remove all line ends, decode HTML entity,
// fix ampersand and angle brackets and add body tag for HTML fragments
Expand Down Expand Up @@ -141,6 +147,7 @@ protected static function parseNode($node, $element, $styles = array(), $data =
'sup' => array('Property', null, null, $styles, null, 'superScript', true),
'sub' => array('Property', null, null, $styles, null, 'subScript', true),
'span' => array('Span', $node, null, $styles, null, null, null),
'font' => array('Span', $node, null, $styles, null, null, null),
'table' => array('Table', $node, $element, $styles, null, null, null),
'tr' => array('Row', $node, $element, $styles, null, null, null),
'td' => array('Cell', $node, $element, $styles, null, null, null),
Expand Down Expand Up @@ -648,6 +655,41 @@ private static function parseImage($node, $element)
break;
}
}
if (strpos($src, 'data:image') !== false) {
$tmpDir = Settings::getTempDir() . '/';

$match = array();
preg_match('/data:image\/(\w+);base64,(.+)/', $src, $match);

$src = $imgFile = $tmpDir . uniqid() . '.' . $match[1];

$ifp = fopen($imgFile, 'wb');
Copy link
Contributor

Choose a reason for hiding this comment

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


fwrite($ifp, base64_decode($match[2]));
fclose($ifp);
}
$src = urldecode($src);

if (!is_file($src)
&& !is_null(self::$options)
&& isset(self::$options['IMG_SRC_SEARCH'])
&& isset(self::$options['IMG_SRC_REPLACE'])) {
$src = str_replace(self::$options['IMG_SRC_SEARCH'], self::$options['IMG_SRC_REPLACE'], $src);
}

if (!is_file($src)) {
if ($imgBlob = file_get_contents($src)) {
$tmpDir = Settings::getTempDir() . '/';
$match = array();
preg_match('/.+\.(\w+)$/', $src, $match);
$src = $tmpDir . uniqid() . '.' . $match[1];

$ifp = fopen($src, 'wb');

fwrite($ifp, $imgBlob);
fclose($ifp);
}
}
$newElement = $element->addImage($src, $style);

return $newElement;
Expand Down
72 changes: 72 additions & 0 deletions tests/PhpWord/Shared/HtmlTest.php

Large diffs are not rendered by default.