user29027808
user29027808

Reputation: 13

Using PHP MPDF, I need to write Hebrew or Arabic text to specific location in PDF

I am creating a functionality for signing PDF files with dynamic data. I use screenshot of PDF where user can click on specific areas of it and then (by coordinates of a click) automatically populate PDF.

I am basically converting pct of X,Y coordinates to PDF, writing on PDF via WriteFixedPosHTML and that's working correctly.

Issue is that I need to write from right to left.

I am using $mpdf->WriteFixedPosHTML($content, $x_in_mm - $content_length, $y_in_mm, $content_length, $font_size);

and

$content_length = $content_length * 0.2645833333;

I believe issue comes that $content_length is in pixels and I need to convert to mm (so it is not fully precise).

Is there a way that I write RTL without actually doing $x_in_mm - $content_length

Thank you in advance!

Upvotes: 1

Views: 81

Answers (1)

Nikunja
Nikunja

Reputation: 26

Yes, you can achieve writing text from right to left (RTL) in mPDF without manually subtracting $content_length from $x_in_mm. mPDF supports RTL text alignment natively. Here’s how you can implement this:

Use mPDF's RTL Support

mPDF supports the dir="rtl" attribute in HTML or the CSS direction property. When using WriteFixedPosHTML, you can directly use this feature to align text from right to left. Here's an example:

$content = '<div dir="rtl" style="font-size:' . $font_size . 'mm;">Your RTL Content Here</div>';
$mpdf->WriteFixedPosHTML($content, $x_in_mm, $y_in_mm, $width_in_mm, $font_size);

Key Points:

  1. dir="rtl" or direction: rtl;:

    • Use the dir="rtl" attribute in the HTML or the direction: rtl; CSS property to enable right-to-left text alignment.
  2. No Manual Adjustment of $x_in_mm:

    • When you use RTL alignment, mPDF will handle the positioning automatically, starting the text from the right side of the container defined by $x_in_mm and $width_in_mm.
  3. Width in mm:

    • Ensure that the $width_in_mm parameter is sufficient to accommodate the text. mPDF uses this width to determine the bounding box for text rendering.
  4. Font Size Consistency:

    • The font-size CSS property should match the $font_size used in your PDF generation logic.

Example with Conversion

If you still want to calculate $content_length accurately for other purposes, you can convert from pixels to millimeters using the following formula:

// Assuming 96 DPI (typical screen resolution):
$pixels_to_mm_conversion = 25.4 / 96;
$content_length_in_mm = $content_length_in_pixels * $pixels_to_mm_conversion;

Alternative: CSS Text Alignment

If you're not constrained to WriteFixedPosHTML, you can use regular WriteHTML with CSS for better layout control:

$content = '<div style="text-align: right; direction: rtl; font-size:' . $font_size . 'mm;">Your RTL Content Here</div>';
$mpdf->WriteHTML($content);

This approach avoids manually setting positions entirely and lets mPDF handle the alignment for you.

Upvotes: 1

Related Questions