-1

I'm working on TCPDF. I have a 3 lines RTL text when I use justify in MultiCell, the last line starts from the left (it supposed to start from right).

$txt = 'السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكمالسلام عليكمالسلام عليكم
';

$pdf->MultiCell(170, 0, $txt."\n", 0, 'J', false, 1);

When I tried using WriteHTML, it is the same thing

$htmlpersian = '<p align="justify" dir="rtl" >
السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكم السلام عليكمالسلام عليكمالسلام عليكم   
</p>';
$pdf->WriteHTML($htmlpersian."\n", true, 0, true, 0);

"\n" is added to the string to prevent justifying the last line

output

A.K.
  • 141
  • 1
  • 2
  • 14

1 Answers1

0

Here is a work around I came up with but it will require additional sensitivity variable to be adjusted manually with different texts which is basically to write the last line in a separate cell justified to the right.

$textexploded   =   explode(" ",$lettertext);                   // create array of words
$lettercount    =   mb_strlen($lettertext,'UTF-8');             // Get total number of characters of the whole text
$linesnum       =   $lettercount / $linechar;                    // divide number of characters over sensitivity number "how many letters per line"
$whole          =   floor($linesnum);                       // before decimal       
$fraction       =   $linesnum - $whole;                      // decimal
$last           =   $fraction * $linechar;                   // estimate number of characters of last line 
$last           =   $last - 3;                                  // refining

if (    $lettercount >= $linechar   ) {                     // Proceed of text characters are more than one line limit
    $total = 0;
    for ($x = count($textexploded); $total < $last; $x--) {   // Start at the end of the words array
        $total += mb_strlen($textexploded[$x-1],'UTF-8');    // Estimate at what array word last line starts
    }

    $slice_position = array_search($x, array_keys($textexploded));  // determine the slice position of the original text 
    $a1 = array_slice($textexploded, 0, $slice_position, true);     // Slice the first lines 
    $a2 = array_slice($textexploded, $slice_position, count($textexploded), true);  //slice the last line
    $a1 = implode(" ",$a1);                                         // recombine words 
    $a2 = implode(" ",$a2);                                         // recombine words 

    $pdf->MultiCell(190, 0, '       '.$a1, 0, 'J', 0, 1, '', '', false, 2, false, true, 0, 'M');
    $pdf->Cell(0, 6, $a2, 0, 0, 'R', 0, '', 0, false, 'T', 'M');
} else {
    $pdf->Cell(0, 6, $lettertext, 0, 0, 'R', 0, '', 0, false, 'T', 'M');
}
A.K.
  • 141
  • 1
  • 2
  • 14