Transforms (HasTransforms)
The HasTransforms trait provides geometric transformations that modify how subsequent drawing operations are rendered. Transformations operate on the Current Transformation Matrix (CTM) and must be wrapped in startTransform() / stopTransform() pairs to isolate their effects.
All methods return static, so every call can be chained.
Quick Reference
| Method | Effect |
|---|---|
startTransform() | Save graphics state (push CTM) |
stopTransform() | Restore graphics state (pop CTM) |
rotate() | Rotate around a point |
scale() | Scale horizontally and vertically |
translate() | Move the coordinate origin |
skew() | Shear along X and Y axes |
mirrorH() | Mirror horizontally |
mirrorV() | Mirror vertically |
mirrorP() | Mirror about a point |
mirrorL() | Mirror about an arbitrary line |
Basic Example
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
// Rotated text
->startTransform()
->rotate(45, 105, 60)
->text(100, 55, 'Rotated 45°')
->stopTransform()
// Scaled rectangle
->startTransform()
->scale(150, 150, 50, 150)
->rect(40, 140, 20, 20, 'DF')
->stopTransform()
// Mirrored text
->startTransform()
->mirrorH(105)
->text(100, 200, 'Mirrored')
->stopTransform();WARNING
Always wrap transformations in startTransform() / stopTransform() pairs. Forgetting stopTransform() leaves the graphics state modified for all subsequent operations.
startTransform / stopTransform
$pdf->startTransform(); // Push current graphics state
// ... drawing operations with transforms ...
$pdf->stopTransform(); // Pop and restore previous stateThese calls can be nested. Each startTransform() pushes a new state onto the stack.
rotate()
$pdf->rotate(float $angle, float $x = '', float $y = '');Rotates subsequent content by $angle degrees counter-clockwise around point (x, y). If x and y are omitted, the current position is used.
scale()
$pdf->scale(float $sx, float $sy, float $x = '', float $y = '');Scales content by $sx percent horizontally and $sy percent vertically, relative to point (x, y). A value of 100 means no change; 200 doubles the size.
translate()
$pdf->translate(float $tx, float $ty);Shifts the coordinate origin by $tx horizontally and $ty vertically. All subsequent coordinates are offset by this amount until the transform is stopped.
skew()
$pdf->skew(float $angleX, float $angleY, float $x = '', float $y = '');Applies a shear transformation. $angleX skews along the X axis and $angleY along the Y axis, both in degrees.
Mirror Methods
$pdf->mirrorH(float $x = ''); // Horizontal mirror about vertical axis at x
$pdf->mirrorV(float $y = ''); // Vertical mirror about horizontal axis at y
$pdf->mirrorP(float $x = '', float $y = ''); // Mirror about a point
$pdf->mirrorL(float $angle, float $x = '', float $y = ''); // Mirror about a line through (x, y) at angle$pdf->startTransform()
->mirrorH(105)
->setFont('Helvetica', '', 14)
->text(80, 50, 'Flipped horizontally')
->stopTransform();Combining Transforms
Multiple transforms can be stacked within a single block. They are applied in the order called.
$pdf->startTransform()
->translate(20, 20)
->rotate(30)
->scale(120, 120)
->rect(10, 10, 40, 20, 'DF')
->stopTransform();Full CTM Support
For advanced use cases, the underlying Current Transformation Matrix is fully accessible. The six-element matrix [a, b, c, d, e, f] maps to the standard PDF transformation matrix, giving complete control over affine transformations when the convenience methods are not sufficient.