見本の一覧へ
納品書(伝票の形)
1 ページの明細の行の数を決め、足りない行は空の行で埋めます。金額はページごとに小計し、次のページへ繰り越します。ロゴ(SVG)・発行元・ページ番号は、全ページ共通の要素として 1 回だけ書きます。
- 明細の固定行数(空の行で埋める)
- ページごとの小計・繰越
- 全ページ共通のヘッダー・フッター
- SVG のロゴ
作れませんでした
下に出ているのは、前に作った PDF です。
エンジンからの警告(PDF はできています)
use PdfBooster\Builder\Color;
use PdfBooster\Builder\Document;
use PdfBooster\Builder\Table;
/**
* 納品書を、補助クラスのメソッドで組み立てる。$in は画面の入力(宛先・明細など)。
* 位置と大きさは mm(左上が原点)。ロゴ・発行元・ページ番号は everyPage() に書くと、続きのページにも置かれる。
*/
public static function build(array $in): Document
{
$doc = Document::a4()
->font('gothic', 'NotoSansJP-Regular.otf')
->font('ud', 'BIZUDGothic-Regular.ttf');
$gray = Color::gray(12);
// 明細の行と合計(金額は桁区切りを入れた文字列。ページごとの小計は、エンジンがこの文字を読んで足す)
$rows = [];
$subtotal = 0;
foreach (array_values($in['rows']) as $i => $r) {
$amount = (int) $r['qty'] * (int) $r['price'];
$subtotal += $amount;
$rows[] = [$i + 1, $r['name'], (int) $r['qty'], $r['unit'] ?? '', number_format((int) $r['price']), number_format($amount)];
}
$tax = intdiv($subtotal * 10, 100);
// 全ページに置くもの:ロゴ(SVG。中の文字は gothic で描く)・発行元・下の線・ページ番号
$every = $doc->everyPage();
if ($in['logo']) {
$every->image('logo.svg', 15, 10, 60, 16)->font('gothic');
}
if (($in['issuer'] ?? '') !== '') {
$every->text($in['issuer'], 115, 10, 80, 16)->size(7.5)->right()->wrap()->shrinkToFit(6);
}
$every->line(15, 281, 195, 281)->strokeWidth(0.3);
$every->text('{page} / {pages}', 15, 283, 180, 5)->size(8)->center();
// 1 ページ目:題名・宛先・納品日
$page = $doc->page();
$page->text('納 品 書', 15, 30, 180, 11)->size(20)->center()->middle();
$page->text($in['customer'].' 御中', 15, 46, 100, 8)->size(13)->bottom()->condenseToFit();
$page->line(15, 54.5, 110, 54.5)->strokeWidth(0.5);
$date = new DateTimeImmutable($in['date']);
$page->text('納品日:'.$date->format('Y年n月j日')."\n".'納品番号:'.$in['number'], 120, 46, 75, 9)->size(9)->right();
$page->text('下記のとおり納品いたしました。', 15, 58, 95, 5)->size(9);
// 明細の表。入りきらなければ、エンジンが続きのページを足す
$table = $page->table(15, 66, 180, 205)
->font('ud')->size(9)->minRowHeight(6.5)
->column(10, 'center')->column()->column(14, 'right')
->column(12, 'center')->column(22, 'right')->column(26, 'right')
->header(['No.', '品名', '数量', '単位', '単価', '金額'])
->headerFill($gray)
->stripe(Color::gray(4))
->rows($rows)
->footer(['', Table::cell('合計(税抜)')->colspan(4), number_format($subtotal)])
->footer(['', Table::cell('消費税(10%)')->colspan(4), number_format($tax)])
->footer(['', Table::cell('合計(税込)')->colspan(4)->fill($gray), Table::cell(number_format($subtotal + $tax))->fill($gray)])
->lines('all', 0.4);
if ((int) $in['rows_per_page'] > 0) {
// 1 ページの明細の行の数。足りないページは空の行で埋める
$table->rowsPerPage((int) $in['rows_per_page']);
}
if ($in['totals']) {
// 金額の列(左から 0 で数えて 5 列目)をページごとに足し、小計・次頁へ繰越・前頁より繰越の行を置く
$table->pageTotals([5], labelColumn: 1, options: ['fill' => Color::gray(6)]);
}
// 続きのページ:上の余白を詰めて、題名(続き)と宛先を置く
$table->continuedBox(15, 40, 180, 231);
$table->continued()->text('納 品 書(続き) '.$in['customer'].' 御中', 15, 31, 180, 6)->size(10)->condenseToFit();
return $doc;
}
$in は左の欄の入力です。$doc->toArray() が「渡した JSON」と同じ内容の配列になり、$doc->render($renderer) で PDF になります(「PHP では」)。
補助クラスは PHP 版に付いています(PHP 8.2 以上。Composer でも、require 1 行でも読み込めます)。配列で書くか、メソッドで書くかは好みで選べます。
「渡した JSON」と同じ形の配列($job)を渡すだけです。JSON の文字列のままでも、「メソッドで書く」で組み立てた $doc からでも作れます。
// フォントを置いたフォルダを渡す(ジョブの fonts の file はここからの場所)
$renderer = new \PdfBooster\Renderer(__DIR__.'/fonts');
$pdf = $renderer->render($job); // PDF のバイト列が返る
// $pdf = $doc->render($renderer); // メソッドで組み立てたときは、こう書いてもよい
file_put_contents('slip.pdf', $pdf);
// はみ出し・細すぎるバーなどは、例外ではなく警告として返る
foreach ($renderer->getWarnings() as $warning) {
error_log($warning);
}
入力の誤り(チェックデジットの違い、知らない項目名など)は \PdfBooster\PdfBoosterException になり、どこが違うかを日本語で返します。