LoginSignup
2
1

More than 3 years have passed since last update.

wkhtmltopdf+snappyでヘッダー(フッター)をすべてのページに表示させる方法

Posted at

phpでpdfを作成するときに、ヘッダーをすべてのページに入れる必要があった。
wkhtmltopdf&snappyのオプションを使ってできるとのことだったので実装してみた。

結論

下記のoptionを指定する。

文字を入れる場合
・header-right
・header-left
・header-center

htmlを入れる場合
・header-html
・footer-html
をオプションで指定する。

サンプル

// htmlファイルを読み込む ※htmlファイルを読み込むことも可
$html = '<EOF
<div>
    <table>
        <tr>
            <th>hoge</th>
            <th>hoge2</th>
            <th>hoge3</th>
            <th>hoge4</th>
            <th>hoge5</th>
        </tr>
        <tr>
            <td>hogehoge</td>
            <td>hogehoge2</td>
            <td>hogehoge3</td>
            <td>hogehoge4</td>
            <td>hogehoge5</td>
        </tr>
    </table>
</div>
<style>
    table, tr, th, td {
        border: 1px solid #4c4c4c;
    }
    table {
        border-collapse: collapse;
    }
    th {
        background: #0a8aa1;
        color: #fff;
    }
</style>';

// 保存するディレクトリを指定
$tmpDir = $tmpDir . date("Ymd")."_application.pdf";

// wkhtmltopdfの位置をコンストラクタに渡す
$pdf = new Knp\Snappy\Pdf('/usr/local/bin/wkhtmltopdf', [], ["LANG" => "ja_JP.UTF8"]);

// optionをセット
$options = [
    'encoding'                => 'utf-8',
    'page-size'               => 'A4',
    'orientation'             => 'portrait',
    'margin-top'              => '5',
    'margin-right'            => '10',
    'margin-bottom'           => '5',
    'margin-left'             => '10',
    'disable-smart-shrinking' => true,
];
$pdf->setOptions($options);
$pdf->generateFromHtml($html, $tmpDir, []);

ページ番号を表示したい場合など文字列を表示する場合

optionsにheader-right(右上) / header-left(左上) / header-center(上真ん中)
のいずれかを指定するが早い。

$options = [
    'encoding'                => 'utf-8',
    'page-size'               => 'A4',
    'orientation'             => 'portrait',
    'margin-top'              => '10',
    'margin-right'            => '10',
    'margin-bottom'           => '5',
    'margin-left'             => '10',
    'disable-smart-shrinking' => true,
    'header-right'            => "[page] / [toPage]", // ←これで右上にページ番号が自動的に入るようになる。
];

組んだhtmlファイルを表示する場合

$header = <<<HTML
<!DOCTYPE html>
<html>
  <head><style type="text/css">p { color: #FF0000; }</style></head>
  <body><p>Lorem ipsum</p></body>
</html>
HTML;

// optionをセット
$options = [
    'encoding'                => 'utf-8',
    'page-size'               => 'A4',
    'orientation'             => 'portrait',
    'margin-top'              => '5',
    'margin-right'            => '10',
    'margin-bottom'           => '5',
    'margin-left'             => '10',
    'disable-smart-shrinking' => true,
     'header-html'            => $header, 
];

引っかかったこと

①ヘッダーのHTMLが表示されない
→HTMLの改行を消したらできた。

wkhtmltopdfの公式サイト

▼wkhtmltopdf
https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

▼snappy
https://github.com/KnpLabs/snappy

困ったらdoc→FAQを読んだほうが早い。

2
1
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
2
1