Completed
Push — master ( 20aa43...e59ed7 )
by satoru
02:20 queued 01:06
created

TcpdfWrapper   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 445
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 2
dl 0
loc 445
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPrintHeader() 0 4 1
A setPrintFooter() 0 4 1
A setFont() 0 4 1
A addPage() 0 14 1
B setVal() 0 51 5
B setValTategaki() 0 57 4
A getTategakiWordType() 0 10 3
A setHtml() 0 28 1
A getFont() 0 8 2
A setImage() 0 14 1
A colorCodeConvert() 0 18 2
A setAutoPageBreak() 0 4 1
A getStringWidth() 0 3 1
A write() 0 8 1
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
17
    const TATEGAKI_TYPE_NORMAL = 1;
18
    const TATEGAKI_TYPE_ROUND = 2;
19
    const TATEGAKI_TYPE_RIGHT = 3;
20
21
    // publicにしておくので必要に応じて設定
22
    public $setTategakiType = [
23
        self::TATEGAKI_TYPE_ROUND => [
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
        self::TATEGAKI_TYPE_RIGHT => [
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
    * __construct
106
    *
107
    * @author hagiwara
108
    */
109
    public function __construct()
110
    {
111
        $this->__pdf = new Fpdi();
112
        $this->__tcpdfFonts = new TCPDF_FONTS();
113
    }
114
115
    /**
116
    * setPrintHeader
117
    *
118
    * @param boolean $print 出力フラグ
119
    * @author hagiwara
120
    */
121
    public function setPrintHeader($print)
122
    {
123
        $this->__pdf->setPrintHeader($print);
124
    }
125
126
    /**
127
    * setPrintFooter
128
    *
129
    * @param boolean $print 出力フラグ
130
    * @author hagiwara
131
    */
132
    public function setPrintFooter($print)
133
    {
134
        $this->__pdf->setPrintFooter($print);
135
    }
136
137
    /**
138
    * setFont
139
    *
140
    * @param string $name フォント名
141
    * @param string $path フォントパス nullでデフォルトセット
142
    * @author hagiwara
143
    */
144
    public function setFont($name, $path)
145
    {
146
        $this->__fonts[$name] = $this->__tcpdfFonts->addTTFfont($path);
147
    }
148
149
    /**
150
    * addPage
151
    *
152
    * @param string $template テンプレートパス
153
    * @param integer $templateIndex テンプレートページ
154
    * @author hagiwara
155
    */
156
    public function addPage($template, $templateIndex)
157
    {
158
        // ページを追加
159
        $this->__pdf->AddPage();
160
161
        // テンプレートを読み込み
162
        $this->__pdf->setSourceFile($template);
163
164
        // 読み込んだPDFの1ページ目のインデックスを取得
165
        $tplIdx = $this->__pdf->importPage($templateIndex);
166
167
        // 読み込んだPDFの1ページ目をテンプレートとして使用
168
        $this->__pdf->useTemplate($tplIdx, null, null, null, null, true);
169
    }
170
171
    /**
172
    * setVal
173
    *
174
    * @param string $text テキスト
175
    * @param array $option オプション
176
    * @param array $rotateOption 回転オプション(縦書き対応用)
177
    * @author hagiwara
178
    */
179
    public function setVal($text, $option, $rotateOption = [])
180
    {
181
        $default_option = [
182
            'w' => 0,
183
            'h' => 0,
184
            'border' => 0,
185
            'align' => '',
186
            'fill' => false,
187
            'link' => '',
188
            'x' => 0,
189
            'y' => 0,
190
            'color' => '000000',
191
            'font' => '',
192
            'size' => 11,
193
            'stretch' => 0,
194
            'auto_size' => false,
195
        ];
196
        $option = array_merge($default_option ,$option);
197
        
198
        // 自動で枠に収めるかどうかのチェック
199
        if ($option['auto_size'] == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
200
            $fontDefaultWidth = $this->getStringWidth($text, $option['font'], '', $option['size']);
201
            if ($fontDefaultWidth > $option['w']) {
202
                $option['align'] ='J';
203
                $option['stretch'] =1;
204
            }
205
        }
206
        
207
        // 書き込む文字列のフォントを指定
208
        $this->__pdf->SetFont($this->getFont($option['font']), '', $option['size']);
209
        // 書き込む文字列の文字色を指定
210
        $concertColor = $this->colorCodeConvert($option['color']);
211
        $this->__pdf->SetTextColor($concertColor['r'], $concertColor['g'], $concertColor['b']);
212
213
        $this->__pdf->SetXY($option['x'], $option['y']);
214
        if (!empty($rotateOption)) {
215
            $default_rotate_option = [
216
                'angle' => 0,
217
                'x' => '',
218
                'y' => '',
219
            ];
220
            $rotateOption = array_merge($default_rotate_option ,$rotateOption);
221
            $this->__pdf->Rotate($rotateOption['angle'], $rotateOption['x'], $rotateOption['y']);
222
        }
223
        // 文字列を書き込む
224
        $this->__pdf->Cell($option['w'], $option['h'], $text, $option['border'], 0, $option['align'], $option['fill'], $option['link'], $option['stretch']);
225
        // 元に戻しておく
226
        if (!empty($rotateOption)) {
227
            $this->__pdf->Rotate($rotateOption['angle'] * -1, $rotateOption['x'], $rotateOption['y']);
228
        }
229
    }
230
231
    /**
232
    * setValTategaki
233
    * 縦書き対応/改行は対応しきれない。折り返しもしない
234
    *
235
    * @param string $text テキスト
236
    * @param array $option オプション
237
    * @author hagiwara
238
    */
239
    public function setValTategaki($text, $option)
240
    {
241
        $default_option = [
242
            'h' => 0,
243
            'border' => 0,
244
            'fill' => false,
245
            'link' => '',
246
            'x' => 0,
247
            'y' => 0,
248
            'color' => '000000',
249
            'font' => '',
250
            'size' => 11,
251
        ];
252
        $option = array_merge($default_option ,$option);
253
254
        // 設定している固定の高さとする
255
        $wordHeight = $option['h'];
256
        // 文字の幅は対応する文字の一番幅の大きい文字とする
257
        $wordWidth = max($this->getStringWidth($text, $option['font'], '', $option['size'], true));
258
        $splitWord = preg_split("//u", $text, -1, PREG_SPLIT_NO_EMPTY);
259
        $top = $option['y'];
260
        foreach ($splitWord as $word) {
261
            // 一文字ことにオプションを設定
262
            $partsOption = $option;
263
            $partsOption['w'] = $wordWidth;
264
            $partsOption['h'] = $wordHeight;
265
            $partsOption['auto_size'] = false;
266
            $partsOption['align'] = 'C';
267
            $partsOption['stretch'] = '0';
268
            $partsOption['y'] = $top;
269
270
            // 縦書き対応
271
            $rotateOption = [];
272
            switch ($this->getTategakiWordType($word)) {
273
                // 回転が必要な文字
274
                case self::TATEGAKI_TYPE_ROUND:
275
                    $rotateOption = [
276
                        'angle' => -90,
277
                        'x' => $partsOption['x'] + ($partsOption['w'] * 0.5),
278
                        'y' => $partsOption['y'] + ($partsOption['h'] * 0.5),
279
                    ];
280
                    break;
281
                // 小さいゃゅょ、句読点を少し右寄せする
282
                case self::TATEGAKI_TYPE_RIGHT:
283
                    $partsOption['x'] += $partsOption['size'] * 0.05;
284
                    break;
285
286
                default:
287
                    break;
288
            }
289
290
            $this->setVal($word, $partsOption, $rotateOption);
291
292
            // 固定の高さ分文字幅を取る
293
            $top += $wordHeight;
294
        }
295
    }
296
297
    /**
298
    * getTategakiWordType
299
    * 縦書きに必要な種別の取得
300
    *
301
    * @param string $word テキスト
302
    * @return int
303
    * @author hagiwara
304
    */
305
    private function getTategakiWordType($word)
306
    {
307
        if (in_array($word, $this->setTategakiType[self::TATEGAKI_TYPE_ROUND], true)) {
308
            return self::TATEGAKI_TYPE_ROUND;
309
        } elseif (in_array($word, $this->setTategakiType[self::TATEGAKI_TYPE_RIGHT], true)) {
310
            return self::TATEGAKI_TYPE_RIGHT;
311
        } else {
312
            return self::TATEGAKI_TYPE_NORMAL;
313
        }
314
    }
315
316
    /**
317
    * setHtml
318
    *
319
    * @param string $html HTML
320
    * @param array $option オプション
321
    * @author hagiwara
322
    */
323
    public function setHtml($html, $option)
324
    {
325
        $default_option = [
326
            'w' => 0,
327
            'h' => 0,
328
            'border' => 0,
329
            'align' => '',
330
            'fill' => false,
331
            'link' => '',
332
            'x' => 0,
333
            'y' => 0,
334
            'color' => '000000',
335
            'font' => '',
336
            'size' => '',
337
            'reseth' => true,
338
            'autopadding' => false,
339
        ];
340
        $option = array_merge($default_option ,$option);
341
        // 書き込む文字列の文字色を指定
342
        //$concertColor = $this->colorCodeConvert($option['color']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
343
        //var_dump($concertColor);
344
        //$this->__pdf->SetTextColor($concertColor['r'], $concertColor['g'], $concertColor['b']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
345
346
        // 書き込む文字列のフォントを指定
347
        $this->__pdf->SetFont($this->getFont($option['font']), '', $option['size']);
348
        
349
        $this->__pdf->writeHTMLCell( $option['w'], $option['h'], $option['x'], $option['y'], $html, $option['border'], 0, $option['fill'], $option['reseth'], $option['align'], $option['autopadding']);
350
    }
351
352
    /**
353
    * getFont
354
    *
355
    * @param string $font フォント名
356
    * @author hagiwara
357
    */
358
    private function getFont($font)
359
    {
360
        if (array_key_exists($font, $this->__fonts)) {
361
            return $this->__fonts[$font];
362
        } else {
363
            return $font;
364
        }
365
    }
366
367
    /**
368
    * setImage
369
    *
370
    * @param string $image 画像パス
371
    * @param array $option オプション
372
    * @author hagiwara
373
    */
374
    public function setImage($image, $option)
375
    {
376
        $default_option = [
377
            'x' => 0,
378
            'y' => 0,
379
            'w' => 0,
380
            'h' => 0,
381
            'link' => '',
382
            'resize' => true,
383
            'dpi' => '300',
384
        ];
385
        $option = array_merge($default_option ,$option);
386
        $this->__pdf->Image($image, $option['x'], $option['y'], $option['w'], $option['h'], '', $option['link'], '', $option['resize'], $option['dpi']);
387
    }
388
389
390
    /**
391
    * colorCodeConvert
392
    *
393
    * @param string $color カラーコード(16進数)
394
    * @author hagiwara
395
    */
396
    private function colorCodeConvert($color)
397
    {
398
        if (
399
            preg_match('/^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/', $color, $colorCheck)
400
        ) {
401
            return [
402
                'r' => hexdec($colorCheck[1]),
403
                'g' => hexdec($colorCheck[2]),
404
                'b' => hexdec($colorCheck[3]),
405
            ];
406
        } else {
407
            return [
408
                'r' => 0,
409
                'g' => 0,
410
                'b' => 0,
411
            ];
412
        }
413
    }
414
415
    /**
416
     * setAutoPageBreak
417
     * page brackeを自動で行うかどうか。画像を下部に埋め込む際には切っておいたほうが良さげ
418
     * @param int $auto
419
     * @param int $margin
420
     */
421
    public function setAutoPageBreak($auto, $margin = 0)
422
    {
423
        $this->__pdf->SetAutoPageBreak($auto, $margin);
424
    }
425
    
426
   /**
427
    * getStringWidth
428
    *
429
    * @param string $text テキスト
430
    * @param string $font フォント名
431
    * @param string $fontstyle フォントスタイル
432
    * @param integer $fontsize サイズ
433
    * @param bool $getarray 結果を1文字ずつ配列で返すか
434
    * @author hagiwara
435
    */
436
    public function getStringWidth($text, $font, $fontstyle, $fontsize, $getarray = false) {
437
        return $this->__pdf->GetStringWidth( $text, $font, $fontstyle, $fontsize, $getarray);
438
    }
439
440
    /**
441
    * write
442
    *
443
    * @param string $file 出力ファイル
444
    * @author hagiwara
445
    */
446
    public function write($file)
447
    {
448
        $pdf_info = $this->__pdf->Output(null, 'S');
449
450
        $fp = fopen($file, 'w');
451
        fwrite($fp ,$pdf_info);
452
        fclose($fp);
453
    }
454
455
}
456