Daniel Katzan
Daniel Katzan

Reputation: 564

mixing rtl with ltr in tcpdf

I'm using tcpdf to create pdf files in my webapp.

I'm using the writeHtml() function of tcpdf and trying to mix some hebrew with english,

when trying to combine both of the languages in the same line, the output comes out in the wrong direction.

for example - this is a piece of my code:

    $pdf = new TCPDF ( PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false );
    $pdf->SetDisplayMode ( "default", "OneColumn" );
    $pdf->setRTL ( false );
    $pdf->SetFont ( 'freeserif', '', 10, '' );


    $html = '<body  style="text-align: center">';
    $html .= "<p> some words in english ואחכ כמה מילים בעברית </p>";
    $html .= "<p> כמה מילים כתובות בעברית and then some words in english</p>";
    $html .= '</body>';
    $pdf->AddPage ();

    $pdf->writeHTML ( $comments_table, true, false, true, false, 'R' );
    $pdf->Output ( $path, 'F' );

the expected output will be:

some words in english ואחכ כמה מילים בעברית

כמה מילים כתובות בעברית and then some words in english.

but the words in the second language in every language come in the oppsite order my output is:

some words in english בעברית מילים כמה ואחכ

כמה מילים כתובות בעברית english in words some then and

as you can see - in the first line the english is ok - and the hebrew not, in the second line the hebrew is ok and the english isnt

Upvotes: 2

Views: 2014

Answers (5)

Eng.MoaathHomaid
Eng.MoaathHomaid

Reputation: 1

To make output showing correctly with RTL or LTR write directions, simply you can check from your solution the current direction and store it in $direction variable, then check it if is RTL or not. add this to your code :

$direction = $user->get_config("direction");
$style = "";

if ($direction == "rtl"){
        $style = "
                 <style> h1,h2,h3,h4,h5,div,p,table,thead,tr,td {
                         direction:rtl !important; text-align: right !important;}
                 </style>";
    }

Then Add this before you put anything in $html variable

$html = $style . "<HTML CODE>";

Finally Print your output :

$pdf->writeHTML($html, true, false, false, false, '');

Upvotes: 0

Atara
Atara

Reputation: 3569

use setRTL():

...
$pdf->setRTL(true);
$pdf->writeHTML($html, true, false, true, false, '');

Upvotes: 0

Daniel Katzan
Daniel Katzan

Reputation: 564

I switched from tcpdf to mpdf which has a built in support for Bidirectional languages

Upvotes: 1

Chester M
Chester M

Reputation: 366

According to this tutorial from w3.org about bidirectional text:

For inline text, tightly wrap all opposite-direction phrases in markup that sets their base direction.

So your code should be something like this:

$html = '<body style="text-align: center">
            <span> some words in english<span>
            <span>ואחכ כמה מילים בעברית <br> כמה מילים כתובות בעברית<span>
            <span>and then some words in english</span>
        </body>';

$pdf->AddPage();

$pdf->writeHTML( $html, true, false, true, false, '' );

Upvotes: 2

JamesG
JamesG

Reputation: 4309

Try wrapping the Hebrew chunks in span tags with dir="rtl". I know that would work in a browser, just not sure about TCPDF. You may also be able to include entire sentences that have mixed English and Hebrew within those span tags.

Upvotes: -1

Related Questions