set_option("searchpath={" . $searchpath . "}"); /* load_font() 等でエラーが起きた場合、0を返す */ $p->set_option("errorpolicy=return"); $p->set_option("stringformat=utf8"); $indoc = $p->open_pdi_document($infile, ""); if ($indoc == 0) throw new Exception("Error: " . $p->get_errmsg()); /* * 入力文書のページ数を決定し、出力文書数を計算する */ $page_count = (int) $p->pcos_get_number($indoc, "length:pages"); $outdoc_count = $page_count / SUBDOC_PAGES + ($page_count % SUBDOC_PAGES > 0 ? 1 : 0); /* * このループでは、HTTP経由で返された1つの出力文書のみを生成する。 * 全ての出力文書を生成するには、以下のようにループ条件を変更する。 * $outdoc_counter < $outdoc_count */ for ($outdoc_counter = 0, $page = 0; $outdoc_counter < 1; $outdoc_counter += 1) { $outfile = $outfile_basename . "_" . ($outdoc_counter + 1) . ".pdf"; /* * 出力文書を開く */ if ($p->begin_document("", "") == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", $title); $p->set_info("Subject", "Sub-document " . ($outdoc_counter + 1) . " of " . $outdoc_count . " of input document '" . $infile . "'"); for ($i = 0; $page < $page_count && $i < SUBDOC_PAGES; $page += 1, $i += 1) { /* ページサイズは fit_pdi_page()で調整される */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); $pagehdl = $p->open_pdi_page($indoc, $page + 1, ""); if ($pagehdl == 0) throw new Exception("Error opening page: " . $p->get_errmsg()); /* * 出力ページ上にインポートしたページを配置し、ページサイズを * 調整する */ $p->fit_pdi_page($pagehdl, 0, 0, "adjustpage"); $p->close_pdi_page($pagehdl); $p->end_page_ext(""); } /* 出力文書を閉じる */ $p->end_document(""); /* ユーザー側に出力文書を返す。もし、すべての分割された文書を処理する場合は、 * 例えば、ディスクに文書を作成し、出力文書のリンクのリストでHTMLページを作成 * する。 */ $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=" . $outfile); print $buf; } /* 入力文書を閉じる */ $p->close_pdi_document($indoc); } 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); exit(1); } $p = 0; ?>