set_option("errorpolicy=return"); $p->set_option("searchpath={" . $searchpath . "}"); $p->set_option("stringformat=utf8"); if ($p->begin_document($outfilename, "") == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", "Starter Table"); /* -------------------- テーブルにセルを追加する -------------------- */ /* ---------- 1行目: テーブルセル (全ての列にまたがる) */ $row = 1; $col = 1; $font = $p->load_font("NotoSerif-Bold", "unicode", ""); if ($font == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } $optlist = "fittextline={position=center font=" . $font . " fontsize=14} " . "colspan=" . $colmax; $tbl = $p->add_table_cell($tbl, $col, $row, $headertext, $optlist); if ($tbl == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } /* ---------- 2行目: 様々な種類のコンテンツ */ /* -----シンプルなテキストセル */ $row++; $col=1; $optlist = "fittextline={font=" . $font . " fontsize=10 orientate=west}"; $tbl = $p->add_table_cell($tbl, $col, $row, "vertical line", $optlist); if ($tbl == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } /* ----- 色付きの背景 */ $col++; $optlist = "fittextline={font=" . $font . " fontsize=10} " . "matchbox={fillcolor={rgb 0.9 0.5 0}}"; $tbl = $p->add_table_cell($tbl, $col, $row, "some color", $optlist); if ($tbl == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } /* ----- テキストフローを使用した複数行テキスト */ $col++; $font = $p->load_font("NotoSerif-Regular", "unicode", ""); if ($font == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } $optlist = "charref fontname=NotoSerif-Regular encoding=unicode fontsize=8 "; $tf = $p->add_textflow($tf, $tf_text, $optlist); if ($tf == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } $optlist = "margin=2 textflow=" . $tf; $tbl = $p->add_table_cell($tbl, $col, $row, "", $optlist); if ($tbl == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } /* ----- 回転したイメージ */ $col++; $image = $p->load_image("auto", $imagefile, ""); if ($image == 0) { echo("Couldn't load $image: " . $p->get_errmsg()); exit(1); } $optlist = "image=" . $image . " fitimage={orientate=west}"; $tbl = $p->add_table_cell($tbl, $col, $row, "", $optlist); if ($tbl == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } /* ----- 斜めのスタンプ */ $col++; $optlist = "fittextline={font=" . $font . " fontsize=10 stamp=ll2ur}"; $tbl = $p->add_table_cell($tbl, $col, $row, "entry void", $optlist); if ($tbl == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } /* ---------- 3行目は、行番号・列番号でセルを埋める */ for ($row++; $row <= $rowmax; $row++) { for ($col = 1; $col <= $colmax; $col++) { $num = "Col " . $col . "/Row " . $row; $optlist = "colwidth=20% fittextline={font=" . $font . " fontsize=10}"; $tbl = $p->add_table_cell($tbl, $col, $row, $num, $optlist); if ($tbl == 0) { echo("Error: " . $p->get_errmsg()); exit(1); } } } /* ---------- テーブルをページ上に配置する ---------- */ /* * 全てのテーブルを配置するまで繰り返す; テーブルインスタンスを配置する * 必要がある限り、新しいページを生成する */ do { $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* 1 行おきに塗りつぶす; すべてのテーブルセルに線を引く。 * セルの境界線を視覚化するには、"showcells showborder" を追加する。 */ $optlist = "header=1 rowheightdefault=auto " . "fill={{area=rowodd fillcolor={gray 0.9}}} " . "stroke={{line=other}} "; /* テーブルインスタンスを配置する */ $result = $p->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist); if ($result == "_error") { echo("Couldn't place table: " . $p->get_errmsg()); exit(1); } $p->end_page_ext(""); } while ($result == "_boxfull"); /* 結果をチェック。_stop はすべてOKを意味する */ if ($result != "_stop") { if ($result == "_error") { echo("Error when placing table: " . $p->get_errmsg()); exit(1); } else { /* * それ以外の戻り値はすべて専用のコードで扱う必要がある */ echo("User return found in Table"); exit(1); } } /* テーブルで使用されている テキストフローハンドルも削除する */ $p->delete_table($tbl, ""); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=starter_table.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred in starter_table sample:\n" . "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Exception $e) { echo($e); exit(1); } $p = 0; ?>