Forum


Replies: 7   Views: 2585
Replacevariablebyhtml css render with root tag not working
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by Bouillou  · 05-05-2019 - 09:48

Hi,

I am using PHPDocx Corporate license 5.5.

Could you tell me why this example doesn't not render the text in red color?

$docx->replaceVariableByHTML('LETTER_TEXT3', 'block', "<html><body style='color: red;'><p>This is a test.</p><p>Another test which must be separated in a specific paragraph.</p><p>And another</p></body></html>", array('parseDivsAsPs' => true));

Best regards,

Sébastien

Posted by admin  · 06-05-2019 - 06:59

Hello,

We have run the following script using phpdocx 5.5:

$docx = new CreateDocx();
$docx->embedHTML('
<html>
<head>
</head>
<body style="color: red">

<p>This is a test.</p>
<p>Another test which must be separated in a specific paragraph.</p>
<p>And another</p>

</body>
</html>');

$docx->createDocx('output.docx');

And the color is applied correctly to all paragraphs. As you are using a template maybe it has some custom style or any other content that is overwriting the new contents/styles. We recommend you to check your template.

Regards.

Posted by Bouillou  · 06-05-2019 - 09:10

Thank you for your reply.

I tested your example and the text is not render in red color. Therefore, it is not related to the template.

Is it a bug in the corprate version 5.5?

Below, the exact code I am running :

<?php

require_once '../../../Classes/Phpdocx/Create/CreateDocx.inc';

$docx = new Phpdocx\Create\CreateDocx();
$docx->embedHTML('
<html>
<head>
</head>
<body style="color: red">

<p>This is a test.</p>
<p>Another test which must be separated in a specific paragraph.</p>
<p>And another</p>

</body>
</html>');

$docx->createDocx('output');

 

Posted by admin  · 06-05-2019 - 09:23

Hello,

The previous script was tested using phpdocx Corporate 5.5, classic package. If we test the same script using phpdocx Corporate 5.5 namespaces package:

<?php

require_once 'phpdocx/5.5/namespaces/Classes/Phpdocx/Create/CreateDocx.inc';

$docx = new Phpdocx\Create\CreateDocx();
$docx->embedHTML('
<html>
<head>
</head>
<body style="color: red">

<p>This is a test.</p>
<p>Another test which must be separated in a specific paragraph.</p>
<p>And another</p>

</body>
</html>');

$docx->createDocx('output');

The output is the same. We have opened the DOCX output with MS Word 2007 to MS Word 2016 and LibreOffice 6 using PHP 5.6, PHP 7.0, PHP 7.1 and PHP 7.2 and the style is correctly applied in all cases.

We recommend you to check if Tidy is installed and enabled, and test the same script using the trial package of phpdocx 9 (https://www.phpdocx.com/download_trial). And also test the samples available on the documentation page (https://www.phpdocx.com/documentation/introduction/html-to-word-PHP).

Regards.

Posted by Bouillou  · 06-05-2019 - 10:05

Thank you very much for your support. Installing and enabling Tidy works with versions 5.5 and 9 Trial.

However, setting "font-size: initial" permitting to use the font size of the "Normal" style has a wird side effect because <p> are no longer considered as paragraph.

Could you try the code below?

<?php

require_once '../../../classes/CreateDocx.php';

$docx = new CreateDocx();

$docx->embedHTML('
<html>
<head>
</head>
<body style="color: red; font-size: initial;">

<p>This is a test.</p>
<p>Another test which must be separated in a specific paragraph.</p>
<p>And another</p>

</body>
</html>');

$docx->createDocx('output');

 

Posted by admin  · 06-05-2019 - 11:09

Hello,

The initial value is not supported by the HTML to DOCX transformation, so you can see a weird output if you try to use it. In your script, the paragraphs aren't removed in the DOCX output, but the spacing is set to 0 for all paragraphs:

<w:p>
    <w:pPr>
        <w:widowControl w:val="on"/>
        <w:pBdr/>
        <w:spacing w:after="0" w:before="0" w:line="240" w:lineRule="auto"/>
        <w:ind w:left="0" w:right="0"/>
        <w:jc w:val="left"/>
    </w:pPr>
    <w:r>
        <w:rPr>
            <w:color w:val="FF0000"/>
        </w:rPr>
        <w:t xml:space="preserve">This is a test.</w:t>
    </w:r>
</w:p>

Regards.

Posted by Bouillou  · 06-05-2019 - 11:47

OK, it is working if I define the font size in each <p> with a <span> like example below. This font size definition is very useful when working with template because the size of the font is the same as the rest of the static text in the document.

But, it would be better to define the real font size of the Normal style in the <body>. So, is there a way to retrieve the font size of the Normal style of a template in order to set it like <body style='font-size: ??px'> ?

<?php

require_once '../../../classes/CreateDocx.php';

$docx = new CreateDocx();

$docx->embedHTML('
<html>
<head>
</head>
<body style="color: red;">

<p><span style="font-size:initial">This is a test.</span></p>
<p><span style="font-size:initial">Another test which must be separated in a specific paragraph.</span></p>
<p><span style="font-size:initial">And another</span></p>

</body>
</html>');

$docx->createDocx('output');

Best regards,

Sébastien

Posted by admin  · 06-05-2019 - 11:51

Hello,

UPDATE: Premium licenses include the stylesReplacementType option to use the placeholder styles when doing replacements by HTML.

You can get all information about styles,using getWordStyles, available since phpdocx 9 (https://www.phpdocx.com/news/post/phpdocx-v9-release-notes/218).

For example, using this method you can get the font size used in a content or style (default, custom and others) from a template or a DOCX generated from scratch and use it to set font size values with HTML, addText and other methods.

Regards.