Completed
Push — master ( a5d74f...59cad6 )
by satoru
9s
created

TcpdfWrapper::setPrintFooter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace TcpdfWrapper;
4
5
use \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
    /**
18
    * __construct
19
    *
20
    * @author hagiwara
21
    */
22
    public function __construct()
23
    {
24
        $this->__pdf = new FPDI();
25
        $this->__tcpdfFonts = new TCPDF_FONTS();
26
    }
27
28
    /**
29
    * setPrintHeader
30
    *
31
    * @param boolean $print 出力フラグ
32
    * @author hagiwara
33
    */
34
    public function setPrintHeader($print)
35
    {
36
        $this->__pdf->setPrintHeader($print);
37
    }
38
39
    /**
40
    * setPrintFooter
41
    *
42
    * @param boolean $print 出力フラグ
43
    * @author hagiwara
44
    */
45
    public function setPrintFooter($print)
46
    {
47
        $this->__pdf->setPrintFooter($print);
48
    }
49
50
    /**
51
    * setFont
52
    *
53
    * @param string $name フォント名
54
    * @param string $path フォントパス nullでデフォルトセット
55
    * @author hagiwara
56
    */
57
    public function setFont($name, $path)
58
    {
59
        $this->__fonts[$name] = $this->__tcpdfFonts->addTTFfont($path);
60
    }
61
62
    /**
63
    * addPage
64
    *
65
    * @param string $template テンプレートパス
66
    * @param integer $templateIndex テンプレートページ
67
    * @author hagiwara
68
    */
69
    public function addPage($template, $templateIndex)
70
    {
71
        // ページを追加
72
        $this->__pdf->AddPage();
73
74
        // テンプレートを読み込み
75
        $this->__pdf->setSourceFile($template);
76
77
        // 読み込んだPDFの1ページ目のインデックスを取得
78
        $tplIdx = $this->__pdf->importPage($templateIndex);
79
80
        // 読み込んだPDFの1ページ目をテンプレートとして使用
81
        $this->__pdf->useTemplate($tplIdx, null, null, null, null, true);
82
    }
83
84
    /**
85
    * setVal
86
    *
87
    * @param string $text テキスト
88
    * @param array $option オプション
89
    * @author hagiwara
90
    */
91
    public function setVal($text, $option)
92
    {
93
        $default_option = [
94
            'w' => 0,
95
            'h' => 0,
96
            'border' => 0,
97
            'align' => '',
98
            'fill' => false,
99
            'link' => '',
100
            'x' => 0,
101
            'y' => 0,
102
            'color' => '000000',
103
            'font' => '',
104
            'size' => 11,
105
            'stretch' => 0,
106
            'auto_size' => false,
107
        ];
108
        $option = array_merge($default_option ,$option);
109
        
110
        // 自動で枠に収めるかどうかのチェック
111
        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...
112
            $fontDefaultWidth = $this->getStringWidth($text, $option['font'], '', $option['size']);
113
            if ($fontDefaultWidth > $option['w']) {
114
                $option['align'] ='J';
115
                $option['stretch'] =1;
116
            }
117
        }
118
        
119
        // 書き込む文字列のフォントを指定
120
        $this->__pdf->SetFont($this->getFont($option['font']), '', $option['size']);
121
        // 書き込む文字列の文字色を指定
122
        $concertColor = $this->colorCodeConvert($option['color']);
123
        $this->__pdf->SetTextColor($concertColor['r'], $concertColor['g'], $concertColor['b']);
124
125
        $this->__pdf->SetXY($option['x'], $option['y']);
126
        // 文字列を書き込む
127
        $this->__pdf->Cell($option['w'], $option['h'], $text, $option['border'], 0, $option['align'], $option['fill'], $option['link'], $option['stretch']);
128
    }
129
130
    /**
131
    * getFont
132
    *
133
    * @param string $font フォント名
134
    * @author hagiwara
135
    */
136
    private function getFont($font)
137
    {
138
        if (array_key_exists($font, $this->__fonts)) {
139
            return $this->__fonts[$font];
140
        } else {
141
            return $font;
142
        }
143
    }
144
145
    /**
146
    * setImage
147
    *
148
    * @param string $image 画像パス
149
    * @param array $option オプション
150
    * @author hagiwara
151
    */
152
    public function setImage($image, $option)
153
    {
154
        $default_option = [
155
            'x' => 0,
156
            'y' => 0,
157
            'w' => 0,
158
            'h' => 0,
159
            'link' => '',
160
            'resize' => true,
161
            'dpi' => '300',
162
        ];
163
        $option = array_merge($default_option ,$option);
164
        $this->__pdf->Image($image, $option['x'], $option['y'], $option['w'], $option['h'], '', $option['link'], '', $option['resize'], $option['dpi']);
165
    }
166
167
168
    /**
169
    * colorCodeConvert
170
    *
171
    * @param string $color カラーコード(16進数)
172
    * @author hagiwara
173
    */
174
    private function colorCodeConvert($color)
175
    {
176
        if (
177
            preg_match('/^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/', $color, $colorCheck)
178
        ) {
179
            return [
180
                'r' => hexdec($colorCheck[1]),
181
                'g' => hexdec($colorCheck[2]),
182
                'b' => hexdec($colorCheck[3]),
183
            ];
184
        } else {
185
            return [
186
                'r' => 0,
187
                'g' => 0,
188
                'b' => 0,
189
            ];
190
        }
191
    }
192
    
193
   /**
194
    * getStringWidth
195
    *
196
    * @param string $text テキスト
197
    * @param string $font フォント名
198
    * @param string $fontstyle フォントスタイル
199
    * @param integer $fontsize サイズ
200
    * @param bool $getarray 結果を1文字ずつ配列で返すか
201
    * @author hagiwara
202
    */
203
    public function getStringWidth($text, $font, $fontstyle, $fontsize, $getarray = false) {
204
        return $this->__pdf->GetStringWidth( $text, $font, $fontstyle, $fontsize, $getarray);
205
    }
206
207
    /**
208
    * write
209
    *
210
    * @param string $file 出力ファイル
211
    * @author hagiwara
212
    */
213
    public function write($file)
214
    {
215
        $pdf_info = $this->__pdf->Output(null, 'S');
216
217
        $fp = fopen($file, 'w');
218
        fwrite($fp ,$pdf_info);
219
        fclose($fp);
220
    }
221
222
}
223