<?php
/* データファイルの場所です。必要なように適合させます */
$searchpath = "../data";
$imagefile = "nesrin.jpg";
$tf=0; $tbl=0;
$rowmax = 50; $colmax = 5;
$llx= 50; $lly=50; $urx=550; $ury=800;
$headertext = "テーブルヘッダー(全ての列の中心)";
$text1 = "縦文字";
$text2 = "同色";
$text3 = "空文字";
$str_col = "列目";
$str_row = "行目";
/* マルチラインなTextflowのセルのダミーテキスト */
$tf_text =
"PDFlibは、Adobe PDF(Portable Document Format)形式ファイルをプログラ
ムで生成するためのプログラムライブラリーです。文書を作るイメージで手
順に従っ て関数を呼んでいけばPDF文書を生成するプログラムを容易に作成
することができます。PDFlibはさまざまなOS上のさまざまな言語をサポート
しているので、き っと日頃お使いになっている開発環境を使ってPDFを作成
することができるでしょう。";
$p = new PDFlib();
/* load_font()等から返る値を調べなければいけないことを表しています */
$p->set_parameter("errorpolicy", "return");
$p->set_parameter("SearchPath", $searchpath);
/* テキストフォーマットに"utf8"を使用し、
* Unicodeのエンコードを許可します。
*/
$p->set_parameter("textformat", "utf8");
if ($p->begin_document("", "") == 0) {
die("Error: " . $p->get_errmsg());
}
/* SJISからUTF-8へエンコーディングします */
$headertext = mb_convert_encoding($headertext, "UTF-8", "SJIS");
$text1 = mb_convert_encoding($text1, "UTF-8", "SJIS");
$text2 = mb_convert_encoding($text2, "UTF-8", "SJIS");
$text3 = mb_convert_encoding($text3, "UTF-8", "SJIS");
$str_col = mb_convert_encoding($str_col, "UTF-8", "SJIS");
$str_row = mb_convert_encoding($str_row, "UTF-8", "SJIS");
$tf_text = mb_convert_encoding($tf_text, "UTF-8", "SJIS");
$font = $p->load_font("HeiseiMin-W3", "UniJIS-UCS2-H", "");
if ($font == 0) {
die("Error: " . $p->get_errmsg());
}
$p->set_info("Creator", "PDFlib starter sample");
$p->set_info("Title", "starter_table");
/* -------------------- テーブルセルを追加 -------------------- */
/* ---------- 行1: テーブルヘッダー (全ての行を測る) */
$row = 1; $col = 1;
$optlist = "fittextline={position=center font=" . $font .
" fontsize=14} colspan=" . $colmax;
$tbl = $p->add_table_cell($tbl, $col, $row, $headertext, $optlist);
if ($tbl == 0) {
die("Error: " . $p->get_errmsg());
}
/* ---------- 行2: 様々な種類の内容 */
/* ----- 簡単なテキストセル */
$row++; $col=1;
$font = $p->load_font("HeiseiMin-W3", "UniJIS-UCS2-H", "");
if ($font == 0) {
die("Error: " . $p->get_errmsg());
}
$optlist = "fittextline={font=" . $font . "
fontsize=10 orientate=west}";
$tbl = $p->add_table_cell($tbl, $col, $row, "$text1", $optlist);
if ($tbl == 0) {
die("Error: " . $p->get_errmsg());
}
/* ----- 背景の配色 */
$col++;
$optlist = "fittextline={font=" . $font . " fontsize=10} " .
"matchbox={fillcolor={rgb 0.9 0.5 0}}";
$tbl = $p->add_table_cell($tbl, $col, $row, "$text2", $optlist);
if ($tbl == 0) {
die("Error: " . $p->get_errmsg());
}
/* ----- Textflowのマルチラインテキスト */
$col++;
/* テキストの長さを取得します */
$length = strlen($tf_text);
$optlist = "charref fontname=HeiseiMin-W3 encoding=UniJIS-UCS2-H " .
"fontsize=8 font=" . $font . " textlen=" . $length ;
$tf = $p->add_textflow($tf, $tf_text, $optlist);
if ($tf == 0) {
die("Error: " . $p->get_errmsg());
}
$optlist = "margin=2 textflow=" . $tf;
$tbl = $p->add_table_cell($tbl, $col, $row, "", $optlist);
if ($tbl == 0) {
die("Error: " . $p->get_errmsg());
}
/* ----- 回転イメージ */
$col++;
$image = $p->load_image("auto", $imagefile, "");
if ($image == 0) {
die("Couldn't load $image: " . $p->get_errmsg());
}
$optlist = "image=" . $image . " fitimage={orientate=west}";
$tbl = $p->add_table_cell($tbl, $col, $row, "", $optlist);
if ($tbl == 0) {
die("Error: " . $p->get_errmsg());
}
/* ----- 対角線の種類 */
$col++;
$optlist = "fittextline={font=" . $font . " fontsize=10 stamp=ll2ur}";
$tbl = $p->add_table_cell($tbl, $col, $row, "$text3", $optlist);
if ($tbl == 0) {
die("Error: " . $p->get_errmsg());
}
/* ---------- 3行目とそれらの数字より上で満たす */
for ($row++; $row <= $rowmax; $row++) {
for ($col = 1; $col <= $colmax; $col++) {
$num = $col . $str_col . " / " . $row . $str_row;
$optlist = "colwidth=20% fittextline={font=" . $font .
" fontsize=10}";
$tbl = $p->add_table_cell($tbl, $col, $row, $num, $optlist);
if ($tbl == 0) {
die("Error: " . $p->get_errmsg());
}
}
}
/* ---------- 一つかそれ以上のページにテーブルを配置 ---------- */
/* 全てのテーブルが配置されるまでループし、それ以上の
* テーブル段階が配置を必要とする間に新しいページを作っていきます。
*/
do {
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
/* 他の全ての行が変化し、全てのテーブルセルに線を描きます
* "showcells showborder"をセルの境界を視覚化するために
* 追加します。
*/
$optlist = "header=1 fill={{area=rowodd fillcolor={gray 0.9}}} " .
"stroke={{line=other}} ";
/* テーブルを配置します */
$result = $p->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist);
if ($result == "_error") {
die("Couldn't place table: " . $p->get_errmsg());
}
$p->end_page_ext("");
} while ($result == "_boxfull");
/* 結果を調べる。"_stop"は全てが良いことを表します */
if ($result != "_stop") {
if ($result == "_error") {
die("Error when placing table: " . $p->get_errmsg());
} else {
/* 他のいくつかの戻り値は、"return"オプションによって
* ユーザーが終了を引き起こしたもので、処理をする
* 精巧なコードを必要とします。
*/
die("User return found in Textflow");
}
}
/* テーブルで使われたTextflowのハンドルも消去されるでしょう*/
$p->delete_table($tbl, "");
$p->end_document("");
/* PDFデータをバッファから取り出す */
$buf = $p->get_buffer();
$len = strlen($buf);
/* httpヘッダを生成する */
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=starter_block.pdf");
print $buf;
$p->delete();
?>
|