-
Notifications
You must be signed in to change notification settings - Fork 6
/
PdfLabel.php
453 lines (422 loc) · 12.3 KB
/
PdfLabel.php
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<?php
namespace Uskur\PdfLabel;
/**
* TCPDF Class to print labels in Avery or custom formats
* Based on work of Laurent PASSEBECQ <[email protected]>
*
* @author Burak USGURLU <[email protected]>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
class PdfLabel extends \TCPDF
{
/**
* Left margin of labels
*
* @var float
*/
protected $marginLeft;
/**
* Top margin of labels
*
* @var float
*/
protected $marginTop;
/**
* Horizontal space between 2 labels
*
* @var float
*/
protected $xSpace;
/**
* Vertical space between 2 labels
*
* @var float
*/
protected $ySpace;
/**
* Number of labels horizontally
*
* @var integer
*/
protected $xNumber;
/**
* Number of labels vertically
*
* @var integer
*/
protected $yNumber;
/**
* Width of label
*
* @var float
*/
protected $labelWidth;
/**
* Height of label
*
* @var float
*/
protected $labelHeight;
/**
* Padding
*
* @var float
*/
protected $labelPadding;
/**
* Type of unit for the document
*
* @var string
*/
protected $sheetUnit;
/**
* Current x position
*
* @var integer
*/
protected $xPosition;
/**
* Current y position
*
* @var integer
*/
protected $yPosition;
/**
* Cut lines enabled?
*
* @var boolean
*/
protected $cutLines;
/**
* List of label formats
*
* @var array
*/
const LABELS = array(
'5160' => array(
'paper-size' => 'LETTER',
'unit' => 'mm',
'marginLeft' => 4.7625,
'marginTop' => 12.7,
'NX' => 3,
'NY' => 10,
'SpaceX' => 3.175,
'SpaceY' => 0,
'width' => 66.675,
'height' => 25.4
),
'5161' => array(
'paper-size' => 'LETTER',
'unit' => 'mm',
'marginLeft' => 0.967,
'marginTop' => 10.7,
'NX' => 2,
'NY' => 10,
'SpaceX' => 3.967,
'SpaceY' => 0,
'width' => 101.6,
'height' => 25.4
),
'5162' => array(
'paper-size' => 'LETTER',
'unit' => 'mm',
'marginLeft' => 0.97,
'marginTop' => 20.224,
'NX' => 2,
'NY' => 7,
'SpaceX' => 4.762,
'SpaceY' => 0,
'width' => 100.807,
'height' => 35.72
),
'5163' => array(
'paper-size' => 'LETTER',
'unit' => 'mm',
'marginLeft' => 1.762,
'marginTop' => 10.7,
'NX' => 2,
'NY' => 5,
'SpaceX' => 3.175,
'SpaceY' => 0,
'width' => 101.6,
'height' => 50.8
),
'5164' => array(
'paper-size' => 'LETTER',
'unit' => 'in',
'marginLeft' => 0.148,
'marginTop' => 0.5,
'NX' => 2,
'NY' => 3,
'SpaceX' => 0.2031,
'SpaceY' => 0,
'width' => 4.0,
'height' => 3.33
),
'8600' => array(
'paper-size' => 'LETTER',
'unit' => 'mm',
'marginLeft' => 7.1,
'marginTop' => 19,
'NX' => 3,
'NY' => 10,
'SpaceX' => 9.5,
'SpaceY' => 3.1,
'width' => 66.6,
'height' => 25.4
),
'L7163' => array(
'paper-size' => 'A4',
'unit' => 'mm',
'marginLeft' => 5,
'marginTop' => 15,
'NX' => 2,
'NY' => 7,
'SpaceX' => 25,
'SpaceY' => 0,
'width' => 99.1,
'height' => 38.1
),
'3422' => array(
'paper-size' => 'A4',
'unit' => 'mm',
'marginLeft' => 0,
'marginTop' => 8.5,
'NX' => 3,
'NY' => 8,
'SpaceX' => 0,
'SpaceY' => 0,
'width' => 70,
'height' => 35
),
'NewPrint4005' => array(
'paper-size' => 'A4',
'unit' => 'mm',
'marginLeft' => 4,
'marginTop' => 15,
'NX' => 2,
'NY' => 4,
'SpaceX' => 3,
'SpaceY' => 0,
'width' => 99.1,
'height' => 67.2
),
'L7161' => array(
'paper-size' => 'A4',
'unit' => 'mm',
'marginLeft' => 7.25,
'marginTop' => 8.7,
'NX' => 3,
'NY' => 6,
'SpaceX' => 2.5,
'SpaceY' => 0,
'width' => 63.5,
'height' => 46.6
),
'90x54' => array(
'paper-size' => 'A4',
'unit' => 'mm',
'marginLeft' => 15,
'marginTop' => 13.5,
'NX' => 2,
'NY' => 5,
'SpaceX' => 0,
'SpaceY' => 0,
'width' => 90,
'height' => 55,
'cutLines' => true
),
'138x98' => array(
'paper-size' => 'A4',
'unit' => 'mm',
'marginLeft' => 7,
'marginTop' => 10.5,
'NX' => 2,
'NY' => 2,
'SpaceX' => 0,
'SpaceY' => 0,
'width' => 98,
'height' => 138,
'cutLines' => true
)
);
function Header()
{}
function Footer()
{}
/**
*
* @author Burak USGURLU <[email protected]>
* @param array|string $format
* Label type name or dimensions array
* @param string $unit
* @param number $posX
* @param number $posY
* @throws \Exception
*/
public function __construct($format, $unit = 'mm', $posX = 1, $posY = 1)
{
if (is_array($format)) {
// Custom format
$Tformat = $format;
} else {
// Built-in format
if (! isset(PdfLabel::LABELS[$format]))
throw new \Exception('Unknown label format: ' . $format);
$Tformat = PdfLabel::LABELS[$format];
}
parent::__construct('P', $unit, $Tformat['paper-size'], true, 'UTF-8');
$this->setViewerPreferences([
'PrintScaling' => 'None'
]);
$this->sheetUnit = $unit;
$this->setFormat($Tformat);
$this->SetMargins(0, 0);
$this->SetAutoPageBreak(false);
$this->xPosition = $posX - 2;
$this->yPosition = $posY - 1;
}
/**
* Initialize class based on label dimensions
*
* @param array $format
*/
protected function setFormat($format)
{
$this->marginLeft = $this->convertUnit($format['marginLeft'], $format['unit']);
$this->marginTop = $this->convertUnit($format['marginTop'], $format['unit']);
$this->xSpace = $this->convertUnit($format['SpaceX'], $format['unit']);
$this->ySpace = $this->convertUnit($format['SpaceY'], $format['unit']);
$this->xNumber = $format['NX'];
$this->yNumber = $format['NY'];
$this->labelWidth = $this->convertUnit($format['width'], $format['unit']);
$this->labelHeight = $this->convertUnit($format['height'], $format['unit']);
$this->labelPadding = $this->convertUnit(isset($format['padding']) ? $format['padding'] : 3, 'mm');
$this->cutLines = isset($format['cutLines']) ? $format['cutLines'] : false;
}
/**
* convert units (in to mm, mm to in)
*
* @param float $value
* @param string $src
* @return number
*/
protected function convertUnit($value, $src)
{
$dest = $this->sheetUnit;
if ($src != $dest) {
$a['in'] = 39.37008;
$a['mm'] = 1000;
return $value * $a[$dest] / $a[$src];
} else {
return $value;
}
}
/**
* Print label as a TCPDF MultiCell
*
* @param string $text
*/
public function addLabel($text)
{
list ($width, $height) = $this->newLabelPosition();
$this->MultiCell($width, $height, $text, 0, 'L');
}
/**
* Print label as a TCPDF HTMLCell
*
* @param string $html
*/
public function addHtmlLabel($html)
{
list ($width, $height) = $this->newLabelPosition();
$this->writeHTMLCell($width, $height, null, null, $html);
}
/**
* Print label as a TCPDF HTMLCell with a background image
*
* @param string $html
*/
public function addHtmlLabelWithBackground($html, $backgroundImage)
{
list ($width, $height) = $this->newLabelPosition();
$this->Image($backgroundImage, $this->GetX() - $this->labelPadding, $this->GetY() - $this->labelPadding, $this->labelWidth, $this->labelHeight);
$this->writeHTMLCell($width, $height, null, null, $html);
}
/**
* Sets the TCPDF X,Y positions for a new label cell.
*
* @return number[] Returns the height and width of the cell.
*/
protected function newLabelPosition()
{
// on a new page if enabled, draw cutlines
if ($this->xPosition == 0 && $this->cutLines)
$this->drawCutLines();
$this->xPosition ++;
if ($this->xPosition == $this->xNumber) {
// Row full, we start a new one
$this->xPosition = 0;
$this->yPosition ++;
if ($this->yPosition == $this->yNumber) {
// End of page reached, we start a new one
$this->yPosition = 0;
$this->AddPage();
}
}
$this->_PosX = $this->marginLeft + $this->xPosition * ($this->labelWidth + $this->xSpace) + $this->labelPadding;
$this->_PosY = $this->marginTop + $this->yPosition * ($this->labelHeight + $this->ySpace) + $this->labelPadding;
$this->SetXY($this->_PosX, $this->_PosY);
return [
$this->labelWidth - (2 * $this->labelPadding),
$this->labelHeight - (2 * $this->labelPadding)
];
}
protected function drawCutLines()
{
$style = array(
'width' => 0.3,
'cap' => 'butt',
'join' => 'miter',
'dash' => 1,
'color' => array(
200,
200,
200
)
);
for ($i = 0; $i < $this->xNumber; $i ++) {
$x = $this->marginLeft + ($i * ($this->labelWidth + $this->xSpace));
if ($this->cutLines === "s") {
$this->Line($x, 0, $x, $this->marginTop + 1, $style);
$this->Line($x, $this->getPageHeight() - $this->marginTop - 1, $x, $this->getPageHeight(), $style);
} else {
$this->Line($x, 0, $x, $this->getPageHeight(), $style);
}
$x = $this->marginLeft + (($i + 1) * ($this->labelWidth + $this->xSpace)) - $this->xSpace;
if ($this->cutLines === "s") {
$this->Line($x, 0, $x, $this->marginTop + 1, $style);
$this->Line($x, $this->getPageHeight() - $this->marginTop - 1, $x, $this->getPageHeight(), $style);
} else {
$this->Line($x, 0, $x, $this->getPageHeight(), $style);
}
}
for ($i = 0; $i < $this->yNumber; $i ++) {
$y = $this->marginTop + ($i * ($this->labelHeight + $this->ySpace));
if ($this->cutLines === "s") {
$this->Line(0, $y, $this->marginLeft + 1, $y, $style);
$this->Line($this->getPageWidth() - $this->marginLeft - 1, $y, $this->getPageWidth(), $y, $style);
} else {
$this->Line(0, $y, $this->getPageWidth(), $y, $style);
}
$y = $this->marginTop + (($i + 1) * ($this->labelHeight + $this->ySpace)) - $this->ySpace;
if ($this->cutLines === "s") {
$this->Line(0, $y, $this->marginLeft + 1, $y, $style);
$this->Line($this->getPageWidth() - $this->marginLeft - 1, $y, $this->getPageWidth(), $y, $style);
} else {
$this->Line(0, $y, $this->getPageWidth(), $y, $style);
}
}
}
}