图形 (HasDrawing)
HasDrawing trait 提供矢量绘图基元,涵盖直线、矩形、圆形、多边形、曲线、箭头及扇形。所有方法都返回 static,因此每个调用都可以链式串接。
速查表
| 方法 | 图形 |
|---|---|
line() | 两点之间的直线 |
rect() | 矩形 |
roundedRect() | 圆角矩形 |
circle() | 圆形 |
ellipse() | 椭圆 |
polygon() | 任意多边形(顶点数组) |
regularPolygon() | 正多边形(n 边) |
starPolygon() | 星形 |
arrow() | 带箭头的线段 |
pieSector() | 扇形(用于图表) |
curve() | 三次贝塞尔曲线 |
polyCurve() | 多段贝塞尔曲线 |
基本示例
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 字符串,用于控制渲染方式:
| 值 | 含义 |
|---|---|
S | 仅描边(轮廓) — 默认值 |
F | 仅填充 |
DF 或 B | 描边加填充(两者皆有) |
直线与矩形
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() 从 (x1, y1) 画到 (x2, y2)。rect() 绘制标准矩形。roundedRect() 增加半径为 $r 的圆角。
圆形与椭圆
php
$pdf->circle(float $x0, float $y0, float $r, string $style = '');
$pdf->ellipse(float $x0, float $y0, float $rx, float $ry, string $style = '');circle() 接受圆心与半径。ellipse() 则使用水平与垂直两个独立的半径。
多边形
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() 接受一个扁平的坐标数组 [x1, y1, x2, y2, ...]。regularPolygon() 绘制内接于半径 $r 圆形的 n 边正多边形。starPolygon() 绘制具有 $nv 个顶点及间距因子 $ng 的星形。
php
// 三角形
$pdf->polygon(
points: [50, 10, 10, 60, 90, 60],
style: 'DF'
);
// 正六边形
$pdf->regularPolygon(
x0: 60, y0: 80, r: 25,
ns: 6, style: 'DF'
);
// 五角星
$pdf->starPolygon(
x0: 150, y0: 80, r: 25,
nv: 5, ng: 3, style: 'DF'
);箭头与扇形
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() 绘制一条在终点带有箭头的线段。pieSector() 从角度 $a 到 $b(度数)绘制扇形,非常适合用于饼图:
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');贝塞尔曲线
php
$pdf->curve(float $x0, float $y0, float $x1, float $y1, float $x2, float $y2, float $x3, float $y3);
$pdf->polyCurve(array $points);curve() 从 (x0, y0) 到 (x3, y3) 绘制三次贝塞尔曲线,(x1, y1) 和 (x2, y2) 为控制点。polyCurve() 从坐标数组串接多段贝塞尔曲线。
php
// 三次贝塞尔曲线
$pdf->curve(
20, 200, // 起点
80, 150, // 控制点 1
120, 250, // 控制点 2
180, 200 // 终点
);线条样式
php
$pdf->setLineStyle(array $style);$style 数组支持以下键值:width(浮点数)、cap(butt、round、square)、join(miter、round、bevel)、dash(字符串或数组模式)以及 color(RGB 数组)。
php
$pdf->setLineStyle([
'width' => 0.5,
'cap' => 'round',
'join' => 'round',
'dash' => '3,2',
'color' => [0, 0, 200],
])->line(10, 10, 190, 10);线帽样式
| 值 | 说明 |
|---|---|
butt | 平切 — 线条在端点处截断(默认) |
round | 圆形 — 在端点加上半圆形突出 |
square | 方形 — 在端点加上半方形突出 |
线接样式
| 值 | 说明 |
|---|---|
miter | 尖角 — 线段延伸至交汇点(默认) |
round | 圆角 — 在接合处加上圆弧 |
bevel | 斜角 — 在接合处截平 |
裁切标记与对位标记
在专业印刷输出中加入对位标记与裁切标记:
php
$pdf->cropMark(20, 20, 10, 10)
->registrationMark(105, 10)
->colorRegistrationBar(20, 280, 170, 5);cropMark()— 裁切标记,标示纸张的裁切位置。registrationMark()— 对位标记(十字标记),用于多色套印对位。colorRegistrationBar()— 色条,用于检查印刷颜色的准确性。