set_option("searchpath={" . $searchpath . "}"); /* load_font() 等でエラーが起きた場合、0を返す */ $p->set_option("errorpolicy=return"); $p->set_option("stringformat=utf8"); 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("Helvetica-Bold", "unicode", ""); if ($font == 0) throw new Exception("Error: " . $p->get_errmsg()); /* 背景画像をロードする */ $image = $p->load_image("auto", $imagefile, ""); if ($image == 0) throw new Exception("Error: " . $p->get_errmsg()); /* 画像サイズを取得する */ $imagewidth = $p->info_image($image, "imagewidth", ""); $imageheight = $p->info_image($image, "imageheight", ""); /* 画像の解像度が 72 dpi を超えることがある。もし、取得した寸法 *(ポイント単位)をパターンの高さと幅に使用し、画像をパターンに * 配置した場合、パターンのサイズに一致せず、白い余白が残る。 * 画像とパターンを同じサイズにするために、解像度に基づいて画像 * サイズを計算する。 * * 元の画像の解像度を取得する */ $resx = $p->info_image($image, "resx", ""); $resy = $p->info_image($image, "resy", ""); /* 画像の寸法を計算する */ if ($resx > 0) { $imagewidth = $imagewidth * 72 / $resx; $imageheight = $imageheight * 72 / $resy; } /* 取得した画像の寸法を使用してパターンを生成する */ $pattern = $p->begin_pattern_ext($imagewidth, $imageheight, ""); $p->fit_image($image, 0, 0, ""); $p->end_pattern(); $p->close_image($image); /* ページを始める */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* パターンを現在の塗り色として設定する。setcolor()を、save() とrestore() * で挟むことで、元の色を引き継ぐことができる。 */ $p->save(); /* テキストの塗りつぶし(デフォルト)に加え、textrendering=2 を設定することで * テキストを描線し、黒以外の描線色を設定することもできる。 */ $p->set_text_option("textrendering=2"); $p->setcolor("stroke", "rgb", 0.5, 0.2, 0.1, 0); $p->setcolor("fill", "pattern", $pattern, 0, 0, 0); /* 現在の塗りつぶし色(つまりパターンカラー)でテキストを出力する */ $fontsize = 50; $p->set_text_option("font=" . $font . " fontsize=" . $fontsize); $p->fit_textline("Hello World!", 50, 500, ""); $p->fit_textline("(says PDFlib GmbH)", 50, 500 - $fontsize, ""); $p->restore(); $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=image_as_text_fill_color.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred:\n". "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Exception $e) { echo($e->getMessage()); exit(1); } $p = 0; ?>