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!
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:
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);
dir="rtl"
or direction: rtl;
:
dir="rtl"
attribute in the HTML or the direction: rtl;
CSS property to enable right-to-left text alignment.No Manual Adjustment of $x_in_mm
:
$x_in_mm
and $width_in_mm
.Width in mm:
$width_in_mm
parameter is sufficient to accommodate the text. mPDF uses this width to determine the bounding box for text rendering.Font Size Consistency:
font-size
CSS property should match the $font_size
used in your PDF generation logic.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;
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.