CodeIgniter 2.0.1ja以上2.0.3jaなら全部OK。
2.0.1jaでインストールして今2.0.3jaで使ってるから
- TCPDFのダウンロード
http://sourceforge.net/projects/tcpdf/files/
ここから最新のzip アーカイブをダウンロードする。 - CodeIgniterへのインストール
執筆時点のバージョンは5.9.158なんで
tcpdf_5_9_158.zipをダウンロード。
ダウンロードしたファイルを展開。
展開すると「tcpdf」フォルダがあります。この「tcpdf」フォルダを「/application/third_party」フォルダにコピーします。
今回は「third_party」へのコピーという方法をとっていますが、「/application/libraries」にコピーする方法もあるかと - ユーザーライブラリの作成
「/application/libraries」にユーザーライブラリファイルを作成。
「pdf.php」というファイルを追加しました。
ファイルの内容は次のとおり1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950<?php (defined('BASEPATH')) OR exit('No direct script access allowed');# include TCPDFrequire_once(APPPATH.'third_party/tcpdf/config/lang/jpn.php');require_once(APPPATH.'third_party/tcpdf/tcpdf.php');/*** TCPDF - CodeIgniter Integration*/class pdf extends TCPDF {/*** Initialize**/function __construct($params = array()){$orientation = 'P';$unit = 'mm';$format = 'A4';$unicode = true;$encoding = 'UTF-8';$diskcache = false;if (isset($params['orientation'])) {$orientation = $params['orientation'];}if (isset($params['unit'])) {$unit = $params['unit'];}if (isset($params['format'])) {$format = $params['format'];}if (isset($params['encoding'])) {$encoding = $params['encoding'];}if (isset($params['diskcache'])) {$diskcache = $params['diskcache'];}# initialize TCPDFparent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache);}}// END pdf Class/* End of file pdf.php *//* Location: ./application/libraries/pdf.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 |
class samplepdf extends CI_Controller { function __construct() { parent::__construct(); } function index() { $this->printPdf(); } private function printPdf() { // PDFライブラリ呼出 $this->load->library('pdf'); // ページ向き(横) $pageOrientation = 'L'; // ページフォーマット $pageFormat = 'A4'; $pdf = new TCPDF($pageOrientation, 'pt', $pageFormat, true, 'UTF-8', false); ここにTCPDFのロジック $pdf->Close(); $pdf->Output("ファイル名".'.pdf','I'); exit; } } |