Skip to content

Commit 6680709

Browse files
committed
fixed alignment
1 parent 4b3dc2d commit 6680709

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

lib/HtmlToDocx/src/h2d_htmlconverter.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function htmltodocx_html_allowed_children($tag = NULL)
4141
'h6' => array('a', 'em', 'i', 'strong', 'b', 'br', 'span', 'code', 'u', 'sup', 'text'),
4242
'p' => array('a', 'em', 'i', 'strong', 'b', 'ul', 'ol', 'img', 'table', 'br', 'span', 'code', 'u', 'sup', 'text', 'div', 'p'), // p does not nest - simple_html_dom will create a flat set of paragraphs if it finds nested ones.
4343
'div' => array('a', 'em', 'i', 'strong', 'b', 'ul', 'ol', 'img', 'table', 'br', 'span', 'code', 'u', 'sup', 'text', 'div', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'),
44-
'a' => array('text'), // PHPWord doesn't allow elements to be placed in link elements
44+
'a' => array('text', 'img'), // PHPWord doesn't allow elements to be placed in link elements
4545
'em' => array('a', 'strong', 'b', 'br', 'span', 'code', 'u', 'sup', 'text'), // Italic
4646
'i' => array('a', 'strong', 'b', 'br', 'span', 'code', 'u', 'sup', 'text'), // Italic
4747
'strong' => array('a', 'em', 'i', 'br', 'span', 'code', 'u', 'sup', 'text'), // Bold
@@ -95,6 +95,7 @@ function htmltodocx_clean_text($text)
9595
// Convert entities:
9696
//$text = html_entity_decode($text, ENT_COMPAT, 'UTF-8');
9797
$text = htmlspecialchars($text);
98+
$text = str_replace('&','&', $text);
9899
//echo ("\n out =".$text);
99100
return $text;
100101
}
@@ -163,6 +164,10 @@ function _htmltodocx_get_style($element, $state)
163164
}
164165
}
165166

167+
if (!empty($element->attr['align'])) {
168+
$inline_style_list[] = 'text-align: '.trim($element->attr['align']);
169+
}
170+
166171
// Look for style definitions of these inline styles:
167172
$inline_styles = array();
168173
if (!empty($inline_style_list) && !empty($style_sheet['inline'])) {
@@ -286,11 +291,8 @@ function htmltodocx_insert_html_recursive(&$phpword_element, $html_dom_array, &$
286291

287292
// Go through each element:
288293
foreach ($html_dom_array as $element) {
289-
290294
$old_style = $state['current_style'];
291-
292295
$state['current_style'] = _htmltodocx_get_style($element, $state);
293-
294296
switch ($element->tag) {
295297

296298
case 'p':
@@ -453,13 +455,21 @@ function htmltodocx_insert_html_recursive(&$phpword_element, $html_dom_array, &$
453455
}
454456
if ($state['context'] == 'section') {
455457

456-
if (strpos($element->href, 'http://') === 0) {
458+
if (strpos($element->href, 'http://') === 0 || strpos($element->href, 'https://') === 0) {
457459
$href = $element->href;
458460
} elseif (strpos($element->href, '/') === 0) {
459461
$href = $state['base_root'] . $element->href;
460462
} else {
461463
$href = $state['base_root'] . $state['base_path'] . $element->href;
462464
}
465+
466+
array_unshift($state['parents'], $element->tag);
467+
foreach($element->nodes as $node){
468+
if($node->tag=='img'){
469+
htmltodocx_insert_html_recursive($phpword_element, [$node], $state);
470+
}
471+
}
472+
array_shift($state['parents']);
463473
// Replace any spaces in url with %20 - to prevent errors in the Word
464474
// document:
465475
$state['textrun']->addLink(htmltodocx_url_encode_chars($href), htmltodocx_clean_text($element->innertext), $state['current_style']);
@@ -618,7 +628,6 @@ function htmltodocx_insert_html_recursive(&$phpword_element, $html_dom_array, &$
618628
break;
619629

620630
case 'img':
621-
$image_style = array();
622631
if ($element->height && $element->width) {
623632
$state['current_style']['height'] = $element->height;
624633
$state['current_style']['width'] = $element->width;
@@ -632,7 +641,7 @@ function htmltodocx_insert_html_recursive(&$phpword_element, $html_dom_array, &$
632641
$element_src = $element->src;
633642
}
634643

635-
if (strpos($element_src, 'http://') === 0) {
644+
if (strpos($element_src, 'http://') === 0 || strpos($element_src, 'https://') === 0) {
636645
// The image url is from another site. Most probably the image won't
637646
// appear in the Word document.
638647
$src = $element_src;
@@ -645,7 +654,17 @@ function htmltodocx_insert_html_recursive(&$phpword_element, $html_dom_array, &$
645654
if(isset($state['url_callback']) && is_callable($state['url_callback'])){
646655
$src = $state['url_callback']($element);
647656
}
657+
658+
659+
if(isset($state['current_style']['inline_with_text'])
660+
&& $state['current_style']['inline_with_text']){
661+
if(!isset($state['textrun'])){
662+
$state['textrun'] = $phpword_element->createTextRun();
663+
}
664+
$state['textrun']->addImage($src, $state['current_style']);
665+
}else{
648666
$phpword_element->addImage($src, $state['current_style']);
667+
}
649668

650669
break;
651670

lib/HtmlToDocx/src/styles.inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ function htmltodocx_styles_example()
114114
'text-decoration: underline' => array(
115115
'underline' => Font::UNDERLINE_SINGLE,
116116
),
117+
'text-decoration: line-through' => array(
118+
'strikethrough' => true,
119+
),
120+
117121
'vertical-align: left' => array(
118122
'align' => 'left',
119123
),
@@ -123,6 +127,19 @@ function htmltodocx_styles_example()
123127
'vertical-align: right' => array(
124128
'align' => 'right',
125129
),
130+
131+
'text-align: center' => array(
132+
'align' => 'center',
133+
),
134+
135+
'text-align: left' => array(
136+
'align' => 'left',
137+
),
138+
139+
'text-align: right' => array(
140+
'align' => 'right',
141+
),
142+
126143
);
127144

128145
return $styles;

0 commit comments

Comments
 (0)