Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add RTL support to Html #2427

Closed
focaloid-dev opened this issue Jul 23, 2023 · 23 comments
Closed

How to add RTL support to Html #2427

focaloid-dev opened this issue Jul 23, 2023 · 23 comments

Comments

@focaloid-dev
Copy link

focaloid-dev commented Jul 23, 2023

Can any one please help me make this $html as RTL with right aligned ?
$html = "<p> الألم الذي ربما تنجم عنه بعض ا.</p>";
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);

The File Output i have

sorry the $html is different here
image

@oleibman
Copy link
Contributor

$PHPWord = new PhpWord();
$section = $PHPWord->addSection();
$rtlFont = $PHPWord->addFontStyle('rtlFont', ['rtl' => true]);
$rtlParagraph = $PHPWord->addParagraphStyle('rtlParagraph', ['bidi' => true]);

$text = "الألم الذي ربما تنجم عنه بعض ا.";
$section->addText($text, 'rtlFont', 'rtlParagraph');

@focaloid-dev
Copy link
Author

focaloid-dev commented Jul 28, 2023

$PHPWord = new PhpWord();
$section = $PHPWord->addSection();
$rtlFont = $PHPWord->addFontStyle('rtlFont', ['rtl' => true]);
$rtlParagraph = $PHPWord->addParagraphStyle('rtlParagraph', ['bidi' => true]);

$text = "الألم الذي ربما تنجم عنه بعض ا.";
$section->addText($text, 'rtlFont', 'rtlParagraph');

thanks for the reply , my content is placed in db as raw html , ie from a text editor , so am using the Html::addHtml($section, $html, false, false); i need the contents of html as RTL , is it possible to do so , i hope yes
I followed this https://github.com/PHPOffice/PHPWord/blob/master/samples/Sample_26_Html.php

@oleibman
Copy link
Contributor

In the sample which you referenced, the input html specifies, among other things:

<p style="text-align: right; direction: rtl">

If you add that style to your html, you'll get close to the result you want. The trailing period is misplaced; I can offer some suggestions for that, but need to know whether this is what you are looking for first.

@focaloid-dev
Copy link
Author

focaloid-dev commented Jul 29, 2023

In the sample which you referenced, the input html specifies, among other things:

<p style="text-align: right; direction: rtl">

If you add that style to your html, you'll get close to the result you want. The trailing period is misplaced; I can offer some suggestions for that, but need to know whether this is what you are looking for first.

No this is not the thing i wanted , i cannot add a rtl directive in html tags since , the html i added above as paragraph is coming from database (Dynamic) and its the output of CKeditor i directly store my html and shows it in FE , so i need this whole html string to pass throught the lib functions and get the output as RTL , hope you get what am pointingto ?

See My Table :
image

@oleibman
Copy link
Contributor

Okay, try:

$PHPWord->setDefaultParagraphStyle(['bidi' => true]);

That should handle everything except the trailing period. There is no global option for setting rtl on the font, which is what you need to handle that. An RLM character at the end of the text would also work; you might be able to insert it with your editor, or you could add it as an html entity:

$html = "<p>  الألم الذي ربما تنجم عنه بعض ا.&rlm;</p>";

Note that, if you are using mixed rtl and ltr paragraphs, there is no current support for turning the bidi attribute off when the default paragraph style specifies true. The same might be true if the ability to add a default font rtl were to be added.

@focaloid-dev
Copy link
Author

'bidi' => true

this seems to be not working , my texts seem to be LTR , is there anything i can do with the ::html function

@oleibman
Copy link
Contributor

Sorry I haven't been able to help you more. Here is my entire program. Word2007 gets RTL and alignment correct; ODText and Html get RTL correct but not alignment; RTF seems to have problems with both. Do your results differ?

use PhpOffice\PhpWord\PhpWord;

$extension = 'docx';
$formatMapping = [
    'docx' => 'Word2007',
    'odt' => 'ODText',
    'rtf' => 'RTF',
    'html' => 'HTML',
];
$outputFormat = $formatMapping[$extension] ?? 'Word2007';

$PHPWord = new PhpWord();
$PHPWord->setDefaultParagraphStyle(['bidi' => true]);

$section = $PHPWord->addSection();
$html = "<p>  الألم الذي ربما تنجم عنه بعض ا.&rlm;</p>";
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);

$outname = "issue.2427c.$extension";
$PHPWord->save($outname, $outputFormat);
echo "Saved $outname\n";

@focaloid-dev
Copy link
Author

Sorry I haven't been able to help you more. Here is my entire program. Word2007 gets RTL and alignment correct; ODText and Html get RTL correct but not alignment; RTF seems to have problems with both. Do your results differ?

