-
-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathE023_cell_alignment.php
More file actions
412 lines (367 loc) · 12.7 KB
/
Copy pathE023_cell_alignment.php
File metadata and controls
412 lines (367 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
declare(strict_types=1);
/**
* E023_cell_alignment.php
*
* @since 2026-04-26
* @category Library
* @package Pdf
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2002-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf
*
* This file is part of tc-lib-pdf software library.
*/
// NOTE: local file reads (images, fonts, attachments) are restricted to an allowlist of
// trusted paths that covers this package tree, so run the examples in place. To read assets
// from other locations, list them in the 'allowedPaths' entry of the fileOptions constructor
// parameter (see E047_remote_resources_security.php).
// NOTE: run make fonts in the project root to generate the dependencies and example fonts.
// autoloader when using Composer
require __DIR__ . '/../vendor/autoload.php';
// define fonts directory
\define('K_PATH_FONTS', \realpath(__DIR__ . '/../vendor/tecnickcom/tc-lib-pdf-font/target/fonts'));
$pdf = new \Com\Tecnick\Pdf\Tcpdf(
unit: \Com\Tecnick\Pdf\Page\Unit::Millimeter,
isunicode: true,
subsetfont: false,
compress: true,
mode: \Com\Tecnick\Pdf\PdfConformance::None,
objEncrypt: null,
);
$pdf->setCreator('tc-lib-pdf');
$pdf->setAuthor('Nicola Asuni');
$pdf->setSubject('tc-lib-pdf example: 023');
$pdf->setTitle('Cell alignment');
$pdf->setKeywords('TCPDF tc-lib-pdf example cell alignment');
$pdf->setPDFFilename('023_cell_alignment.pdf');
$pdf->setViewerPreferences(['DisplayDocTitle' => true]);
$pdf->enableDefaultPageContent();
$setFont = static function (\Com\Tecnick\Pdf\Tcpdf $pdf, string $family, string $style, int $size): array {
$font = $pdf->font->insert($pdf->pon, $family, $style, $size, 0.0, 1.0);
$pdf->page->addContent($font['out']);
return $font;
};
$getLineStyle = static fn(float $width, string $color): array => [
'lineWidth' => $width,
'lineCap' => 'butt',
'lineJoin' => 'miter',
'dashArray' => [],
'dashPhase' => 0,
'lineColor' => $color,
];
$pdf->font->insert($pdf->pon, 'helvetica', '', 10, 0.0, 1.0);
$pdf->addPage();
$setFont($pdf, 'helvetica', 'B', 20);
$pdf->page->addContent($pdf->getTextCell(
'Cell Alignment Example',
15,
15,
180,
8,
valign: \Com\Tecnick\Pdf\TextVAlign::Center,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
drawcell: false,
));
$bodyFont = $setFont($pdf, 'helvetica', '', 11);
$borderStyle = ['all' => $getLineStyle(0.7, '#0080ff')];
$lineStyle = $getLineStyle(0.1, '#ff0000');
$cellDef = [
'margin' => ['T' => 0.0, 'R' => 0.0, 'B' => 0.0, 'L' => 0.0],
'padding' => ['T' => 0.0, 'R' => 0.0, 'B' => 0.0, 'L' => 0.0],
'borderpos' => 0.0,
];
$cellW = 30.0;
$cellH = $pdf->toUnit($bodyFont['size']) * 3.0;
$startX = 15.0;
$fontHeight = $pdf->toUnit($bodyFont['height']);
$fontAscent = $pdf->toUnit($bodyFont['ascent']);
$columns = [
['prefix' => 'Top', 'cellAlign' => 'T'],
['prefix' => 'Center', 'cellAlign' => 'C'],
['prefix' => 'Bottom', 'cellAlign' => 'B'],
['prefix' => 'Ascent', 'cellAlign' => 'A'],
['prefix' => 'Baseline', 'cellAlign' => 'L'],
['prefix' => 'Descent', 'cellAlign' => 'D'],
];
$rows = [
['lineY' => 60.0, 'suffix' => 'Center', 'textAlign' => 'C'],
['lineY' => 90.0, 'suffix' => 'Top', 'textAlign' => 'T'],
['lineY' => 120.0, 'suffix' => 'Bottom', 'textAlign' => 'B'],
];
$getCellTopY = static fn(
float $lineY,
float $cellH,
string $cellAlign,
string $textAlign,
float $fontHeight,
float $fontAscent,
): float => match ($cellAlign) {
'C' => $lineY - ($cellH / 2.0),
'B' => $lineY - $cellH,
'A' => match ($textAlign) {
'T' => $lineY,
'B' => $lineY - ($cellH - $fontHeight),
default => $lineY - (($cellH - $fontHeight) / 2.0),
},
'L' => match ($textAlign) {
'T' => $lineY - $fontAscent,
'B' => $lineY - ($cellH - ($fontHeight - $fontAscent)),
default => $lineY - ((($cellH - $fontHeight) / 2.0) + $fontAscent),
},
'D' => match ($textAlign) {
'T' => $lineY - $fontHeight,
'B' => $lineY - $cellH,
default => $lineY - (($cellH + $fontHeight) / 2.0),
},
default => $lineY,
};
$drawCellBorder = static fn(
\Com\Tecnick\Pdf\Tcpdf $pdf,
float $x,
float $y,
float $w,
float $h,
array $style,
): string => (
$pdf->graph->getLine($x, $y, $x + $w, $y, $style)
. $pdf->graph->getLine($x + $w, $y, $x + $w, $y + $h, $style)
. $pdf->graph->getLine($x + $w, $y + $h, $x, $y + $h, $style)
. $pdf->graph->getLine($x, $y + $h, $x, $y, $style)
);
foreach ($rows as $row) {
$pdf->page->addContent($pdf->graph->getLine(15, $row['lineY'], 195, $row['lineY'], $lineStyle));
foreach ($columns as $idx => $col) {
$x = $startX + ($idx * $cellW);
$y = $getCellTopY($row['lineY'], $cellH, $col['cellAlign'], $row['textAlign'], $fontHeight, $fontAscent);
$label = $col['prefix'] . '-' . $row['suffix'];
$pdf->page->addContent($pdf->color->getPdfColor('black'));
$pdf->page->addContent($pdf->getTextCell(
$label,
$x,
$y,
$cellW,
$cellH,
valign: $row['textAlign'],
halign: \Com\Tecnick\Pdf\TextHAlign::Center,
cell: $cellDef,
drawcell: false,
));
$pdf->page->addContent($drawCellBorder($pdf, $x, $y, $cellW, $cellH, $borderStyle['all']));
}
}
$imagePath = __DIR__ . '/images/tcpdf_cell.png';
if (\is_file($imagePath)) {
$imgId = $pdf->image->add($imagePath);
$page = $pdf->page->getPage($pdf->page->getPageId());
$pdf->page->addContent($pdf->image->getSetImage($imgId, 15, 160, 100, 100, $page['height']));
}
$legend =
"LEGEND:\n\n"
. "X: cell x top-left origin (top-right for RTL)\n"
. "Y: cell y top-left origin (top-right for RTL)\n"
. "CW: cell width\n"
. "CH: cell height\n"
. "LW: line width\n"
. "NRL: normal line position\n"
. "EXT: external line position\n"
. "INT: internal line position\n"
. "ML: margin left\n"
. "MR: margin right\n"
. "MT: margin top\n"
. "MB: margin bottom\n"
. "PL: padding left\n"
. "PR: padding right\n"
. "PT: padding top\n"
. "PB: padding bottom\n"
. "TW: text width\n"
. "FA: font ascent\n"
. "FB: font baseline\n"
. 'FD: font descent';
$setFont($pdf, 'helvetica', '', 10);
$pdf->page->addContent($pdf->getTextCell($legend, 125, 160, 70, 100, 0, 1.25, 'T', 'L', drawcell: false));
// ---------------------------------------------------------------------------
// Second page: alignment combined with asymmetric paddings.
// ---------------------------------------------------------------------------
$pdf->addPage();
$setFont($pdf, 'helvetica', 'B', 20);
$pdf->page->addContent($pdf->getTextCell(
'Cell Padding and Alignment',
15,
15,
180,
8,
valign: \Com\Tecnick\Pdf\TextVAlign::Center,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
drawcell: false,
));
$setFont($pdf, 'helvetica', '', 9);
$pdf->page->addContent($pdf->getTextCell(
'The text is aligned inside the padding box (dashed grey) and not inside the whole cell (solid blue).'
. ' The margin and padding values of the "cell" parameter are expressed in points,'
. ' so they are converted here with toPoints(); the padding sizes below are in millimeters.',
15,
25,
180,
12,
0,
0.5,
valign: \Com\Tecnick\Pdf\TextVAlign::Top,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
drawcell: false,
));
/**
* Returns a cell definition with the given padding expressed in user units.
*/
$getCellDef = static fn(\Com\Tecnick\Pdf\Tcpdf $pdf, float $top, float $right, float $bottom, float $left): array => [
'margin' => ['T' => 0.0, 'R' => 0.0, 'B' => 0.0, 'L' => 0.0],
// the cell definition stores the padding in points
'padding' => [
'T' => $pdf->toPoints($top),
'R' => $pdf->toPoints($right),
'B' => $pdf->toPoints($bottom),
'L' => $pdf->toPoints($left),
],
'borderpos' => \Com\Tecnick\Pdf\Base::BORDERPOS_DEFAULT,
];
$padStyle = $getLineStyle(0.1, '#808080');
$padStyle['dashArray'] = [1, 1];
$drawPaddingBox = static fn(
\Com\Tecnick\Pdf\Tcpdf $pdf,
float $x,
float $y,
float $w,
float $h,
array $pad,
array $style,
): string => $drawCellBorder(
$pdf,
$x + $pdf->toUnit($pad['L']),
$y + $pdf->toUnit($pad['T']),
$w - $pdf->toUnit($pad['L'] + $pad['R']),
$h - $pdf->toUnit($pad['T'] + $pad['B']),
$style,
);
$vcolumns = [
['label' => 'Top', 'valign' => \Com\Tecnick\Pdf\TextVAlign::Top],
['label' => 'Center', 'valign' => \Com\Tecnick\Pdf\TextVAlign::Center],
['label' => 'Bottom', 'valign' => \Com\Tecnick\Pdf\TextVAlign::Bottom],
['label' => 'Ascent', 'valign' => \Com\Tecnick\Pdf\TextVAlign::Ascent],
['label' => 'Baseline', 'valign' => \Com\Tecnick\Pdf\TextVAlign::Baseline],
['label' => 'Descent', 'valign' => \Com\Tecnick\Pdf\TextVAlign::Descent],
];
$vpaddings = [
['label' => 'vertical padding: T=0 B=0', 'T' => 0.0, 'B' => 0.0],
['label' => 'vertical padding: T=10 B=0', 'T' => 10.0, 'B' => 0.0],
['label' => 'vertical padding: T=0 B=10', 'T' => 0.0, 'B' => 10.0],
['label' => 'vertical padding: T=12 B=4', 'T' => 12.0, 'B' => 4.0],
];
$vcellW = 180.0 / \count($vcolumns);
$vcellH = 24.0;
$vrowPitch = 32.0;
$vtop = 48.0;
$setFont($pdf, 'helvetica', 'B', 11);
$pdf->page->addContent($pdf->getTextCell(
'Vertical alignment (valign)',
15,
40,
180,
5,
valign: \Com\Tecnick\Pdf\TextVAlign::Top,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
drawcell: false,
));
foreach ($vpaddings as $vrow => $vpad) {
$rowY = $vtop + ($vrow * $vrowPitch);
$cellY = $rowY + 6.0;
$cellDef = $getCellDef($pdf, $vpad['T'], 0.0, $vpad['B'], 0.0);
$setFont($pdf, 'helvetica', 'I', 8);
$pdf->page->addContent($pdf->getTextCell(
$vpad['label'],
15,
$rowY,
180,
5,
valign: \Com\Tecnick\Pdf\TextVAlign::Top,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
drawcell: false,
));
foreach ($vcolumns as $idx => $col) {
$x = $startX + ($idx * $vcellW);
$setFont($pdf, 'helvetica', '', 9);
$pdf->page->addContent($pdf->getTextCell(
$col['label'],
$x,
$cellY,
$vcellW,
$vcellH,
valign: $col['valign'],
halign: \Com\Tecnick\Pdf\TextHAlign::Center,
cell: $cellDef,
drawcell: false,
));
$pdf->page->addContent($drawCellBorder($pdf, $x, $cellY, $vcellW, $vcellH, $borderStyle['all']));
$pdf->page->addContent($drawPaddingBox($pdf, $x, $cellY, $vcellW, $vcellH, $cellDef['padding'], $padStyle));
}
}
$hcolumns = [
['label' => 'Left', 'halign' => \Com\Tecnick\Pdf\TextHAlign::Left],
['label' => 'Center', 'halign' => \Com\Tecnick\Pdf\TextHAlign::Center],
['label' => 'Right', 'halign' => \Com\Tecnick\Pdf\TextHAlign::Right],
];
$hpaddings = [
['label' => 'horizontal padding: L=0 R=0', 'L' => 0.0, 'R' => 0.0],
['label' => 'horizontal padding: L=15 R=0', 'L' => 15.0, 'R' => 0.0],
['label' => 'horizontal padding: L=0 R=15', 'L' => 0.0, 'R' => 15.0],
];
$hcellW = 180.0 / \count($hcolumns);
$hcellH = 12.0;
$hrowPitch = 20.0;
$htop = 190.0;
$setFont($pdf, 'helvetica', 'B', 11);
$pdf->page->addContent($pdf->getTextCell(
'Horizontal alignment (halign)',
15,
182,
180,
5,
valign: \Com\Tecnick\Pdf\TextVAlign::Top,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
drawcell: false,
));
foreach ($hpaddings as $hrow => $hpad) {
$rowY = $htop + ($hrow * $hrowPitch);
$cellY = $rowY + 6.0;
$cellDef = $getCellDef($pdf, 0.0, $hpad['R'], 0.0, $hpad['L']);
$setFont($pdf, 'helvetica', 'I', 8);
$pdf->page->addContent($pdf->getTextCell(
$hpad['label'],
15,
$rowY,
180,
5,
valign: \Com\Tecnick\Pdf\TextVAlign::Top,
halign: \Com\Tecnick\Pdf\TextHAlign::Left,
drawcell: false,
));
foreach ($hcolumns as $idx => $col) {
$x = $startX + ($idx * $hcellW);
$setFont($pdf, 'helvetica', '', 9);
$pdf->page->addContent($pdf->getTextCell(
$col['label'],
$x,
$cellY,
$hcellW,
$hcellH,
valign: \Com\Tecnick\Pdf\TextVAlign::Center,
halign: $col['halign'],
cell: $cellDef,
drawcell: false,
));
$pdf->page->addContent($drawCellBorder($pdf, $x, $cellY, $hcellW, $hcellH, $borderStyle['all']));
$pdf->page->addContent($drawPaddingBox($pdf, $x, $cellY, $hcellW, $hcellH, $cellDef['padding'], $padStyle));
}
}
$rawpdf = $pdf->getOutPDFString();
$pdf->renderPDF(rawpdf: $rawpdf);