Forum


Replies: 9   Views: 3758
Rtl lists generation from html
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 nregev  · 09-12-2016 - 17:16

Hi !

I'm embedding HTML lists into a docx document and no matter what I try, the lists always come our LTR.
When viewing the same HTML in a browser things look fine (i.e. RTL).

assume this simple HTML example :

<ul>
   <li>
      aaa
   </li>
   <li>
      bbb
   </li>
</ul>

Things I've already tried (+ combinations of them) :

  • setting the document to RTL using setRTL() function (before / after embedding the HTML)
  • setting the document dir attribute to 'rtl'
  • placing the <ul> tag in a div with style="direction: rtl;"
  • setting a style="direction: rtl;" on the <ul> tag and/or <li> tags
  • setting dir="rtl" on the <ul> tag and/or <li> tags

The only thing that made *some* difference was to set the alignment of the <ul> to 'right' but obviously this yields the wrong results as the text shows up to the right of the list "symbol" (e.g. dot / number) instead of to it's left.

For viewing the docx file I've already tried :

  • MS Word viewer (with the conversion addon)
  • MS Word 2013
  • Libre Office 4.2.8.2

All yielded the same results.

Any clues on how to solve this ??

Thanks.

 

Posted by admin  · 09-12-2016 - 19:41

Hello,

We recommend to use the API methods to add RTL content, as you can check on:

http://www.phpdocx.com/documentation/introduction/right-left-languages-tutorial

embedHTML doesn't include an option to set RTL.

Almost all HTML imported using the embedHTML method can include this attribute:

dir="rtl"

to set RTL direction. In the case of lists this options is missing, so we recommend you to add the RTL list using the addList method adding placeholders in each list item and then use replaceVariableByHTML (http://www.phpdocx.com/api-documentation/templates/replace-variable-html-Word-document) to replace the placeholders in the list by HTML.

Anyway, you can try this quick patch to add bidi support in lists (including dir="rtl" in each li tag). Edit the file classes/HTML2WordML.inc and in the method generateListPr add this code (around line 1964):

if ((isset($properties['dir']) && strtolower($properties['dir']) == 'rtl') ||
                (isset($properties['direction']) && strtolower($properties['direction']) == 'rtl')) {
    $stringListPr .= '<w:bidi w:val="1" />';
}

The code must be:

if (!$this->strictWordStyles) {
    if (isset($properties['background_color']) && is_array($properties['background_color'])) {
        $color = $properties['background_color'];
        $color = $this->wordMLColor($color);
        $stringListPr .='<w:shd w:val="clear"  w:color="auto" w:fill="' . $color . '" />';
    }
    $stringListPr .= $this->pPrSpacing($properties);
}
if ((isset($properties['dir']) && strtolower($properties['dir']) == 'rtl') ||
 (isset($properties['direction']) && strtolower($properties['direction']) == 'rtl')) {
    $stringListPr .= '<w:bidi w:val="1" />';
}
if (isset($properties['list_style_type']) && $properties['list_style_type'] == 'none') {
    $stringListPr .= $this->pPrIndent($properties);
}

Also don't forget to enable these options:

bidi = "true"
rtl = "true"

as explained on http://www.phpdocx.com/documentation/introduction/right-left-languages-tutorial.

Regards.

Posted by nregev  · 10-12-2016 - 00:48

Hi there,

Unfortunately, these solutions don't seem to work.

replaceVariableByHTML, if I understand correctly, only works for template.
when I do try to use it, the code just crashes.
the code I tried looks something like this :

$html = '<p>$testing$</p>';

$doc = new CreateDocx();
$doc->setRTL();
$doc->embedHTML($html);

$doc->replaceVariableByHTML('testing', 'block', '<p>testing 123</p>');

also tried to add the code to HTML2WordML as per your suggestion but this didn't seem to work either.
(with setRTL and changing the configuration as suggested).

the HTML I've tested was this :

<ul dir="rtl">
   <li dir="rtl">
      aaa
   </li>
   <li dir="rtl">
      bbb
   </li>
</ul>

Any further sugestions ?

Nir.

Posted by admin  · 10-12-2016 - 07:34

Hello,

We have moved the request to the dev team. They're checking it and send a solution as soon as possible.

Regards.

Posted by admin  · 12-12-2016 - 08:02

Hello,

We have tested the change proposed by the admin of the forum and it's working fine. After adding this code in the file HTML2WordML:

if ((isset($properties['dir']) && strtolower($properties['dir']) == 'rtl') ||
         (isset($properties['direction']) && strtolower($properties['direction']) == 'rtl')) {
            $stringListPr .= '<w:bidi w:val="1" />';
}

this code generates a RTL list in the DOCX ouput:

<?php

require_once 'classes/CreateDocx.inc';

$docx = new CreateDocx();
$docx->setRTL();

$html = '<ul>
            <li dir="rtl">One</li>
            <li dir="rtl">Two <b>and a half</b></li>
        </ul>';
$docx->embedHTML($html);

$docx->createDocx('output_rtl');

We have tested the output with MS Word 2007, MS Word 2013 and LibreOffice 5. Please try this code running it standalone and check that PHP Tidy is enabled and running on your server.

For futher support about this issue, please write to contact[at]phpdocx.com

Regards,
Phpdocx support team

Posted by nregev  · 12-12-2016 - 12:28

Hey Guys,

Looks fine now, not wure why it didn't work the first time :(
AFAIK, it was the same fix exactly, maybe there was something different with the source HTML.

Thanks for your assistance !

Posted by admin  · 12-12-2016 - 15:02

Hello,

Just a quick reply to point out that this small change will be added in the master branch of the library within the next release.

Regards.

Posted by nregev  · 12-12-2016 - 20:41

Good news :) to know that this is not a "local hack" and it'll be there when updating.

Thanks for your support !

Posted by nregev  · 12-12-2016 - 22:15

Hello again.

I was working on other files here and found that this solution is somewhat unstable.
Did some further digging and found that if the <li> tag has a text-align style set to "right" then the list item will show on the left after all.

For example, this will create a list item on the left :

<ol>
   <li dir="rtl" style="text-align:right">
      <span>RTL</span>
   </li>
</ol>

For the moment, I've applied the following (somewhat rough, I know) sollution in HTML2WordML :

(added the 'unset' to the solution you've suggested previously)

if ((isset($properties['dir']) && strtolower($properties['dir']) == 'rtl') ||
    (isset($properties['direction']) && strtolower($properties['direction']) == 'rtl')) {
       unset($properties['text_align']);
       $stringListPr .= '<w:bidi w:val="1" />';
}

Will be happy to hear that this (or a better) solution will also be included in the next versions to come :)

Thanks.

Nir.

Posted by admin  · 13-12-2016 - 10:37

Hello,

That's how MS Word works. If you add a RTL list and set align as right, MS Word align it to the left in the page.

We recommend you to don't set align as right when adding a RTL list if you need it to keep on the right of the document.

Regards.