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); $font = $p->load_font("NotoSerif-Regular", "unicode", ""); if ($font == 0) throw new Exception("Error: " . $p->get_errmsg()); /* 出力PDFページをA4サイズで作成し、トップダウン座標系を使用する */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height topdown"); /* PDFlib の座標系はDTPポイントを使用している。ポイントとmmの関係は * 1 pt = 1/72 inch = 25.4/72 mm = 0.3528 mm. 座標単位をポイントか * らセンチメールに変換すると、72/2.54 = 28.3465pt/cm * この値を p.scale()に設定する。 */ $p->scale(28.3465, 28.3465); /* これにより、PDFlibは座標はセンチメートル単位で設定される(ただし * 注釈などのインタラクティブ要素は除く)。またこれらは現在のページに * 対して有効であり、必要に応じて各ページで繰り返す必要がある。 */ /* 注釈作成等に使用する座標は、デフォルト座標で認識されるため、上記のユーザー * 座標が用いられていることを認識させるようtrueを設定する */ $p->set_option("usercoordinates=true"); /* 1cm間隔で境界線を引く。3ライン目は1cm四方の正方形を描き、塗りつぶす */ $p->setlinewidth(0.01); for ($i = 1; $i < 29; $i++) { $p->moveto(0, $i); $p->lineto(1, $i); if ($i == 3) { $p->rect(0, $i, 1, 1); } $p->fill_stroke(); } /* フォントサイズを設定(cm単位) */ $p->setfont($font, 0.5); $p->fit_textline("Centimeters from the top left instead of points from " . "the bottom left:", 2, 5, ""); $p->fit_textline("The small lines on the left are placed 1 cm from " . "each other.", 2, 6, ""); $p->fit_textline("This text line is displayed at 7 cm from top and 2 " . "cm from the left.", 2, 7, ""); $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=metric_topdown_coordinates.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; ?>