Forum


Replies: 6   Views: 415
Widowcontrol in combination with html inserted text
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 Dale.Cooper  · 19-10-2023 - 13:09

Hi everyone,

I am just new to phpdocx but I really love it. In my current usecase, I am inserting a lot of HTML to a template with these functions:

$docx = new CreateDocxFromTemplate('xxx.docx');

$docx->embedHTML($html_input);

Now I want to enable the widowcontrol for everything in the word document. I have set up this an combined it with embedHTML

$paragraphOptions = array(
    'widowControl' => true,
);

$docx->embedHTML($html_input, $paragraphOptions);

But it's not working. Is it possible, that the paragraphOptions only work for addText? In my usecase, I would like to stay with my preformatted html coming from another system and just hand it over as one junk rather splitting it up and rebuilding the structure in different paragraphs. 

Am I doing/misunderstanding anything wrong here? Is the widow control achievable with embedHTML or any other way to deal with this?

Thanks in advance!

 

Posted by admin  · 19-10-2023 - 13:17

Hello,

What version and license of phpdocx are you using?

Please note that the second parameter of embedHTML doesn't include the same options as addText. When transforming HTML, the widowControl option (default as true for paragraphs) can be set using HTML styles or a custom paragraph style.

If you need to change styles on-the-fly of existing contents in a DOCX you need to use customizeWordContent. available in Premium licenses.

Regards.

Posted by Dale.Cooper  · 19-10-2023 - 13:42

Hi and thank you for the super fast reply!
Currently I am testing everything out with the trial but will buy the Advanced shortly (maybe Premium as the relevant department might need password/encryption of docs).

Do you have any sugestion what would be better: html style or custom paragraph style? Am I right that the whole html chunk I am passing into the template then needs to be surrounded "fully" by the widowcontrol and within the word document, it will be recognized for every page break? Or do I still need to break the long html text up into smaller pieces?

 

Thanks again!

Posted by admin  · 19-10-2023 - 13:54

Hello,

You don't need to break the HTML into smaller pieces. You can apply supported CSS styles as needed.
If you don't want to change the HTML to be transformed, the easiest approach is using CSS styles. Setting custom styles may require adding new class to select the contents to be applied (HTML tags can also be used to set custom styles).

Please note that as default, HTML to DOCX sets widowControl as true.
For example, if you run:

$html = '<p>Text content</p>';
$docx->embedHTML($html);

The widowControl tag is enabled:

<w:p>
    <w:pPr>
        <w:widowControl w:val="on"/>
        <w:pBdr/>
        <w:spacing w:after="240" w:before="240" 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="000000"/>
            <w:sz w:val="24"/>
            <w:szCs w:val="24"/>
        </w:rPr>
        <w:t xml:space="preserve">Text content</w:t>
    </w:r>
</w:p>

If you open the DOCX with MS Word you can check (right click on the paragraph and "Line and Page Breaks" option) that the Widow/Orphan control option is enabled. You can disable widowControl using the page-break-before style (https://www.phpdocx.com/htmlapi-documentation/html-standard/insert-paragraph-text-Word-document-with-HTML):

$html = '<style>p{page-break-before: avoid;}</style><p>Text content</p>';
$docx->embedHTML($html);

After purchasing a license we can provide further support about this and other questions. After you have purchased a license, do not hesitate to send a message to contact[at]phpdocx with a HTML sample you are using so we can help you.

Regards.

Posted by Dale.Cooper  · 20-10-2023 - 06:13

Thank you very much! Good to know all these options. As widowcontrol is enabled by default with html (I can confirm this by checking in Word) I do not need to change anything regarding this setting.

However, it's not really working. I have an example where a sentence (two lines long) is broken into line 1 on page one (bottom) and line 2 on page 2 (top).

I am aware, that further support is bound to the license. I am still waiting for the department to decide which license and hopefully I can buy shortly - alway bad waiting :D

Have a great day!

 

 

Posted by admin  · 20-10-2023 - 06:44

Hello,

Please note that enabling Widow/Orphan Control as true in Word doesn't assure that all lines of the same paragraph are in the same page. These terms (widow and orphan) refer to one (and sometimes two or three) lines (https://wordribbon.tips.net/T012695_Controlling_Widows_and_Orphans.html).

To accomplish this task as you require, you should enable the option "Keep lines together". This setting can be enabled when transforming HTML to DOCX using the following style:

page-break-inside: avoid;

After purchasing a license we can provide further support if needed.

Regards.

Posted by Dale.Cooper  · 20-10-2023 - 10:14

I just purchased a license and I have sent a message via the contact form.

Thanks!