-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Comments
$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 |
In the sample which you referenced, the input html specifies, among other things:
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 ? |
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> الألم الذي ربما تنجم عنه بعض ا.‏</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. |
this seems to be not working , my texts seem to be LTR , is there anything i can do with the ::html function |
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> الألم الذي ربما تنجم عنه بعض ا.‏</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 |
@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 👏 |
If you may, can you give me an example, I tried the above this message and it make it rtl for 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: |
@malozaibi My change was incorporated in PhpWord 1.2. Can you upload the file you are having a problem with? |
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 $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]); |
Part of the problem is, of course, that I should have specified |
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. |
$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 |
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). |
Yes it is correct! |
<?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"; |
Thank you very very much. |
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. |
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](https://private-user-images.githubusercontent.com/74759660/255425444-c1656308-798c-460b-868a-78ab75ff998e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg5NDM2MjQsIm5iZiI6MTcxODk0MzMyNCwicGF0aCI6Ii83NDc1OTY2MC8yNTU0MjU0NDQtYzE2NTYzMDgtNzk4Yy00NjBiLTg2OGEtNzhhYjc1ZmY5OThlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA2MjElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNjIxVDA0MTUyNFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTk2NGFkMTkxYTFmZjI0MTdiZGRkNzAzOTQzNWQwNWQ5YTYzNzc3YjM3YWE0OGNjM2U5NTNhMTgxYjU2MmNlZTUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.OHhKFsUEUJxrVKsNGfcrPT8-7jnyfRqGCl57MagrT0M)
The text was updated successfully, but these errors were encountered: