Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
39
Help us understand the problem. What is going on with this article?

More than 3 years have passed since last update.

@noratmt

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

この記事の続きです。

環境

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にクラス化しておくと良い感じ。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
39
Help us understand the problem. What is going on with this article?