LoginSignup
19
40

More than 5 years have passed since last update.

PHPでTCPDFを使ってHTMLをPDF化する

Posted at

この記事の続きです。

環境

Mac OS X El Capitan 10.11.4
PHP 5.6.24
CodeIgniter 2.2.0(古い。が、他のFWでも参考になるかと思います)

ライブラリのインストール

composer require tecnickcom/tcpdf
requireを付けてcomposer.jsonにも追加。

実装

// composerでインストールしたライブラリを参照する
require_once('./vendor/tecnickcom/tcpdf/tcpdf.php');

// 用紙の方向、用紙サイズを指定する
$tcpdf = new TCPDF('L', "mm",'A4');
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(false);
$tcpdf->AddPage();

// CodeIgniterのviewの第三引数をtrueにしてhtml文字列として扱う
$html = $this->load->view('open_position', $this->data, true);

$tcpdf->WriteHTML($html, true, 0, false, true, 'L');
// 出力用バッファの内容を消去
ob_end_clean();
$tcpdf->Output($file_name, "D");

CodeIgniterらしくapplication/librariesにクラス化しておくと良い感じ。

19
40
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
40