set_option("searchpath={" . $searchpath . "}"); /* This means we must check return values of load_font() etc. */ $p->set_option("errorpolicy=return"); $p->set_option("stringformat=utf8"); if ($p->begin_document($outfile, "pdfa=PDF/A-1b:2005") == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", $title); /* ----------------------------------------------------------------- * 既存PDF文書を開く。 * パターンを定義する前に出力インテントを設定する必要があるため、 * パターンを作成する前に、この処理を行う。 * ----------------------------------------------------------------- */ $indoc = $p->open_pdi_document($pdffile, ""); if ($indoc == 0) throw new Exception("Error: " . $p->get_errmsg()); $endpage = (int) $p->pcos_get_number($indoc, "length:pages"); /* 既存文書から出力インテントを取得し、それを出力文書にコピーする */ $res = $p->pcos_get_string($indoc, "type:/Root/OutputIntents"); if ($res == "array") { $ret = $p->process_pdi($indoc, -1, "action=copyoutputintent"); if ($ret == 0) throw new Exception("Error: " . $p->get_errmsg()); }else { /* If the input document doesn't have any output intent we * explicitly set sRGB so that we can use grayscale bitmaps. */ $p->load_iccprofile("sRGB", "usage=outputintent"); } /* ------------------------------------------------------------------- * 画像マスクに基づいて、ビットマップパターンを定義する。 * 画面上で滑らかに見えるよう、画像を縮小する。 * ------------------------------------------------------------------- */ $bitmap = ""; for ($j=0; $j < count($data[$ht]); $j++) { $bitmap .= sprintf("%c",$data[$ht][$j]); } $p->create_pvf("/pvf/image/bitmap", $bitmap, ""); $image = $p->load_image("raw", "/pvf/image/bitmap", "bpc=1 components=1 height=16 width=16 invert mask"); if ($image == 0) throw new Exception("Error: " . $p->get_errmsg()); $w = 16 / SCALING_FACTOR; $pattern = $p->begin_pattern_ext($w, $w, ""); $p->fit_image($image, 0, 0, "scale=" . (1 / SCALING_FACTOR)); $p->end_pattern(); $p->close_image($image); /* 既存PDF文書数分ループする */ for ($pageno = 1; $pageno <= $endpage; $pageno++) { $page = $p->open_pdi_page($indoc, $pageno, ""); if ($page == 0) throw new Exception("Error: " . $p->get_errmsg()); /* ページサイズは fit_pdi_page() で調整される */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* 既存PDFページを出力PDFページ上に配置し、サイズを調整する */ $p->fit_pdi_page($page, 0, 0, "adjustpage"); /* スタンプで使用するフォントをロードする */ $font = $p->load_font("NotoSerif-Regular", "unicode", "embedding"); if ($font == 0) throw new Exception("Error: " . $p->get_errmsg()); /* パターンカラーで塗りつぶされたスタンプを配置する */ $p->setcolor("fill", "pattern", $pattern, 0, 0, 0); $p->fit_textline("PUBLISHED", 20, 20, "font=" . $font . " fontsize=1 boxsize={550 800} stamp=ll2ur"); $p->close_pdi_page($page); $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=transparent_stamp_for_pdfa1.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; ?>