php - TCPDF - Text Rotation Problem (wrong AX, AY)
Get the solution ↓↓↓I've got a problem with rotating text on PDF. My form before any rotation looks like this:
The positions seems to be correct, when I set rotation to 0. But the rotation may vary from the item and can be anything.
Rotation from the data is, that LEFT has -90 degrees, right +90 degrees and bottom +180 degrees. When I set it to it, the only correct text is TOP (because rotation is 0). The rest of text items are totally wrong, some even not visible.
I've managed to do the rotation with images, example:
The code I've used for image rotation is:
$pdf->StartTransform();
$pdf->Rotate(
-$item['rotation'],
$imagePosition['x'] + $imageSize['x'] / 2,
$imagePosition['y'] + $imageSize['y'] / 2);
$pdf->Image(
$filename,
$imagePosition['x'],
$imagePosition['y'],
$imageSize['x'],
$imageSize['y'],
strtoupper($imagick->getImageFormat()),
'',
'',
false,
(int)$dpi,
'',
false,
false,
0,
false,
false,
false
);
$pdf->StopTransform();
For image it's easy, because I have position and size.
How to do the propper Text rotation? I was trying to write that text in Imagick and from there get text width and height and try to do the same like for the image, but without any success.
My code for text rotation:
$pdf->StartTransform();
$pdf->Rotate(
-$item['rotation'],
$fontPosition['x'] - $textSize['x'] / 2,
$fontPosition['y'] - $textSize['y'] / 2
);
list($r, $g, $b) = sscanf($item['attributes']['color'], "#%02x%02x%02x");
$pdf->SetTextColor($r, $g, $b);
$pdf->SetFont($fontName, '', $fontSize, '', false);
$pdf->Text($fontPosition['x'], $fontPosition['y'], $item['value'], 0, 0);
$pdf->StopTransform();
Note that Text size variable is probably not correct.
Any ideas how to write image rotated to PDF without so many complications in TCPDF?
For FPDF I found extension: http://www.fpdf.org/en/script/script31.php
When I've tried to implement it to TCPDF, then the text was squares, instead of the real content.
PLZ HELP :D
Answer
Solution:
You need to keep track of the current "origin" of the coordinate system, where 0,0 is after the current transformation.
You also need to keep in mind that transformations are combined, and earlier transformations affect later ones.
If you rotate 90 degrees clockwise, then move in +Y (up), you actually move in +X (right)... because the entire coordinate system has rotated.
If on the other hand, you moved (translated), then rotated, your new origin around which you would rotate would be wherever it was you wanted +Y.
So if you do your transformations in the correct order, and know which direction things are drawn, you can place things wherever you want.
If you rotate an untranslated image 90 degrees clockwise, it's rotated around the origin (0,0) and therefore disappears. On the other hand, if you move the origin to take the image's movement around the origin into account, it'll land right where you wanted.
One of the tricks I used when messing around with PDF transformation features/bugs was\s to expand the page by a couple thousand units in every direction. IIRC, an 8.5"x11" page has a media box of [0 0 612 792] (wow, I actually remembered that correctly: go me). If you set the media box to [-2000 -2000 2612 2792], then you have to work hard to screw up your transformation so badly that you can't see where your text/image/whatever landed (though it can, and has, been done).
Note that if your page also has a crop box, you have to mess with it too.
Answer
Solution:
Ok, I've sort it out. Working code for text rotation should be like this:
$pdf->StartTransform();
$pdf->Rotate(
-$item['rotation'],
$fontPosition['x'] + $textSize['x'] / 2,
$fontPosition['y'] + $textSize['y'] / 2
);
list($r, $g, $b) = sscanf($item['attributes']['color'], "#%02x%02x%02x");
$pdf->SetTextColor($r, $g, $b);
$pdf->SetFont($fontName, '', $fontSize, '', false);
$pdf->Text($fontPosition['x'], $fontPosition['y'], $item['value'], 0, 0);
$pdf->StopTransform();
Wrong was:
$fontPosition['x'] - $textSize['x'] / 2,
$fontPosition['y'] - $textSize['y'] / 2
Should be:
$fontPosition['x'] + $textSize['x'] / 2,
$fontPosition['y'] + $textSize['y'] / 2
I've tried this combination before, but my textSize wasn't correct (I was calculating it with imagick). In my case, I've counted percentage of our draw area that text is taking and after that I correctly counted it in mm.
Hope it helps someone else.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: port 80 in use by "unable to open process" with pid 4!
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.