use PhpOffice\PhpWord\PhpWord;

$extension = 'docx';
$formatMapping = [
    'docx' => 'Word2007',
    'odt' => 'ODText',
    'rtf' => 'RTF',
    'html' => 'HTML',
];
$outputFormat = $formatMapping[$extension] ?? 'Word2007';

$PHPWord = new PhpWord();
$PHPWord->setDefaultParagraphStyle(['bidi' => true]);

$section = $PHPWord->addSection();
$html = "<p>  الألم الذي ربما تنجم عنه بعض ا.&rlm;</p>";
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);

$outname = "issue.2427c.$extension";
$PHPWord->save($outname, $outputFormat);
echo "Saved $outname\n";

alignment seems to be the issue , thanks for the help appreciate that

@oleibman
Copy link
Contributor

oleibman commented Aug 2, 2023

@focaloid-dev Even though you've closed this, I've done some more thinking about it. If you can, you might want to test against my PR #2343 (updated for this issue but with unlikely to be merged any time soon). Especially have a look at the new Sample_52, which echoes your case, and now works correctly for alignment and RTL for all of Word2007, ODText, RTF, HTML, and even MPDF. At least the results look correct on my system. I'd be curious to know if they look right on yours.

@alshoja
Copy link

alshoja commented Aug 2, 2023

@focaloid-dev Even though you've closed this, I've done some more thinking about it. If you can, you might want to test against my PR #2343 (updated for this issue but with unlikely to be merged any time soon). Especially have a look at the new Sample_52, which echoes your case, and now works correctly for alignment and RTL for all of Word2007, ODText, RTF, HTML, and even MPDF. At least the results look correct on my system. I'd be curious to know if they look right on yours.

Currently I pushed this thing in my project to a low priority one since it took most of my time to look around but that doesn't mean I am not going to look into this , def this will be a high priority one after my other checklists are ove for this project , am def gona try this PR and get u back if this works or not 👏

@malozaibi
Copy link

malozaibi commented Jan 6, 2024

@focaloid-dev Even though you've closed this, I've done some more thinking about it. If you can, you might want to test against my PR #2343 (updated for this issue but with unlikely to be merged any time soon). Especially have a look at the new Sample_52, which echoes your case, and now works correctly for alignment and RTL for all of Word2007, ODText, RTF, HTML, and even MPDF. At least the results look correct on my system. I'd be curious to know if they look right on yours.

If you may, can you give me an example, I tried the above this message and it make it rtl for <p> tag only but my <h1> <h2> still ltr.
and even the <p> is right aligned not direction rtl.

You mentioned an update in the quoted message, how did it help in rtl?

I would appreciate your help. I didn't reach a solution for a whole day now.

Update:
I used <p style="text-align: right; direction: rtl"> and still ltrl

@oleibman
Copy link
Contributor

oleibman commented Jan 6, 2024

@malozaibi My change was incorporated in PhpWord 1.2. Can you upload the file you are having a problem with?

@malozaibi
Copy link

Yes I upgraded to the last version but still.

$phpWord = new PhpWord();

// Define styles for headers
$phpWord->addTitleStyle(1, array('bold' => true, 'name' => 'Arial', 'size' => 16, 'bidi' => true, 'rtl'));
$phpWord->addTitleStyle(2, array('bold' => true, 'name' => 'Arial', 'size' => 14, 'bidi' => true, 'rtl'));
$phpWord->addTitleStyle(3, array('bold' => true, 'name' => 'Arial', 'size' => 12, 'bidi' => true, 'rtl'));
$phpWord->addTitleStyle(4, array('bold' => true, 'name' => 'Arial', 'size' => 10, 'bidi' => true, 'rtl'));
$phpWord->setDefaultParagraphStyle(['bidi' => true, 'rtl', true]);

// Add a section with bidi direction
$section = $phpWord->addSection(['bidi' => true, 'rtl', true]);
// Your HTML content
$htmlContent = '<h1>مرحبا 1</h1><h2>تجربة 2</h2><h3>تجربة تجربة</h3><h4>هناك hello هنا 4</h4><p>مرحبا here كلمة انجليزي.</p>';

// Add HTML content to the section
Html::addHtml($section, $htmlContent, false, false);

// Save the file
$filename = 'document.docx';
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($filename);

return response()->download($filename)->deleteFileAfterSend(true);

The output:
Screen Shot 2024-01-06 at 20 57 03
It should look like:
Screen Shot 2024-01-06 at 20 59 21
(I open the html in browser and adder dir="rtl" to the html tag)

@oleibman
Copy link
Contributor

oleibman commented Jan 6, 2024

I am not sure that this is being handled correctly by PhpWord. I need to investigate further, but I do have a workaround for your use case.

