1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TcpdfWrapper; |
4
|
|
|
|
5
|
|
|
use setasign\Fpdi\Tcpdf\Fpdi; |
6
|
|
|
use \TCPDF_FONTS; |
7
|
|
|
/** |
8
|
|
|
* TcpdfWrapper |
9
|
|
|
* TcpdfWrapperを記載しやすくするためのラッパー |
10
|
|
|
*/ |
11
|
|
|
class TcpdfWrapper |
12
|
|
|
{ |
13
|
|
|
private $__pdf; |
14
|
|
|
private $__fonts = []; |
15
|
|
|
private $__tcpdfFonts; |
16
|
|
|
// fontの設定キャッシュファイル出力先ディレクトリ |
17
|
|
|
private $fontSettingCacheFileOutDir = ''; |
18
|
|
|
|
19
|
|
|
const TATEGAKI_TYPE_NORMAL = 1; |
20
|
|
|
const TATEGAKI_TYPE_ROUND = 2; |
21
|
|
|
const TATEGAKI_TYPE_RIGHT = 3; |
22
|
|
|
|
23
|
|
|
// publicにしておくので必要に応じて設定 |
24
|
|
|
public $setTategakiType = [ |
25
|
|
|
self::TATEGAKI_TYPE_ROUND => [ |
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
|
|
|
self::TATEGAKI_TYPE_RIGHT => [ |
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
|
|
|
* __construct |
108
|
|
|
* |
109
|
|
|
* @author hagiwara |
110
|
|
|
*/ |
111
|
|
|
public function __construct() |
112
|
|
|
{ |
113
|
|
|
$this->__pdf = new Fpdi(); |
114
|
|
|
$this->__tcpdfFonts = new TCPDF_FONTS(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* setfontSettingCacheFileOutDir |
119
|
|
|
* |
120
|
|
|
* @param string $fontSettingCacheFileOutDir |
121
|
|
|
* @return void |
122
|
|
|
* @author kawano |
123
|
|
|
*/ |
124
|
|
|
public function setFontSettingCacheFileOutDir($fontSettingCacheFileOutDir) |
125
|
|
|
{ |
126
|
|
|
$this->fontSettingCacheFileOutDir = $fontSettingCacheFileOutDir; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* getfontSettingCacheFileOutDir |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
* @author kawano |
134
|
|
|
*/ |
135
|
|
|
public function getFontSettingCacheFileOutDir() |
136
|
|
|
{ |
137
|
|
|
return $this->fontSettingCacheFileOutDir; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* setPrintHeader |
142
|
|
|
* |
143
|
|
|
* @param boolean $print 出力フラグ |
144
|
|
|
* @author hagiwara |
145
|
|
|
*/ |
146
|
|
|
public function setPrintHeader($print) |
147
|
|
|
{ |
148
|
|
|
$this->__pdf->setPrintHeader($print); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* setPrintFooter |
153
|
|
|
* |
154
|
|
|
* @param boolean $print 出力フラグ |
155
|
|
|
* @author hagiwara |
156
|
|
|
*/ |
157
|
|
|
public function setPrintFooter($print) |
158
|
|
|
{ |
159
|
|
|
$this->__pdf->setPrintFooter($print); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* setFont |
164
|
|
|
* |
165
|
|
|
* @param string $name フォント名 |
166
|
|
|
* @param string $path フォントパス nullでデフォルトセット |
167
|
|
|
* @author hagiwara |
168
|
|
|
*/ |
169
|
|
|
public function setFont($name, $path) |
170
|
|
|
{ |
171
|
|
|
$this->__fonts[$name] = $this->__tcpdfFonts->addTTFfont($path, '', '', 32, $this->fontSettingCacheFileOutDir); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* addPage |
176
|
|
|
* |
177
|
|
|
* @param string $template テンプレートパス |
178
|
|
|
* @param integer $templateIndex テンプレートページ |
179
|
|
|
* @author hagiwara |
180
|
|
|
*/ |
181
|
|
|
public function addPage($template, $templateIndex) |
182
|
|
|
{ |
183
|
|
|
// ページを追加 |
184
|
|
|
$this->__pdf->AddPage(); |
185
|
|
|
|
186
|
|
|
// テンプレートを読み込み |
187
|
|
|
$this->__pdf->setSourceFile($template); |
188
|
|
|
|
189
|
|
|
// 読み込んだPDFの1ページ目のインデックスを取得 |
190
|
|
|
$tplIdx = $this->__pdf->importPage($templateIndex); |
191
|
|
|
|
192
|
|
|
// 読み込んだPDFの1ページ目をテンプレートとして使用 |
193
|
|
|
$this->__pdf->useTemplate($tplIdx, null, null, null, null, true); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* setVal |
198
|
|
|
* |
199
|
|
|
* @param string $text テキスト |
200
|
|
|
* @param array $option オプション |
201
|
|
|
* @param array $rotateOption 回転オプション(縦書き対応用) |
202
|
|
|
* @author hagiwara |
203
|
|
|
*/ |
204
|
|
|
public function setVal($text, $option, $rotateOption = []) |
205
|
|
|
{ |
206
|
|
|
$default_option = [ |
207
|
|
|
'w' => 0, |
208
|
|
|
'h' => 0, |
209
|
|
|
'border' => 0, |
210
|
|
|
'align' => '', |
211
|
|
|
'fill' => false, |
212
|
|
|
'link' => '', |
213
|
|
|
'x' => 0, |
214
|
|
|
'y' => 0, |
215
|
|
|
'color' => '000000', |
216
|
|
|
'font' => '', |
217
|
|
|
'size' => 11, |
218
|
|
|
'stretch' => 0, |
219
|
|
|
'auto_size' => false, |
220
|
|
|
]; |
221
|
|
|
$option = array_merge($default_option ,$option); |
222
|
|
|
|
223
|
|
|
// 自動で枠に収めるかどうかのチェック |
224
|
|
|
if ($option['auto_size'] == true) { |
|
|
|
|
225
|
|
|
$fontDefaultWidth = $this->getStringWidth($text, $option['font'], '', $option['size']); |
226
|
|
|
if ($fontDefaultWidth > $option['w']) { |
227
|
|
|
$option['align'] ='J'; |
228
|
|
|
$option['stretch'] =1; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// 書き込む文字列のフォントを指定(フォントの設定キャッシュファイルの出力先がセットされていない場合はデフォルト値) |
233
|
|
|
$fontFilePath = ''; |
234
|
|
|
if (!empty($this->fontSettingCacheFileOutDir)) { |
235
|
|
|
$fontFilePath = $this->generateFontSettingCacheFilePath($option['font']); |
236
|
|
|
} |
237
|
|
|
$this->__pdf->SetFont($this->getFont($option['font']), '', $option['size'], $fontFilePath); |
238
|
|
|
// 書き込む文字列の文字色を指定 |
239
|
|
|
$concertColor = $this->colorCodeConvert($option['color']); |
240
|
|
|
$this->__pdf->SetTextColor($concertColor['r'], $concertColor['g'], $concertColor['b']); |
241
|
|
|
|
242
|
|
|
$this->__pdf->SetXY($option['x'], $option['y']); |
243
|
|
|
if (!empty($rotateOption)) { |
244
|
|
|
$default_rotate_option = [ |
245
|
|
|
'angle' => 0, |
246
|
|
|
'x' => '', |
247
|
|
|
'y' => '', |
248
|
|
|
]; |
249
|
|
|
$rotateOption = array_merge($default_rotate_option ,$rotateOption); |
250
|
|
|
$this->__pdf->Rotate($rotateOption['angle'], $rotateOption['x'], $rotateOption['y']); |
251
|
|
|
} |
252
|
|
|
// 文字列を書き込む |
253
|
|
|
$this->__pdf->Cell($option['w'], $option['h'], $text, $option['border'], 0, $option['align'], $option['fill'], $option['link'], $option['stretch']); |
254
|
|
|
// 元に戻しておく |
255
|
|
|
if (!empty($rotateOption)) { |
256
|
|
|
$this->__pdf->Rotate($rotateOption['angle'] * -1, $rotateOption['x'], $rotateOption['y']); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* setValTategaki |
262
|
|
|
* 縦書き対応/改行は対応しきれない。折り返しもしない |
263
|
|
|
* |
264
|
|
|
* @param string $text テキスト |
265
|
|
|
* @param array $option オプション |
266
|
|
|
* @author hagiwara |
267
|
|
|
*/ |
268
|
|
|
public function setValTategaki($text, $option) |
269
|
|
|
{ |
270
|
|
|
$default_option = [ |
271
|
|
|
'h' => 0, |
272
|
|
|
'border' => 0, |
273
|
|
|
'fill' => false, |
274
|
|
|
'link' => '', |
275
|
|
|
'x' => 0, |
276
|
|
|
'y' => 0, |
277
|
|
|
'color' => '000000', |
278
|
|
|
'font' => '', |
279
|
|
|
'size' => 11, |
280
|
|
|
]; |
281
|
|
|
$option = array_merge($default_option ,$option); |
282
|
|
|
|
283
|
|
|
// 設定している固定の高さとする |
284
|
|
|
$wordHeight = $option['h']; |
285
|
|
|
// 文字の幅は対応する文字の一番幅の大きい文字とする |
286
|
|
|
$wordWidth = max($this->getStringWidth($text, $option['font'], '', $option['size'], true)); |
287
|
|
|
$splitWord = preg_split("//u", $text, -1, PREG_SPLIT_NO_EMPTY); |
288
|
|
|
$top = $option['y']; |
289
|
|
|
foreach ($splitWord as $word) { |
290
|
|
|
// 一文字ことにオプションを設定 |
291
|
|
|
$partsOption = $option; |
292
|
|
|
$partsOption['w'] = $wordWidth; |
293
|
|
|
$partsOption['h'] = $wordHeight; |
294
|
|
|
$partsOption['auto_size'] = false; |
295
|
|
|
$partsOption['align'] = 'C'; |
296
|
|
|
$partsOption['stretch'] = '0'; |
297
|
|
|
$partsOption['y'] = $top; |
298
|
|
|
|
299
|
|
|
// 縦書き対応 |
300
|
|
|
$rotateOption = []; |
301
|
|
|
switch ($this->getTategakiWordType($word)) { |
302
|
|
|
// 回転が必要な文字 |
303
|
|
|
case self::TATEGAKI_TYPE_ROUND: |
304
|
|
|
$rotateOption = [ |
305
|
|
|
'angle' => -90, |
306
|
|
|
'x' => $partsOption['x'] + ($partsOption['w'] * 0.5), |
307
|
|
|
'y' => $partsOption['y'] + ($partsOption['h'] * 0.5), |
308
|
|
|
]; |
309
|
|
|
break; |
310
|
|
|
// 小さいゃゅょ、句読点を少し右寄せする |
311
|
|
|
case self::TATEGAKI_TYPE_RIGHT: |
312
|
|
|
$partsOption['x'] += $partsOption['size'] * 0.05; |
313
|
|
|
break; |
314
|
|
|
|
315
|
|
|
default: |
316
|
|
|
break; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
$this->setVal($word, $partsOption, $rotateOption); |
320
|
|
|
|
321
|
|
|
// 固定の高さ分文字幅を取る |
322
|
|
|
$top += $wordHeight; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* getTategakiWordType |
328
|
|
|
* 縦書きに必要な種別の取得 |
329
|
|
|
* |
330
|
|
|
* @param string $word テキスト |
331
|
|
|
* @return int |
332
|
|
|
* @author hagiwara |
333
|
|
|
*/ |
334
|
|
|
private function getTategakiWordType($word) |
335
|
|
|
{ |
336
|
|
|
if (in_array($word, $this->setTategakiType[self::TATEGAKI_TYPE_ROUND], true)) { |
337
|
|
|
return self::TATEGAKI_TYPE_ROUND; |
338
|
|
|
} elseif (in_array($word, $this->setTategakiType[self::TATEGAKI_TYPE_RIGHT], true)) { |
339
|
|
|
return self::TATEGAKI_TYPE_RIGHT; |
340
|
|
|
} else { |
341
|
|
|
return self::TATEGAKI_TYPE_NORMAL; |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* setHtml |
347
|
|
|
* |
348
|
|
|
* @param string $html HTML |
349
|
|
|
* @param array $option オプション |
350
|
|
|
* @author hagiwara |
351
|
|
|
*/ |
352
|
|
|
public function setHtml($html, $option) |
353
|
|
|
{ |
354
|
|
|
$default_option = [ |
355
|
|
|
'w' => 0, |
356
|
|
|
'h' => 0, |
357
|
|
|
'border' => 0, |
358
|
|
|
'align' => '', |
359
|
|
|
'fill' => false, |
360
|
|
|
'link' => '', |
361
|
|
|
'x' => 0, |
362
|
|
|
'y' => 0, |
363
|
|
|
'color' => '000000', |
364
|
|
|
'font' => '', |
365
|
|
|
'size' => '', |
366
|
|
|
'reseth' => true, |
367
|
|
|
'autopadding' => false, |
368
|
|
|
]; |
369
|
|
|
$option = array_merge($default_option ,$option); |
370
|
|
|
// 書き込む文字列の文字色を指定 |
371
|
|
|
//$concertColor = $this->colorCodeConvert($option['color']); |
372
|
|
|
//var_dump($concertColor); |
373
|
|
|
//$this->__pdf->SetTextColor($concertColor['r'], $concertColor['g'], $concertColor['b']); |
374
|
|
|
|
375
|
|
|
// 書き込む文字列のフォントを指定(フォントの設定キャッシュファイルの出力先がセットされていない場合はデフォルト値) |
376
|
|
|
$fontFilePath = ''; |
377
|
|
|
if (!empty($this->fontSettingCacheFileOutDir)) { |
378
|
|
|
$fontFilePath = $this->generateFontSettingCacheFilePath($option['font']); |
379
|
|
|
} |
380
|
|
|
$this->__pdf->SetFont($this->getFont($option['font']), '', $option['size'], $fontFilePath); |
381
|
|
|
|
382
|
|
|
$this->__pdf->writeHTMLCell( $option['w'], $option['h'], $option['x'], $option['y'], $html, $option['border'], 0, $option['fill'], $option['reseth'], $option['align'], $option['autopadding']); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* getFont |
387
|
|
|
* |
388
|
|
|
* @param string $font フォント名 |
389
|
|
|
* @author hagiwara |
390
|
|
|
*/ |
391
|
|
|
private function getFont($font) |
392
|
|
|
{ |
393
|
|
|
if (array_key_exists($font, $this->__fonts)) { |
394
|
|
|
return $this->__fonts[$font]; |
395
|
|
|
} else { |
396
|
|
|
return $font; |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* setImage |
402
|
|
|
* |
403
|
|
|
* @param string $image 画像パス |
404
|
|
|
* @param array $option オプション |
405
|
|
|
* @author hagiwara |
406
|
|
|
*/ |
407
|
|
|
public function setImage($image, $option) |
408
|
|
|
{ |
409
|
|
|
$default_option = [ |
410
|
|
|
'x' => 0, |
411
|
|
|
'y' => 0, |
412
|
|
|
'w' => 0, |
413
|
|
|
'h' => 0, |
414
|
|
|
'link' => '', |
415
|
|
|
'resize' => true, |
416
|
|
|
'dpi' => '300', |
417
|
|
|
]; |
418
|
|
|
$option = array_merge($default_option ,$option); |
419
|
|
|
$this->__pdf->Image($image, $option['x'], $option['y'], $option['w'], $option['h'], '', $option['link'], '', $option['resize'], $option['dpi']); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* colorCodeConvert |
425
|
|
|
* |
426
|
|
|
* @param string $color カラーコード(16進数) |
427
|
|
|
* @author hagiwara |
428
|
|
|
*/ |
429
|
|
|
private function colorCodeConvert($color) |
430
|
|
|
{ |
431
|
|
|
if ( |
432
|
|
|
preg_match('/^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/', $color, $colorCheck) |
433
|
|
|
) { |
434
|
|
|
return [ |
435
|
|
|
'r' => hexdec($colorCheck[1]), |
436
|
|
|
'g' => hexdec($colorCheck[2]), |
437
|
|
|
'b' => hexdec($colorCheck[3]), |
438
|
|
|
]; |
439
|
|
|
} else { |
440
|
|
|
return [ |
441
|
|
|
'r' => 0, |
442
|
|
|
'g' => 0, |
443
|
|
|
'b' => 0, |
444
|
|
|
]; |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* setAutoPageBreak |
450
|
|
|
* page brackeを自動で行うかどうか。画像を下部に埋め込む際には切っておいたほうが良さげ |
451
|
|
|
* @param int $auto |
452
|
|
|
* @param int $margin |
453
|
|
|
*/ |
454
|
|
|
public function setAutoPageBreak($auto, $margin = 0) |
455
|
|
|
{ |
456
|
|
|
$this->__pdf->SetAutoPageBreak($auto, $margin); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* getStringWidth |
461
|
|
|
* |
462
|
|
|
* @param string $text テキスト |
463
|
|
|
* @param string $font フォント名 |
464
|
|
|
* @param string $fontstyle フォントスタイル |
465
|
|
|
* @param integer $fontsize サイズ |
466
|
|
|
* @param bool $getarray 結果を1文字ずつ配列で返すか |
467
|
|
|
* @author hagiwara |
468
|
|
|
*/ |
469
|
|
|
public function getStringWidth($text, $font, $fontstyle, $fontsize, $getarray = false) { |
470
|
|
|
return $this->__pdf->GetStringWidth( $text, $font, $fontstyle, $fontsize, $getarray); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* write |
475
|
|
|
* |
476
|
|
|
* @param string $file 出力ファイル |
477
|
|
|
* @author hagiwara |
478
|
|
|
*/ |
479
|
|
|
public function write($file) |
480
|
|
|
{ |
481
|
|
|
$pdf_info = $this->__pdf->Output(null, 'S'); |
482
|
|
|
|
483
|
|
|
$fp = fopen($file, 'w'); |
484
|
|
|
fwrite($fp ,$pdf_info); |
485
|
|
|
fclose($fp); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* フォント設定キャッシュファイルのパスを返す |
490
|
|
|
* $this->fontSettingCacheFileOutDir (上記ファイルの出力先ディレクトリ) を指定した場合のみ呼ばれる |
491
|
|
|
* ファイル名生成アルゴリズム部分は右記を参照: https://github.com/tecnickcom/TCPDF/blob/master/include/tcpdf_fonts.php#L79 〜 https://github.com/tecnickcom/TCPDF/blob/master/include/tcpdf_fonts.php#L92 |
492
|
|
|
* @param string $font フォント名 |
493
|
|
|
* @return string |
494
|
|
|
* @author kawano |
495
|
|
|
*/ |
496
|
|
|
private function generateFontSettingCacheFilePath($font) |
497
|
|
|
{ |
498
|
|
|
// build new font name for TCPDF compatibility |
499
|
|
|
$font_path_parts = pathinfo($font); |
500
|
|
|
if (!isset($font_path_parts['filename'])) { |
501
|
|
|
$font_path_parts['filename'] = substr($font_path_parts['basename'], 0, -(strlen($font_path_parts['extension']) + 1)); |
502
|
|
|
} |
503
|
|
|
$font_name = strtolower($font_path_parts['filename']); |
504
|
|
|
$font_name = preg_replace('/[^a-z0-9_]/', '', $font_name); |
505
|
|
|
$search = array('bold', 'oblique', 'italic', 'regular'); |
506
|
|
|
$replace = array('b', 'i', 'i', ''); |
507
|
|
|
$font_name = str_replace($search, $replace, $font_name); |
508
|
|
|
if (empty($font_name)) { |
509
|
|
|
return 'tcpdffont' . '.php'; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
return $this->fontSettingCacheFileOutDir . $font_name . '.php'; |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.