set_option("errorpolicy=return"); $p->set_option("stringformat=utf8"); if ($p->begin_document($outfile, "compatibility=1.7ext3 pagelayout=singlepage") == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", $title); $font = $p->load_font("Helvetica", "winansi", ""); if ($font == 0) throw new Exception("Error: " . $p->get_errmsg()); /* * ページ上の全てのフィールドの内容を再計算するJavaScriptを定義する */ $calculate_now_action = $p->create_action("JavaScript", "script { this.calculateNow(); }"); /* Start page */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height " . "action={open " . $calculate_now_action . "}"); /* * リセットボタンのオプションリストを生成する。デフォルト値にリセットする * には全てのフィールド名を含む必要がある。 */ $reset_optlist = "namelist={"; /* * 使用可能な領域と、テキストとバーコードフィールドのサイズを計算する。 * バーコードフィールドはテキストフィールドの幅の2倍に設定する。 */ $num_fields = 3; $field_area_height = PAGE_HEIGHT - 3 * MARGIN - RESET_BUTTON_HEIGHT; $field_area_width = PAGE_WIDTH - 2 * MARGIN; $field_height = ($field_area_height - ($num_fields - 1) * MARGIN) / $num_fields; $text_field_width = ($field_area_width - MARGIN) / 3; $barcode_field_width = ($field_area_width - MARGIN) * 2 / 3; $field_number = 0; $text_field_name = "text_" . $field_number; $barcode_field_name = "barcode_" . $field_number; /* * リセットするため、テキストフィールド名をリストフィールドに追加する */ $reset_optlist .= " {" . $text_field_name . "}"; /* * テキストフィールドを生成 * * Acrobat は少なくともバージョン10.1までは、ページ上の最初のフィールドにバー * コードフィールドがある場合、クラッシュするバグが含まれていることに注意する。 * そのため、別の種類のフィールドが最初に作成されていることを確認してください。 */ $optlist = "tooltip={PDF417 barcode:\n" . TOOLTIP_TEXT . "} " . "multiline=true bordercolor={gray 0} linewidth=1 " . "font=" . $font . " " . "currentvalue={PDF417 barcode:\n" . SAMPLE_TEXT . "} " . "defaultvalue={PDF417 barcode:\n" . SAMPLE_TEXT . "}"; $x = MARGIN; $y = PAGE_HEIGHT - (MARGIN + RESET_BUTTON_HEIGHT ) - ($field_number + 1) * (MARGIN + $field_height); $p->create_field($x, $y, $x + $text_field_width, $y + $field_height, $text_field_name, "textfield", $optlist); /* * テキストフィールドの内容からバーコードフィールドの値を計算するアクションを * 生成する。 */ $calculate_action = create_calc_action($p, $text_field_name); /* * PDF417 バーコードを生成: * * - 圧縮無し (dataprep=0) * - エラー訂正係数 レベル 7 (ecc=7) * - 横間隔: 3, 縦間隔: 6 (xsymheight=6 xsymwidth=3) */ $optlist = "barcode={symbology=PDF417 dataprep=0 " . "ecc=7 xsymheight=6 xsymwidth=3} " . "action={calculate=" . $calculate_action . "} font=" . $font; $x = 2 * MARGIN + $text_field_width; $p->create_field($x, $y, $x + $barcode_field_width, $y + $field_height, $barcode_field_name, "textfield", $optlist); /* * データマトリックスバーコードを生成するため、いくつかの手順を繰り返す */ $field_number += 1; $text_field_name = "text_" . $field_number; $barcode_field_name = "barcode_" . $field_number; $reset_optlist .= " {" . $text_field_name . "}"; $optlist = "tooltip={Data Matrix barcode:\n" . TOOLTIP_TEXT . "} " . "multiline=true bordercolor={gray 0} linewidth=1 " . "font=" . $font . " " . "currentvalue={Data Matrix barcode:\n" . SAMPLE_TEXT . "} " . "defaultvalue={Data Matrix barcode:\n" . SAMPLE_TEXT . "}"; $x = MARGIN; $y = PAGE_HEIGHT - (MARGIN + RESET_BUTTON_HEIGHT ) - ($field_number + 1) * (MARGIN + $field_height); $p->create_field($x, $y, $x + $text_field_width, $y + $field_height, $text_field_name, "textfield", $optlist); $calculate_action = create_calc_action($p, $text_field_name); /* * データマトリックスバーコードを生成 */ $optlist = "barcode={symbology=DataMatrix dataprep=0 " . "ecc=0 xsymwidth=10} " . "action={calculate=" . $calculate_action . "} font=" . $font; $x = 2 * MARGIN + $text_field_width; $p->create_field($x, $y, $x + $barcode_field_width, $y + $field_height, $barcode_field_name, "textfield", $optlist); /* * QRコードバーコードを生成するため、いくつかの手順を繰り返す */ $field_number += 1; $text_field_name = "text_" . $field_number; $barcode_field_name = "barcode_" . $field_number; $reset_optlist .= " {" . $text_field_name . "}"; $optlist = "tooltip={QR Code barcode:\n" . TOOLTIP_TEXT . "} " . "multiline=true bordercolor={gray 0} linewidth=1 " . "font=" . $font . " " . "currentvalue={QR Code barcode:\n" . SAMPLE_TEXT . "} " . "defaultvalue={QR Code barcode:\n" . SAMPLE_TEXT . "}"; $x = MARGIN; $y = PAGE_HEIGHT - (MARGIN + RESET_BUTTON_HEIGHT ) - ($field_number + 1) * (MARGIN + $field_height); $p->create_field($x, $y, $x + $text_field_width, $y + $field_height, $text_field_name, "textfield", $optlist); $calculate_action = create_calc_action($p, $text_field_name); /* * QR コード バーコードを生成 : * * - エラー訂正係数 :レベル 2 (ecc=2) * - 横間隔: 10 * - エンコードする前に圧縮 (dataprep=1) */ $optlist = "barcode={symbology=QRCode ecc=2 xsymwidth=10 dataprep=1} " . "action={calculate=" . $calculate_action . "} font=" . $font; $x = 2 * MARGIN + $text_field_width; $p->create_field($x, $y, $x + $barcode_field_width, $y + $field_height, $barcode_field_name, "textfield", $optlist); /* * リセットボタンのためのオプションリストを終了し、テキスト入力フィールドを * デフォルト値にリセットするリセットボタンを作成する。 * values. */ $reset_optlist .= "}"; $reset_action = $p->create_action("ResetForm", $reset_optlist); $optlist = "action={activate=" . $reset_action . "} font={" . $font . "} caption={Reset} bordercolor=black " . "tooltip={Reset all fields to default values}"; $x = MARGIN; $y = PAGE_HEIGHT - MARGIN - RESET_BUTTON_HEIGHT; $p->create_field($x, $y, $x + RESET_BUTTON_WIDTH, $y + RESET_BUTTON_HEIGHT, "Reset", "PushButton", $optlist); /* * Adobe Reader の場合、バーコードフィールドを表示できないため、テキストボックスで * 警告を表示する */ $optlist = "font=" . $font . " fontsize=14 fillcolor=red"; $tf = $p->add_textflow(0, "Note that the barcode fields only work with full Acrobat " . "version 9 or newer, but not with Adobe Reader!", $optlist); $x = 2 * MARGIN + RESET_BUTTON_WIDTH; $p->fit_textflow($tf, $x, $y, PAGE_WIDTH + 3 * MARGIN - RESET_BUTTON_WIDTH, $y + RESET_BUTTON_HEIGHT, "fitmethod=auto"); $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=barcode_field.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); } /** * バーコードフィールドの値を計算するためのアクションを作成する * * @param p * pdflib オブジェクト * @param text_field_name * バーコードフィールドの値を決定している、テキストフィールドの名前 * * @return アクションのハンドル * * @throws PDFlibException */ function create_calc_action($p, $text_field_name){ $script = "script { " . "try { " . "var fieldname = \"" . $text_field_name . "\"; " . "event.value = fieldname + \":\" + this.getField(fieldname).value;" . "} " . "catch(e) {" . "event.value = \"EXCEPTION\";" . "}" . "}"; return $p->create_action("JavaScript", $script); } ?>