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 ); /* テンプレートに含める画像を読み込む */ $image = $p->load_image("auto", $imagefile, ""); if ($image == 0) throw new Exception("Error: " . $p->get_errmsg()); /* ビジネスレターのヘッダーを含むテンプレートを定義する。 * 画像と、送信者アドレスのテキストを含むビジネスレターの * ヘッダーを定義する。 */ $templ = $p->begin_template_ext($pagewidth, $pageheight, ""); /* 画像をテンプレートに配置する */ $p->fit_image($image, $pagewidth-230, $pageheight-140, "boxsize={200 100} fitmethod=meet"); /* テキストをテンプレートに配置する */ $p->fit_textline("Kraxi Systems, Inc., 17, Aviation Road, Paperfield", 30, $pageheight-160, "fontname=NotoSerif-Regular fontsize=8"); /* テンプレートを完成させる */ $p->end_template_ext(0, 0); /* 画像をクローズする */ $p->close_image($image); /* 1ページずつ4通のレターを作成する。 * 各ページに、差出人の住所、顧客の住所、説明文などを記載したテンプレート * を配置する。 */ for ($i = 0; $i < 4; $i++) { $p->begin_page_ext($pagewidth, $pageheight, ""); $y = $pageheight - 165; /* 画像を使用するのと同じように、ページ上にテンプレートを * 配置する */ $p->fit_image($templ, 0.0, 0.0, ""); /* 顧客のアドレスを配置する */ for ($j = 0; $j < 3; $j++) { $p->fit_textline($addresses[$i][$j], $x, $y-=15, "fontname=NotoSerif-Regular fontsize=12"); } /* 実際のページコンテンツをページ上に配置する */ $p->fit_textline("Dear customer, this is the actual page contents " . "of page " . ($i+1) . ".", $x, $y-=80, "fontname=NotoSerif-Regular fontsize=12"); $p->fit_textline("The image and the sender's address above are " . "part of a template.", $x, $y -=20, "fontname=NotoSerif-Regular fontsize=12"); $p->end_page_ext(""); } /* 必要であれば、さらに別のページにもテンプレートを配置する。 * その後、クローズする。 */ $p->close_image($templ); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=repeated_contents.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; ?>