source: branches/feature-module-update/data/class/SC_Pdf.php @ 15532

Revision 15532, 15.6 KB checked in by nanasess, 17 years ago (diff)

svn:mime-type 修正

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4 *
5 * http://www.lockon.co.jp/
6 */
7
8/*----------------------------------------------------------------------
9 * [名称] GC_Pdf
10 * [概要] Pdfファイルを表示する。(PDFLib必須)
11 *----------------------------------------------------------------------
12 */
13
14// グリッドと文字の間隔
15define("GRID_SPACE", 4);
16
17class SC_Pdf {
18    var $arrText;
19    var $arrImage;
20    var $license_key;
21    var $block_option;
22    var $src_code;
23    var $dst_code;
24    var $pdiwarning;
25    var $pdfpath;
26    var $page_close;
27           
28    function SC_Pdf($width = 595, $height = 842, $fontsize = 10) {
29        $this->license_key = "B600602-010400-714251-5851C1";
30        $this->src_code = CHAR_CODE;
31        // UTF-8でないとブロック内で改行できない。
32        $this->dst_code = "UTF-8";
33        // PDF BLOCKのプロパティ
34        $this->block_option = "encoding=UniJIS-UCS2-H textformat=utf8 fontname=HeiseiMin-W3 textflow=true";
35        // 警告表示
36        $this->pdiwarning = "true";
37        // ページサイズ設定
38        $this->width = $width;
39        $this->height = $height;
40        // PDF初期化
41        $this->pdf = PDF_new();
42        PDF_set_parameter($this->pdf, "license", $this->license_key);
43        PDF_set_parameter($this->pdf, "pdiwarning", $this->pdiwarning);
44        // ドキュメント開始
45        PDF_begin_document($this->pdf, NULL, NULL);
46        // ページの状態
47        $this->page_open = false;
48        // テーブルの色設定
49        $this->setTableColor();
50        // フォントサイズの設定
51        $this->fontsize = $fontsize;
52        // グリッド描画の特殊指定
53        $this->arrLines = array();
54        // テーブルタイトルのスタイル
55        $this->arrHeaderColSize = array();
56        $this->arrHeaderAlign = array();
57        // テーブル補正値
58        $this->table_left = 0;
59        // タイトル行の出力
60        $this->title_enable = true;
61        // グリッドの出力
62        $this->grid_enable = true;
63    }
64   
65    // タイトルを出力するか否か
66    function setTitleEnable($flag) {
67        $this->title_enable = $flag;
68    }
69   
70    // グリッドを出力するか否か
71    function setGridEnable($flag) {
72        $this->grid_enable = $flag;
73    }
74       
75       
76    // キー:ブロック名、値:表示テキストのハッシュ配列をセットする。
77    function setTextBlock($list) {
78        unset($this->arrText);
79        $this->arrText[] = $list;
80    }
81   
82    // キー:ブロック名、値:ファイルパスのハッシュ配列をセットする。
83    // ※パスはドキュメントルート以下
84    function setImageBlock($list) {
85        unset($this->arrImage);
86        $this->arrImage[] = $list;
87    }
88   
89    // 表示背景となるテンプレートファイルパス
90    // ※パスはドキュメントルート以下
91    function setTemplate($pdfpath) {
92        if(file_exists($pdfpath)) {
93            $this->pdfpath = $pdfpath;
94        } else {
95            print("指定したPDFテンプレートは存在しません:".$pdfpath);
96            exit;
97        }
98    }
99   
100    // テーブル位置補正値
101    function setTableLeft($table_left) {
102        $this->table_left = $table_left;
103    }
104   
105    // グリッド描画の特殊指定
106    function setGridLines($list) {
107        $this->arrLines = $list;
108    }
109   
110    // テーブルタイトルのスタイル設定
111    function setTableHeaderStyle($arrColSize, $arrAlign) {
112        $this->arrHeaderColSize = $arrColSize;
113        $this->arrHeaderAlign = $arrAlign;
114    }
115   
116    // ブロックデータの書き込み(closeすると次回新規ページ)
117    function writeBlock() {
118        // テンプレートを使用する
119        if(!file_exists($this->pdfpath)) {
120            return;
121        }
122        // 既存PDFのドキュメントを取得
123        $doc = pdf_open_pdi($this->pdf, $this->pdfpath, NULL, 0 );
124        // 既存PDFのドキュメントから指定ページを取得
125        $page = pdf_open_pdi_page($this->pdf, $doc, 1, NULL );
126        // ページを開く
127        $this->openPage();
128       
129        // 既存PDFのページを割り当てる
130        PDF_fit_pdi_page($this->pdf, $page, 0, 0, "adjustpage");
131       
132        // テキストブロックの書き込み
133        $max = count($this->arrText);
134        for($i = 0;$i < $max; $i++) {
135            foreach($this->arrText[$i] as $key => $val) {
136                if($val != "") {
137                    // 文字コードの変換
138                    mb_convert_variables($this->dst_code, $this->src_code, $val);
139                    // 書き込み
140                    $ret = PDF_fill_textblock($this->pdf, $page, $key, $val, $this->block_option);
141                }
142            }
143        }
144       
145        // イメージブロックの書き込み
146        $max = count($this->arrImage);
147        for($i = 0;$i < $max; $i++) {
148            foreach($this->arrImage[$i] as $key => $val) {
149                if($val != "") {
150                    $img = PDF_load_image($this->pdf, "auto", $val, NULL );
151                    $ret = PDF_fill_imageblock($this->pdf, $page, $key, $img, NULL);
152                }
153            }
154        }
155       
156        // 割り当てたページを閉じる
157        PDF_close_pdi_page($this->pdf, $page);
158        // 割り当てたドキュメントを閉じる
159        PDF_close_pdi($this->pdf, $doc);
160    }
161   
162    // ページを閉じる
163    function closePage() {
164        if($this->page_open) {
165            // ページを閉じる
166            PDF_end_page_ext($this->pdf, NULL);
167            $this->page_open = false;
168        }       
169    }
170   
171    // ページを開く
172    function openPage() {
173        if(!$this->page_open) {
174            // 新しいページを開く   
175            PDF_begin_page_ext($this->pdf, $this->width, $this->height, NULL);
176            $this->page_open = true;
177        }
178    }
179   
180    // 新しいページを開く
181    function newPage() {
182        PDF_end_page_ext($this->pdf, NULL);
183        PDF_begin_page_ext($this->pdf, $this->width, $this->height, NULL);
184    }
185   
186    // アクティブなページのサイズを取得する
187    function getSize() {
188        $this->openPage();
189        $x = PDF_get_value($this->pdf, 'pagewidth', 0);
190        $y = PDF_get_value($this->pdf, 'pageheight', 0);
191        return array($x, $y);
192    }
193   
194    // 座標を入れ替えて取得する(左下(0,0)を左上(0,0)に変換)
195    function posTopDown($x, $y) {
196        $width = 0;
197        $height = 0;
198        list($width, $height) = $this->getSize();
199        // x座標は、変更の必要なし
200        $pdf_x = $x;
201        $pdf_y = $height - $y;
202        return array($pdf_x, $pdf_y);
203    }
204   
205    // テーブルカラーの設定
206    function setTableColor($frame_color = "000000", $title_color = "F0F0F0", $line_color = "D1DEFE", $last_color = "FDCBFE") {
207        $this->frame_color = $frame_color;
208        $this->title_color = $title_color;
209        $this->line_color = $line_color;
210        $this->last_color = $last_color;
211    }
212   
213    // テーブルのグリッドを表示する。
214    function writeGrid($x, $y, $arrCol, $line_max, $last_color_flg = true) {
215        // テーブル幅
216        $max = count($arrCol);
217        $width = 0;
218        for($i = 0; $i < $max; $i++) {
219            $width += $arrCol[$i];
220        }
221       
222        if($this->title_enable) {
223            // タイトルグリッド描画
224            $this->writeFrameRect($x, $y + GRID_SPACE, $width + GRID_SPACE, $this->fontsize + GRID_SPACE, $this->title_color, $this->frame_color);
225        }
226       
227        // グリッド特殊指定あり
228        if(count($this->arrLines) > 0) {
229            $count = count($this->arrLines);
230            $pos = 0;
231            for($i = 0; $i < $count; $i++) {
232                if(($i % 2) != 0) {
233                    // 行の間隔
234                    $down = ($pos + 1) * $this->fontsize * 1.5;
235                    // 描画する縦幅を求める
236                    $height = ($this->fontsize + GRID_SPACE) * $this->arrLines[$i] + ($this->arrLines[$i] - 1);
237                    // 行グリッド描画
238                    $this->writeRect($x, $y + GRID_SPACE + $down, $width + GRID_SPACE, $height, $this->line_color);
239                }
240                $pos += $this->arrLines[$i];   
241            }                       
242        } else {
243            for($i = 1; $i <= $line_max; $i++) {
244                if(($i % 2) == 0) {
245                    // 行の間隔
246                    $down = $i * $this->fontsize * 1.5;
247                    // 行グリッド描画
248                    $this->writeRect($x, $y + GRID_SPACE + $down, $width + GRID_SPACE, $this->fontsize + GRID_SPACE, $this->line_color);
249                }
250            }
251            // 最終行に色をつける場合
252            if($last_color_flg) {
253                // 行の間隔
254                $down = $line_max * $this->fontsize * 1.5;
255                // 行グリッド描画
256                $this->writeRect($x, $y + GRID_SPACE + $down, $width + GRID_SPACE, $this->fontsize + GRID_SPACE, $this->last_color);
257            }
258        }
259    }
260   
261    // グリッド用のアンダーラインを引く
262    /*
263        $x          :テーブル開始位置X軸
264        $y          :テーブル開始位置Y軸
265        $arrCol     :カラムサイズの配列
266        $line       :アンダーラインを引く行
267        $start_col  :アンダーライン開始カラム(0:開始カラム)
268     */
269    function writeUnderLine($x, $y, $arrCol, $line, $start_col = 0) {
270        // テーブル幅
271        $max = count($arrCol);
272        $width = 0;
273        for($i = 0; $i < $max; $i++) {
274            $width += $arrCol[$i];
275        }
276       
277        $start_x = 0;
278        for($i = 0; $i < $start_col; $i++) {
279            $start_x += $arrCol[$i];
280        }
281       
282        // アンダーラインのY座標を求める
283        $down = ($line + 1) * $this->fontsize * 1.5;
284        // 行グリッド描画
285        $sx = $x + $start_x + GRID_SPACE + $this->table_left;
286        $sy = $y + GRID_SPACE + $down - 1;
287        $ex = $x + $width + GRID_SPACE;
288        $ey = $sy;
289               
290        $this->writeLine($sx, $sy, $ex, $ey);       
291    }
292   
293    // 真ん中横位置を求める
294    function getXCenter($width) {
295        $page_width = 0;
296        $page_height = 0;
297        list($page_width, $page_height) = $this->getSize();
298        $x = ($page_width - $width) / 2;
299        return $x;
300    }
301   
302    // 自動中央よせ
303    function writeTableCenter($table, $y, $arrCol, $arrAlign, $line_max = 256, $start_no = 1, $last_color_flg = false) {
304        // テーブルサイズ取得
305        $width = 0;
306        foreach($arrCol as $val) {
307            $width += $val;
308        }
309        // 中央よせ位置取得
310        $x = $this->getXCenter($width) + $this->table_left;
311        list($ret_x, $ret_y) = $this->writeTable($table, $x, $y, $arrCol, $arrAlign, $line_max, $start_no, $last_color_flg);
312        // X軸の座標を返す
313        return array($ret_x, $ret_y);
314    }
315   
316    // データの書き込み(closeすると次回新規ページ)
317    // $start_no:1行目(タイトル)を0とする。
318    // $line_max:タイトルを含まない行数
319    function writeTable($table, $x, $y, $arrCol, $arrAlign, $line_max = 256, $start_no = 1, $last_color_flg = false) {
320        $this->openPage();
321       
322        $table = ereg_replace("\n$", "", $table);
323               
324        $arrRet = split("\n", $table);
325                               
326        if($line_max > (count($arrRet) - $start_no)) {
327            $line_max = count($arrRet) - $start_no;
328        }
329       
330        // タイトル有効
331        if($this->grid_enable) {
332            // グリッドの描画
333            $this->writeGrid($x, $y, $arrCol, $line_max, $last_color_flg);
334        }
335       
336        // UnicodeエンコーディングとしてUTF-8を設定
337        PDF_set_parameter($this->pdf, "textformat", "utf8");
338       
339        // タイトル有効
340        if($this->title_enable) {
341            if(count($this->arrHeaderColSize) > 0 && count($this->arrHeaderAlign) > 0 ) {
342                list($linecol, $aligncol, $width) = $this->getTableOption($this->arrHeaderColSize, $this->arrHeaderAlign);
343            } else {
344                list($linecol, $aligncol, $width) = $this->getTableOption($arrCol, $arrAlign);
345            }   
346                       
347            // タイトル行の書き込み
348            $option = "ruler {" . $linecol . "} ";
349            $option.= "tabalignment {" . $aligncol . "} ";
350            $fontsize =  $this->fontsize;
351            $option.= "hortabmethod ruler leading=150% fontname=HeiseiKakuGo-W5 fontsize=$fontsize encoding=UniJIS-UCS2-H";
352           
353            $this->writeTableData($table, $x, $y, $width, 0, 0, $option);
354        }
355       
356        list($linecol, $aligncol, $width) = $this->getTableOption($arrCol, $arrAlign);
357       
358        // データ行の書き込み
359        $option = "ruler {" . $linecol . "} ";
360        $option.= "tabalignment {" . $aligncol . "} ";
361        $option.= "hortabmethod ruler leading=150% fontname=HeiseiMin-W3 fontsize=$this->fontsize encoding=UniJIS-UCS2-H";
362       
363        if($start_no <= 0) {
364            $start_no = 1;
365            $end_no = $line_max;
366        } else {
367            $end_no = $start_no + $line_max - 1;
368        }
369       
370        $y += $this->fontsize * 1.5;
371       
372        list($ret_x, $ret_y) = $this->writeTableData($table, $x, $y, $width, $start_no, $end_no, $option);
373       
374        return array($ret_x, $ret_y);
375    }
376   
377    function getTableOption($arrCol, $arrAlign) {
378        // カラムサイズ
379        $max = count($arrCol);
380        $width = 0;
381        for($i = 0; $i < $max; $i++) {
382            $width += $arrCol[$i];
383            $linecol.= $width . " ";
384        }
385       
386        // カラム位置
387        $max = count($arrAlign);
388        for($i = 0; $i < $max; $i++) {
389            $aligncol.= $arrAlign[$i] . " ";
390        }
391       
392        return array($linecol, $aligncol, $width);
393    }
394   
395    // テーブルデータの書き込み
396    function writeTableData($table, $x, $y, $table_width, $start_no, $end_no, $option) {
397        $arrLine = split("\n", $table);
398        for($i = $start_no; $i <= $end_no; $i++) {
399            $line.=$arrLine[$i] . "\n";
400        }
401               
402        // テーブル位置を求める
403        list($pdf_x, $pdf_y) = $this->posTopDown($x, $y);
404                       
405        // テーブル高さを求める
406        $table_height = $this->fontsize * 1.5 * ($end_no - $start_no + 1);
407        // テーブル右下のy座標を求める
408        $end_y = $pdf_y - $table_height;
409        if($end_y < 0) {
410            $end_y = 0;
411        }
412        $enc_table = mb_convert_encoding($line, "utf-8", CHAR_CODE);
413               
414        $tf = PDF_create_textflow($this->pdf, $enc_table, $option);
415
416        PDF_fit_textflow($this->pdf, $tf, $pdf_x, $pdf_y, $pdf_x + $table_width, $end_y, NULL);
417        PDF_delete_textflow($this->pdf, $tf);
418       
419        // テーブル左下座標を返す
420        return array($x, $y + $table_height);       
421    }
422       
423    // 色の設定
424    function setColor($rgb) {
425        if($rgb != "") {
426            list($r, $g, $b) = sfGetPdfRgb($rgb);
427            PDF_setcolor($this->pdf, "fillstroke", "rgb", $r, $g, $b, 0);   
428        }
429    }
430   
431    // 短形を描画
432    function writeRect($x, $y, $width, $height, $rgb = "") {
433        $this->openPage();
434        list($pdf_x, $pdf_y) = $this->posTopDown($x, $y);
435        $this->setColor($rgb);
436        PDF_rect($this->pdf, $pdf_x,$pdf_y,$width,-$height);
437        PDF_fill($this->pdf);
438    }
439   
440    // 枠付の短形を描画
441    function writeFrameRect($x, $y, $width, $height, $rgb, $frgb) {
442        $this->openPage();
443        list($pdf_x, $pdf_y) = $this->posTopDown($x, $y);
444        $this->setColor($frgb);
445        PDF_rect($this->pdf, $pdf_x,$pdf_y,$width,-$height);
446        PDF_fill($this->pdf);
447       
448        $this->setColor($rgb);
449        PDF_rect($this->pdf, $pdf_x+1,$pdf_y-1,$width-2,-$height+2);
450        PDF_fill($this->pdf);       
451    }
452   
453    // 直線を描画
454    function writeLine($sx, $sy, $ex, $ey, $rgb = "000000") {
455        $this->openPage();
456        list($pdf_sx, $pdf_sy) = $this->posTopDown($sx, $sy);
457        list($pdf_ex, $pdf_ey) = $this->posTopDown($ex, $ey);
458        $this->setColor($rgb);
459        PDF_setlinewidth($this->pdf, 1.0);
460        PDF_moveto($this->pdf, $pdf_sx, $pdf_sy);
461        PDF_lineto($this->pdf, $pdf_ex, $pdf_ey);
462        PDF_stroke($this->pdf);
463    }
464       
465    // ファイルのダウンロード
466    function output($filekey = "") {
467        if(isset($this->pdf)) {
468            // ページを閉じる
469            $this->closePage();
470            // PDFの終了
471            PDF_end_document($this->pdf, NULL);
472            // 出力用データの取得
473            $buf = PDF_get_buffer($this->pdf);
474            $filename = $filekey . date("ymdHis").".pdf";
475                       
476            header("Content-disposition: attachment; filename=$filename");
477            header("Content-type: application/octet-stream; name=$filename");
478                   
479            /*
480             * session_start()を事前に呼び出している場合に出力される以下のヘッダは、
481             * URL直接呼び出し時にエラーを発生させるので空にしておく。
482             *
483             * Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
484             * Progma: no-cache
485             *
486             */
487            header("Cache-Control: ");
488            header("Pragma: ");
489            print $buf;
490           
491            // PDF解放
492            PDF_delete($this->pdf);
493        } else {
494            print("PDFが生成されていません。");
495        }
496        exit;       
497    }
498   
499    // ファイルの表示
500    function display() {
501        if(isset($this->pdf)) {
502            // ページを閉じる
503            $this->closePage();
504            // PDFの終了
505            PDF_end_document($this->pdf, NULL);
506           
507            // 出力用データの取得
508            $buf = PDF_get_buffer($this->pdf);
509            $len = strlen($buf);
510            header("Content-type: application/pdf");
511            header("Content-Length: $len");
512            header("Content-Disposition: inline; filename=". date("YmdHis").".pdf");
513                               
514            /*
515             * session_start()を事前に呼び出している場合に出力される以下のヘッダは、
516             * URL直接呼び出し時にエラーを発生させるので空にしておく。
517             *
518             * Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
519             * Progma: no-cache
520             *
521             */
522            header("Cache-Control: ");
523            header("Pragma: ");
524            print $buf;
525           
526            // PDF解放
527            PDF_delete($this->pdf);
528        } else {
529            print("PDFが生成されていません。");
530        }
531        exit;
532    }
533}
534
535?>
Note: See TracBrowser for help on using the repository browser.