Skip to content

Graphics (HasDrawing)

The HasDrawing trait provides vector drawing primitives for lines, rectangles, circles, polygons, curves, arrows, and pie sectors. All methods return static, so every call can be chained.

Quick Reference

MethodShape
line()Straight line between two points
rect()Rectangle
roundedRect()Rectangle with rounded corners
circle()Circle
ellipse()Ellipse
polygon()Arbitrary polygon from point array
regularPolygon()Regular polygon (n sides)
starPolygon()Star shape
arrow()Arrow with head
pieSector()Pie sector for charts
curve()Cubic Bezier curve
polyCurve()Multi-segment Bezier curve

Basic Example

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setDrawColor(255, 0, 0)
    ->setFillColor(200, 220, 255)
    ->line(10, 10, 100, 10)
    ->rect(10, 20, 80, 40, 'DF')
    ->roundedRect(10, 70, 80, 40, 5, 'DF')
    ->circle(150, 40, 30, 'DF')
    ->ellipse(150, 100, 40, 20, 'DF')
    ->arrow(10, 140, 100, 140)
    ->regularPolygon(150, 160, 25, 6, 'DF')
    ->starPolygon(50, 200, 25, 5, 3, 'DF');

Style Parameter

Most drawing methods accept a $style string that controls rendering:

ValueMeaning
SStroke (outline only) — default
FFill only
DF or BDraw and fill (both)

Lines and Rectangles

php
$pdf->line(float $x1, float $y1, float $x2, float $y2);
$pdf->rect(float $x, float $y, float $w, float $h, string $style = '');
$pdf->roundedRect(float $x, float $y, float $w, float $h, float $r, string $style = '');

line() draws from point (x1, y1) to (x2, y2). rect() draws a standard rectangle. roundedRect() adds rounded corners with radius $r.

Circles and Ellipses

php
$pdf->circle(float $x0, float $y0, float $r, string $style = '');
$pdf->ellipse(float $x0, float $y0, float $rx, float $ry, string $style = '');

circle() takes a center point and radius. ellipse() uses separate horizontal and vertical radii.

Polygons

php
$pdf->polygon(array $points, string $style = '');
$pdf->regularPolygon(float $x0, float $y0, float $r, int $ns, string $style = '');
$pdf->starPolygon(float $x0, float $y0, float $r, int $nv, int $ng, string $style = '');

polygon() accepts a flat array of coordinates [x1, y1, x2, y2, ...]. regularPolygon() draws an n-sided polygon inscribed in a circle of radius $r. starPolygon() draws a star with $nv vertices and gap factor $ng.

Arrows and Pie Sectors

php
$pdf->arrow(float $x0, float $y0, float $x1, float $y1);
$pdf->pieSector(float $xc, float $yc, float $r, float $a, float $b, string $style = '');

arrow() draws a line with an arrowhead at the destination. pieSector() draws a pie sector from angle $a to $b (degrees), useful for charts:

php
$pdf->setFillColor(255, 100, 100)->pieSector(100, 100, 40, 0, 120, 'F')
    ->setFillColor(100, 255, 100)->pieSector(100, 100, 40, 120, 250, 'F')
    ->setFillColor(100, 100, 255)->pieSector(100, 100, 40, 250, 360, 'F');

Bezier Curves

php
$pdf->curve(float $x0, float $y0, float $x1, float $y1, float $x2, float $y2, float $x3, float $y3);
$pdf->polyCurve(array $points);

curve() draws a cubic Bezier from (x0, y0) to (x3, y3) with control points (x1, y1) and (x2, y2). polyCurve() chains multiple Bezier segments from a point array.

Line Styles

php
$pdf->setLineStyle(array $style);

The $style array supports keys: width (float), cap (butt, round, square), join (miter, round, bevel), dash (string or array pattern), and color (RGB array).

php
$pdf->setLineStyle([
    'width' => 0.5,
    'cap'   => 'round',
    'join'  => 'round',
    'dash'  => '3,2',
    'color' => [0, 0, 200],
])->line(10, 10, 190, 10);

Crop and Registration Marks

Add print-ready marks for professional output:

php
$pdf->cropMark(20, 20, 10, 10)
    ->registrationMark(105, 10)
    ->colorRegistrationBar(20, 280, 170, 5);

Released under the LGPL-3.0-or-later License.