Skip to content

Graphics

Dragon edited this page Jun 3, 2026 · 1 revision

Graphics

Page exposes a set of drawing primitives for vector shapes, graphics state (color, line width, dash pattern, cap and join style), coordinate transforms, and the graphics-state save/restore stack. All coordinates and sizes are in the document's unit (millimetres by default).

Drawing primitives

line(), rect(), and circle() return a PathOperation that you finish with ->stroke(), ->fill(), or ->strokeAndFill().

use DragonOfMercy\PhpPdf\Color;

$page = $pdf->addPage();

// Stroked rectangle.
$page->setStrokeColor(Color::hex('#ff0000'))
     ->setLineWidth(0.5)
     ->rect(20, 20, 80, 40)
     ->stroke();

// Filled circle.
$page->setFillColor(Color::rgb(0, 0, 255))
     ->circle(105, 150, 20)
     ->fill();

// Stroked and filled simultaneously.
$page->setStrokeColor(Color::hex('#000000'))
     ->setFillColor(Color::gray(200))
     ->rect(20, 70, 40, 20)
     ->strokeAndFill();

// Line segment.
$page->setStrokeColor(Color::gray(0))
     ->setLineWidth(0.3)
     ->line(20, 100, 190, 100)
     ->stroke();

Primitive methods

Method Description
rect(float $x, float $y, float $w, float $h): PathOperation Axis-aligned rectangle. (x, y) is the top-left corner.
circle(float $cx, float $cy, float $r): PathOperation Circle with centre (cx, cy) and radius r.
line(float $x1, float $y1, float $x2, float $y2): PathOperation Straight line segment.
path(): Path Open a free-form path builder for arbitrary move/line/curve/close sequences.

PathOperation methods: ->stroke(), ->fill(), ->strokeAndFill().

Color

Color is an immutable value object. All three factories accept values in the range 0-255 (integers) or a hex string.

Factory Description
Color::rgb(int $r, int $g, int $b) Full-color RGB. Each component 0-255.
Color::hex(string $hex) Hex string: '#rrggbb', 'rrggbb', '#rgb', or 'rgb'.
Color::gray(int $level) Grayscale. 0 = black, 255 = white. Emits a DeviceGray operator.

Graphics state

These methods return $this (the Page) so they can be chained before a primitive call.

Method Description
setStrokeColor(Color $color): self Stroke (outline) color for subsequent paths.
setFillColor(Color $color): self Fill color for subsequent paths.
setLineWidth(float $width): self Line width in the document unit.
setDashPattern(array $pattern, float $phase = 0.0): self Dash/gap alternating lengths in the document unit. Pass [] to reset to solid.
setLineCap(LineCap $cap): self Line cap style (LineCap::BUTT, ROUND, SQUARE).
setLineJoin(LineJoin $join): self Line join style (LineJoin::MITER, ROUND, BEVEL).

Dashed lines example

use DragonOfMercy\PhpPdf\Color;
use DragonOfMercy\PhpPdf\LineCap;

$page->setStrokeColor(Color::gray(100))
     ->setLineWidth(0.4)
     ->setDashPattern([3, 1.5])   // 3 mm dash, 1.5 mm gap
     ->line(20, 50, 190, 50)
     ->stroke();

$page->setDashPattern([]);        // reset to solid

Transforms

Transforms apply a PDF cm (concatenate matrix) operator and affect everything drawn after the call until the matching restore(). They return $this.

Method Description
translate(float $x, float $y): self Move the origin by (x, y) in the document unit.
rotate(float $degrees): self Rotate the coordinate system counter-clockwise by the given angle (degrees).
scale(float $sx, float $sy): self Scale the coordinate system by (sx, sy) (dimensionless factors).

Transforms compose left-to-right: translate then rotate pivots around the translated origin.

Graphics-state save and restore

save() pushes the current graphics state (colors, line width, dash, cap, join, CTM) onto the PDF graphics stack. restore() pops it. Both return $this. Always pair them to avoid corrupting the state of subsequent drawing operations.

$page->save()
     ->setFillColor(Color::rgb(255, 200, 0))
     ->translate(50, 50)
     ->rotate(45)
     ->rect(0, 0, 20, 20)
     ->fill()
     ->restore();

// State is now exactly what it was before save().

See also

Clone this wiki locally