set_option("searchpath={" . $searchpath . "}"); /* load_font() 等の戻り値を確認する */ $p->set_option("errorpolicy=return"); /* 出力PDF文書を開く。第1パラメータには出力ファイル名を指定 */ if ($p->begin_document($outfile, "") == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", $title); /* フォントをロードする */ $font = $p->load_font("NotoSerif-Regular", "unicode", ""); if ($font == 0) throw new Exception("Error: " . $p->get_errmsg()); /* 出力PDFページをA4サイズで作成する(1ページ目) */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* フォントと、フォントサイズを指定する */ $p->setfont($font, 10); /* 出力するテキストを指定する */ $text = "Our paper planes are the ideal way of passing the time. We " . "offer revolutionary new developments of the traditional common " . "paper planes. If your lesson, conference, or lecture turn out " . "to be deadly boring, you can have a wonderful time with our " . "planes. All our models are folded from one paper sheet."; /* Option list to create the Textflow. It is similar in all cases. * The leading is set to 120% and the alignment to justify. */ $cr_optlist = "fontname=NotoSerif-Regular fontsize=14 " . "leading=120% alignment=justify"; /* ---------------------------------------------- * Case 1: テキストをはめ込み枠に上詰めで表示する * ---------------------------------------------- */ /* 説明用のテキストを出力 */ $p->fit_textline("Case 1: Fit Textflow with default settings", $xtext, $ytext, ""); /* テキストフローオブジェクトを作成する */ $tf = $p->create_textflow($text, $cr_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* オプションリストを設定する * "showborder" はめ込み枠の境界線を表示 * "verticalalign=top" はめ込み枠に上詰めで表示(デフォルト値のため省略) * "firstlinedist=leading" はめ込み枠上端から、テキスト先頭行のベースラ * インまでの行送り値を、文字サイズにより決定し適用(デフォルト値のため * 省略) */ $fit_optlist = "showborder"; $result = $p->fit_textflow($tf, $x, $y, ($x + $boxwidth), ($y + $boxheight), $fit_optlist); if (!$result == "_stop") { /* エラーかどうかをチェックする */ } $p->delete_textflow($tf); /* ------------------------------------------------------------------- * Case 2: テキストをはめ込み枠に上詰めで表示する。テキスト先頭行は、 * はめ込み枠の上端から指定した間隔で表示する。 * ------------------------------------------------------------------- */ $y -= $yoffset; $ytext = $y + $boxheight/2; /* 説明用のテキストを出力 */ $p->fit_textline("Case 2: firstlinedist=capheight", $xtext, $ytext, ""); /* テキストフローオブジェクトを作成する */ $tf = $p->create_textflow($text, $cr_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* オプションリストを設定する * "showborder" はめ込み枠の境界線を表示 * "verticalalign=top" はめ込み枠に上詰めで表示(デフォルト値のため省略) * "firstlinedist=capheight" はめ込み枠上端と、テキスト先頭行の大文字 * 最上部のラインが接するように表示 */ $fit_optlist = "showborder firstlinedist=capheight"; $result = $p->fit_textflow($tf, $x, $y, ($x + $boxwidth), ($y + $boxheight), $fit_optlist); if (!$result == "_stop") { /* エラーかどうかをチェックする */ } $p->delete_textflow($tf); /* -------------------------------------------------- * Case 3: テキストをはめ込み枠に下詰めで表示する * -------------------------------------------------- */ $y -= $yoffset; $ytext = $y + $boxheight/2; /* 説明用のテキストを出力 */ $p->fit_textline("Case 3: verticalalign=bottom", $xtext, $ytext, ""); /* テキストフローオブジェクトを作成する */ $tf = $p->create_textflow($text, $cr_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* オプションリストを設定する * "showborder" はめ込み枠の境界線を表示 * "verticalalign=bottom" はめ込み枠に下詰めで表示 */ $fit_optlist = "showborder verticalalign=bottom"; $result = $p->fit_textflow($tf, $x, $y, ($x + $boxwidth), ($y + $boxheight), $fit_optlist); if (!$result == "_stop") { /* エラーかどうかをチェックする */ } $p->delete_textflow($tf); /* ----------------------------------------------------------------- * Case 4: テキストをはめ込み枠に下詰めで表示する。テキスト最終行は * はめ込み枠の下端から指定した間隔で表示する。 * ----------------------------------------------------------------- */ $y -= $yoffset; $ytext = $y + $boxheight/2; /* 説明用のテキストを出力 */ $p->fit_textline("Case 4: verticalalign=bottom lastlinedist=descender", $xtext, $ytext, ""); /* テキストフローオブジェクトを作成する */ $tf = $p->create_textflow($text, $cr_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* オプションリストを設定する * "showborder" はめ込み枠の境界線を表示 * "verticalalign=bottom" はめ込み枠内に下詰めで表示 * "lastlinedist=descender" テキスト最終行のフォントのディセンダの最下 * 部のラインがはめ込み枠の下端に接するように表示 */ $fit_optlist = "showborder verticalalign=bottom lastlinedist=descender"; $result = $p->fit_textflow($tf, $x, $y, ($x + $boxwidth), ($y + $boxheight), $fit_optlist); if (!$result == "_stop") { /* エラーかどうかをチェックする */ } $p->delete_textflow($tf); $p->end_page_ext(""); /* 出力PDFページをA4サイズで作成する(2ページ目) */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* フォントと、フォントサイズを指定する */ $p->setfont($font, 10); $y = $ystart; $yoffset = 230; $ytext = $y + $boxheight/2; /* ------------------------------------------------ * Case 5: テキストをはめ込み枠の縦中央に表示する * ------------------------------------------------ */ /* 説明用のテキストを出力 */ $p->fit_textline("Case 5: ", $xtext, $ytext, ""); $p->fit_textline("verticalalign=center firstlinedist=capheight " . "lastlinedist=descender", $xtext, $ytext-15, ""); /* テキストフローオブジェクトを作成する */ $tf = $p->create_textflow($text, $cr_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* オプションリストを設定する * "showborder" はめ込み枠の境界線を表示 * "verticalalign=center" はめ込み枠内の縦中央に表示 * "firstlinedist=capheight" はめ込み枠上端と、テキスト先頭行の大 * 文字最上部のラインが接するように表示 * "lastlinedist=descender" テキスト最終行のフォントのディセンダの * 最下部のラインが、はめ込み枠の下端に接するように表示 */ $fit_optlist = "showborder verticalalign=center " . "firstlinedist=capheight lastlinedist=descender"; $result = $p->fit_textflow($tf, $x, $y, ($x + $boxwidth), ($y + $boxheight), $fit_optlist); if (!$result == "_stop") { /* エラーかどうかをチェックする */ } $p->delete_textflow($tf); /* -------------------------------------------------------------- * Case 6: テキストをはめ込み枠の縦方向に均等割付する * -------------------------------------------------------------- */ $y -= $yoffset; $ytext = $y + $boxheight/2; /* 説明用のテキストを出力 */ $p->fit_textline("Case 6: verticalalign=justify linespreadlimit=200%", $xtext, $ytext, ""); /* テキストフローオブジェクトを作成する */ $tf = $p->create_textflow($text, $cr_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* オプションリストを設定する * "showborder" はめ込み枠の境界線を表示する * "verticalalign=justify" はめ込み枠内の縦方向にテキストを均等割付 * "linespreadlimit=200% 縦方向の均等割付けの際、行送りの最大値をフォン * トサイズの200%で指定 */ $fit_optlist = "showborder verticalalign=justify linespreadlimit=200%"; $result = $p->fit_textflow($tf, $x, $y, ($x + $boxwidth), ($y + $boxheight), $fit_optlist); if (!$result == "_stop") { /* エラーかどうかをチェックする */ } $p->delete_textflow($tf); /* -------------------------------------------------------------------- * Case 7: テキストをはめ込み枠の縦方向に均等割付し、はめ込み枠の上端 * からテキスト先頭行、はめ込み枠の下端からテキスト最終行を、 * それぞれ指定した間隔で表示する * -------------------------------------------------------------------- */ $y -= $yoffset; $ytext = $y + $boxheight/2; /* 説明用のテキストを出力 */ $p->fit_textline("Case 7:", $xtext, $ytext+15, ""); $p->fit_textline("verticalalign=justify linespreadlimit=200%", $xtext, $ytext, ""); $p->fit_textline("firstlinedist=capheight lastlinedist=descender", $xtext, $ytext-15, ""); /* テキストフローオブジェクトを作成する */ $tf = $p->create_textflow($text, $cr_optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* オプションリストを設定する * "showborder" はめ込み枠の境界線を表示 * "verticalalign=justify" はめ込み枠内の縦方向にテキストを均等割付 * "linespreadlimit=200%" 縦方向の均等割付けの際、行送りの最大値をフォ * ントサイズの200%で指定 * "firstlinedist=capheight" はめ込み枠上端と、テキスト先頭行の大文字 * 最上部のラインが接するように表示 * "lastlinedist=descender" テキスト最終行のフォントのディセンダの最下 * 部が、はめ込み枠の下端に接するように表示 */ $fit_optlist = "showborder verticalalign=justify linespreadlimit=200% " . "firstlinedist=capheight lastlinedist=descender"; $result = $p->fit_textflow($tf, $x, $y, ($x + $boxwidth), ($y + $boxheight), $fit_optlist); 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=vertical_alignment_in_fitbox.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; ?>