set_option("searchpath={" . $searchpath . "}"); /* load_font() 等の戻り値を確認する */ $p->set_option("errorpolicy=return"); if ($p->begin_document($outfile, "") == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", $title ); /* ---------------------------------------------------- * 左寄せの番号リストを作成 * ---------------------------------------------------- */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* テキストフローを作成する。初めに見出しを追加する */ $tf = $p->add_textflow($tf, "Page 1: Numbered list with the numbers " . "left-aligned", $head_optlist); /* テキストのための一般的なオプションを定義する */ $tf = $p->add_textflow($tf, "", "fontname=NotoSerif-Regular fontsize=12 charref " . "alignment=justify leading=140% "); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* 番号リストと、リストアイテム(テキスト)を追加する。 * "leftindent=0%"と"leftindent=10%"オプションを使用し、テキストフローの * はめ込み枠の幅の0% または 10% のインデントを作成する。 */ $num_optlist = "fillcolor={cmyk 1 0.5 0.2 0} leftindent=0%"; $item_optlist = "fillcolor={gray 0} leftindent=10%"; for ($i = 0; $i < 4; $i++) { $tf = $p->add_textflow($tf, "\n\n" . $numbers[$i], $num_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); $tf = $p->add_textflow($tf, $items[$i], $item_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); } /* テキストフローを配置する */ $result = $p->fit_textflow($tf, $llx, $lly, $urx, $ury, ""); if ($result != "_stop") { /* Check for further action */ } $p->delete_textflow($tf); $p->end_page_ext(""); /* ----------------------------------------------------- * 右寄せの番号リストを作成 * ----------------------------------------------------- */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); $tf = 0; /* テキストフローを作成する。初めに見出しを追加する */ $tf = $p->add_textflow($tf, "Page 2: Numbered list with the numbers " . "right-aligned", $head_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* 一般的なテキストのオプションを定義する。 * * タブの設定を定義する。テキストフローのはめ込み枠の幅の3%で、タブの * 位置を指定するには "hortabmethod=ruler" と"ruler=3%" を使用する。 * タブ位置を右揃えにするには "tabalignment=right" を使用する。 */ $tf = $p->add_textflow($tf, "", "fontname=NotoSerif-Regular fontsize=12 charref " . "alignment=justify leading=140% " . "hortabmethod=ruler ruler=3% tabalignment=right"); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* 番号リストのオプションを定義する */ $num_optlist = "fillcolor={cmyk 1 0.5 0.2 0} leftindent=0%"; /* テキスト用のオプションリスト */ $item_optlist = "fillcolor={gray 0} leftindent=10%"; /* 番号リストとテキストをテキストフローに追加する */ for ($i = 0; $i < 4; $i++) { $tf = $p->add_textflow($tf, "\n\n\t" . $numbers[$i], $num_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); $tf = $p->add_textflow($tf, $items[$i], $item_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); } /* テキストフローを配置する */ $result = $p->fit_textflow($tf, $llx, $lly, $urx, $ury, ""); if ($result!= "_stop") { /* チェックを行う */ } $p->delete_textflow($tf); $p->end_page_ext(""); /* ----------------------------------- * マクロを使用して番号リストを作成する * ----------------------------------- */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); $tf = 0; /* 見出しを追加する */ $p->fit_textline("Page 3: Numbered list using macros of inline options", $llx, $ury-17, $head_optlist); /* インデント値の設定と解除は、段落ごとに必要なため面倒である。 * 解決策は"list "というマクロを定義することである。 * 便宜上"indent "というマクロを定義し、これを定数として使用する。 * * "leftindent "オプションは、左余白からの距離を指定する。 * "parindent "オプションには、負の値が設定されており、 * 各段落の先頭行のインデントを解除する。 * * オプション "hortabsize"、"hortabmethod"、"ruler "は、"leftindent "に * 対応したタブ位置を指定している。 * これらのオプションは、"leftindent "に対応するタブ位置を指定している。 * このため、番号の後のテキストは、leftindent で指定した分だけ * インデントされるようになる。 */ $text = "<&list>" . "1.\tLong Distance GliderWith this paper rocket you " . "can send all your messages even when sitting in a hall or in " . "the cinema pretty near the back.\n\n" . "2.\tGiant WingAn unbelievable sailplane! It is " . "amazingly robust and can even do aerobatics. But it is best " . "suited to gliding.\n\n" . "3.\tCone Head RocketThis paper arrow can be thrown " . "with big swing. We launched it from the roof of a hotel. It " . "stayed in the air a long time and covered a considerable " . "distance.\n\n" . "4.\tSuper DartThe super dart can fly giant loops " . "with a radius of 4 or 5 meters and cover very long distances. " . "Its heavy cone point is slightly bowed upwards to get the lift " . "required for loops."; $optlist = "fontname=NotoSerif-Regular fontsize=12 " . "fillcolor={gray 0} alignment=justify leading=140%"; $tf = $p->create_textflow($text, $optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* テキストフローを配置する */ $result = $p->fit_textflow($tf, $llx, $lly, $urx, $ury-34, ""); if ($result != "_stop") { /* チェックを行う */ } $p->delete_textflow($tf); $p->end_page_ext(""); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=numbered_list.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred:\n". "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Throwable $e) { echo("PHP exception occurred: " . $e->getMessage() . "\n"); exit(1); } $p = 0; ?>