It's a little tricky. The font style uses property rtl; the paragraph style uses bidi. You have added bidi uselessly to the font style and not supplied a paragraph style. The following should work for you (but, ideally, you shouldn't have to specify setDefaultRtl nor setDefaultParagraphStyle, though they both will often be suitable).

$phpWord = new PhpWord();
Settings::setDefaultRtl(true);

// Define styles for headers
$phpWord->addTitleStyle(1, array('bold' => true, 'name' => 'Arial', 'size' => 16, 'rtl' => true), array('bidi' => 'true'));
$phpWord->addTitleStyle(2, array('bold' => true, 'name' => 'Arial', 'size' => 14, 'rtl' => true), array('bidi' => 'true'));
$phpWord->addTitleStyle(3, array('bold' => true, 'name' => 'Arial', 'size' => 12, 'rtl' => true), array('bidi' => 'true'));
$phpWord->addTitleStyle(4, array('bold' => true, 'name' => 'Arial', 'size' => 10, 'rtl' => true), array('bidi' => 'true'));
$phpWord->setDefaultParagraphStyle(['bidi' => true]);

@oleibman
Copy link
Contributor

oleibman commented Jan 6, 2024

Part of the problem is, of course, that I should have specified bidi => true rather than bidi => 'true' above in 4 places.

@malozaibi
Copy link

It is good now and all went to right. If I have only arabic letter it is ok, but if mixed with English, it is wrong.
I think this issue is correct and still affected #684
Now (after your help):
Screen Shot 2024-01-07 at 08 50 41
Should look like:
Screen Shot 2024-01-06 at 20 59 21
(See the last two lines)

@oleibman
Copy link
Contributor

oleibman commented Jan 7, 2024

$phpWord->addTitleStyle(4, array('bold' => true, 'name' => 'Arial', 'size' => 10, 'rtl' => true), array('bidi' => true, 'alignment' => 'start'));
$phpWord->setDefaultParagraphStyle(['bidi' => true, 'alignment' => 'start']);

I don't know why there are so many options affecting bidi; I have, in fact, discovered another one while researching this. I am working on a PR. In the meantime, adding alignment to your paragraph styles should work.

@malozaibi
Copy link

Same output unfortunately. they are starting from the right yes, but the direction of the words is LTR making it unreadable (same as the picture).
However thank you very much, but I used another solution for my case (using pandoc).
Thanks 🌹

@oleibman
Copy link
Contributor

oleibman commented Jan 7, 2024

Glad you found a solution, but I'm not sure why my suggestion isn't working for you. Here's what I see in my Word doc. Is it not correct?
image

@malozaibi
Copy link

Yes it is correct!
If the code still with you paste it here just to make sure it is the same as mine. Sorry for taking your time. Thank you

@oleibman
Copy link
Contributor

oleibman commented Jan 8, 2024

<?php
require __DIR__ . '/PhpWord' . '/vendor/autoload.php';

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\Style;

$phpWord = new PhpWord();
Settings::setDefaultRtl(true);

// Define styles for headers
$phpWord->addTitleStyle(1, array('bold' => true, 'name' => 'Arial', 'size' => 16, 'rtl' => true), array('bidi' => true));
$phpWord->addTitleStyle(2, array('bold' => true, 'name' => 'Arial', 'size' => 14, 'rtl' => true), array('bidi' => true));
$phpWord->addTitleStyle(3, array('bold' => true, 'name' => 'Arial', 'size' => 12, 'rtl' => true), array('bidi' => true));
$phpWord->addTitleStyle(4, array('bold' => true, 'name' => 'Arial', 'size' => 10, 'rtl' => true), array('bidi' => true, 'alignment' => 'start'));
$phpWord->setDefaultParagraphStyle(['bidi' => true, 'alignment' => 'start']);

// Add a section with bidi direction
$section = $phpWord->addSection(['bidi' => true, 'rtl' => true]);
// Your HTML content
$htmlContent = '<h1>مرحبا 1</h1><h2>تجربة 2</h2><h3>تجربة تجربة</h3><h4 dir="rtl">هناك hello هنا 4</h4><p>مرحبا here كلمة انجليزي.</p>';

// Add HTML content to the section
Html::addHtml($section, $htmlContent, false, false);

// Save the file
$filename = 'word.bidi.docx';
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($filename);

echo "saved $filename\n";

@malozaibi
Copy link

Thank you very very much.
The missing line in my code was Settings::setDefaultRtl(true); now everything is in the right place.
I really appreciate it. Thank you.

@oleibman
Copy link
Contributor

oleibman commented Jan 8, 2024

You're welcome. I hope the PR I'm working on will make things a little simpler, but I'm glad you can get it working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants