From 889f4e338138c1ba4279e7fc92d925cb4469a670 Mon Sep 17 00:00:00 2001 From: lubosdz Date: Sat, 11 Jul 2020 23:03:51 +0200 Subject: [PATCH] fix converting margin to incorrect unit (points instead of twips) fix image alignment on float - relative to inner margin instead of page margin --- src/PhpWord/Shared/Html.php | 17 ++++++++++++----- tests/PhpWord/Shared/HtmlTest.php | 6 +----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index bf1a688b..332dd6f8 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -633,11 +633,18 @@ class Html } $styles['italic'] = $tValue; break; + case 'margin': + $cValue = Converter::cssToTwip($cValue); + $styles['spaceBefore'] = $cValue; + $styles['spaceAfter'] = $cValue; + break; case 'margin-top': - $styles['spaceBefore'] = Converter::cssToPoint($cValue); + // BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($cValue) + $styles['spaceBefore'] = Converter::cssToTwip($cValue); break; case 'margin-bottom': - $styles['spaceAfter'] = Converter::cssToPoint($cValue); + // BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($cValue) + $styles['spaceAfter'] = Converter::cssToTwip($cValue); break; case 'border-color': self::mapBorderColor($styles, $cValue); @@ -676,7 +683,7 @@ class Html } // normalization: in HTML 1px means tinest possible line width, so we cannot convert 1px -> 15 twips, coz line'd be bold, we use smallest twip instead $size = strtolower(trim($matches[1])); - // (!) BC change: up to ver. 0.17.0 Converter was incorrectly converting to points - Converter::cssToPoint($matches[1]) + // BC change: up to ver. 0.17.0 incorrectly converted to points - Converter::cssToPoint($size) $size = ($size == '1px') ? 1 : Converter::cssToTwip($size); // valid variants may be e.g. borderSize, borderTopSize, borderLeftColor, etc .. $styles["border{$which}Size"] = $size; // twips @@ -732,14 +739,14 @@ class Html case 'float': if (trim($v) == 'right') { $style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_RIGHT; - $style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE; + $style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_MARGIN; // inner section area $style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE; $style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT; $style['overlap'] = true; } if (trim($v) == 'left') { $style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_LEFT; - $style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_PAGE; + $style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_MARGIN; // inner section area $style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE; $style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT; $style['overlap'] = true; diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index 67c9fcaa..010d1918 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -779,7 +779,7 @@ HTML; $xpath = '/w:document/w:body/w:p[4]/w:pPr/w:spacing'; $this->assertTrue($doc->elementExists($xpath)); - $this->assertEquals(22.5, $doc->getElement($xpath)->getAttribute('w:before')); + $this->assertEquals(450, $doc->getElement($xpath)->getAttribute('w:before')); $this->assertEquals(0, $doc->getElement($xpath)->getAttribute('w:after')); $this->assertEquals(240, $doc->getElement($xpath)->getAttribute('w:line')); } @@ -869,10 +869,6 @@ HTML; Html::addHtml($section, $html); $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); - // uncomment to see results - file_put_contents('./table_src.html', $html); - file_put_contents('./table_result_'.time().'.docx', file_get_contents( TestHelperDOCX::getFile() ) ); - $xpath = '/w:document/w:body/w:tbl/w:tr/w:tc[1]/w:tcPr/w:vAlign'; $this->assertFalse($doc->elementExists($xpath));