PHPでPDF出力 ~ dompdfを利用した「ええ感じ」のサンプル(htmlを利用)
dompdfの準備
プロジェクト用ディレクトリ作成
mkdir pdf-example2
ディレクトリに移動
cd pdf-example2
composerの準備
composer init
dompdfのインストール
composer require dompdf/dompdf
load_font.phpの準備
curl https://raw.githubusercontent.com/dompdf/utils/master/load_font.php --output load_font.php
IPAフォントのダウンロードと解凍
curl https://ipafont.ipa.go.jp/IPAfont/IPAfont00303.zip --output IPAfont00303.zip
unzip IPAfont00303.zip
フォントの追加
php load_font.php ipag IPAfont00303/ipag.ttf php load_font.php ipagp IPAfont00303/ipagp.ttf php load_font.php ipam IPAfont00303/ipam.ttf php load_font.php ipamp IPAfont00303/ipamp.ttf
画像埋め込みの確認用に猫の画像を検索してダウンロードしておく
ここで猫の画像を検索 visualhunt.com
名前はcat.phpにしよう。
phpファイル作成
imgタグはどうもネットワーク経由で取得しているらしく、URLが必要だった。 ローカルのファイルを利用する場合は、base64を利用することでうまくいく。 今回はローカルファイルで対応した為、base64で事前に読み込んでおく。
<?php require_once "vendor/autoload.php"; use Dompdf\Dompdf; $img = base64_encode(file_get_contents("cat.jpg")); $html = <<< EOF <style> body { font-family: ipag; } .td { border: solid 1px black; } .td2 { border: solid 1px black; height:100px; } .td3 { font-size:20px; border: solid 1px black; height:100px; } .table{ width:100%; border-collapse: collapse; border-spacing: 0px; } .left{ text-align: left; border: solid 1px black; height:30px; } .right{ text-align: right; border: solid 1px black; height:30px; } .center{ text-align: center; border: solid 1px black; height:30px; } .decoration{ text-decoration: line-through; border: solid 1px black; } .sample1 {text-decoration: none;} .sample2 {text-decoration: underline;} .sample3 {text-decoration: overline;} .sample4 {text-decoration: line-through;} .sample5 {text-decoration: underline dotted red;} .color{ background-color:black; color:white; } </style> <table class="table"> <tr> <td class="left">text-align: left;</td> <td class="center">text-align: center;</td> <td class="right">text-align: right;</td> </tr> <tr> <td class="td">test</td> <td class="td">日本語</td> <td class="td">hoge</td> </tr> <tr> <td class="td">test</td> <td class="td"><table class="table"> <tr> <td class="td" >test</td> </tr> <tr> <td class="td" >test</td> </tr> </table></td> <td class="td">hoge</td> </tr> <tr> <td class="td" colspan="2">colspan="2"</td> <td class="td" rowspan="2">rowspan="2"</td> </tr> <tr> <td class="td">test</td> <td class="td">fuga</td> </tr> <tr> <td class="td2" colspan="3">height:100px; colspan="3"</td> </tr> <tr> <td class="td2 color" colspan="3">改行<br> brタグ<br> で改行<br> 背景色変更<br> フォントカラー変更<br> </td> </tr> <tr> <td class="td3" colspan="3">改行<br> brタグ<br> で改行<br> </td> </tr> <tr> <td class="td2" colspan="3"> <div><span class="sample1">none</span> text-decoration: none;</div> <div><span class="sample2">underline</span> text-decoration: underline;</div> <div><span class="sample3">overline</span> text-decoration: overline;</div> <div><span class="sample4">line-through</span> text-decoration: line-through;</div> <div><span class="sample5">underline dotted red</span> text-decoration: underline dotted red;</div> </td> </tr> <tr> <td class="td2" colspan="3"> 画像挿入 <br> <div> <img src="data:image/jpeg;base64,{$img}" width="300px"/> </div> <br> </td> </tr> </table> EOF; $dompdf = new Dompdf(); $dompdf->loadHtml($html); $options = $dompdf->getOptions(); $options->set(array('isRemoteEnabled' => false)); $dompdf->setOptions($options); $dompdf->setPaper('A4', 'portrait'); $dompdf->render(); $dompdf->stream("example.pdf", array("Attachment" => true));
結果
その他
tcpdfはこちら
tcpdfバージョンのエントリは以下より。
wkhtmltopdfはこちら
wkhtmltopdfバージョンのエントリは以下より。