|
|
XXXV. Forms Data Format 関数
Forms Data Format (FDF)は、PDFドキュメント内部のフォームを処理する
ためのフォーマットです。FDFの詳細および一般的な用途については、
にあるドキュメン
トを参照下さい。
FDFの基本的な考えは、HTMLフォームに似ています。基本的な違いは、投
稿ボタンが押された際にフォームに埋められたデータをサーバーに送信す
る方法のフォーマット(これがForm Data Format そのものです)およびフォー
ムのフォーマット自体(これは Portable Document Format、PDFです)です。
FDFデータの処理は、fdf関数に提供された機能の一つです。しかし機能は
他にもあります。既存のPDFフォームを用いてフォーム自体を修正せずに
入力フィールドのデータを入力させることもあるかと思われます。
このような場合、FDFドキュメント を作成
(fdf_create())、各入力フィールドの値を設定
(fdf_set_value())、PDFフォームとそれを関連付け
(fdf_set_file())ます。最後にブラウザに MIME型
application/vnd.fdf で送信する必要があります。ブラウザの Acrobat
Reader プラグイン がこの MIME 型を認識し、関連する PDF フォームを
読み込み、FDF ドキュメントからのデータを書き込みます。
テキストエディタでFDFドキュメントの中を見てみると、
FDFという名前のカタログオブジェクトが見つかるは
ずです。このようなオブジェクトには、Fields,
F, Status といった複数のエン
トリが含まれます。
最も多く使用されるエントりは、一連の入力フィールドを指す
Fieldsとこのデータが属するPDFドキュメントのファ
イル名を保持するF です。これらのエントリは、
FDFドキュメントの中で/F-Keyまたは /Status-Keyのように参照されてい
ます。 fdf_set_file()および
fdf_set_status()のような関数によりこれらのエン
トリを修正することが可能です。Fields は、
fdf_set_value(),fdf_set_opt()
等で修整可能です。
から取得可能な
FDF toolkit SDKが必要です。
PHP 4.3以降、少なくともSDK version 5.0が必要です。
FDF toolkit libraryは、バイナリ版のみ利用可能で、
Adobeによりサポートされているプラットフォームは、
Win32, Linux, Solaris, AIXです。
--with-fdftk[=DIR]を指定してPHPを
コンパイルする必要があります。
注意:
fdftkサポートを指定してPHPを設定した際に問題を発生した場合、
ヘッダファイルfdftk.hおよび
ライブラリ libfdftk.so が正しい場所にあること
を確認して下さい。configureスクリプトは、FDF SDK配布ファイルおよび
通常のDIR/include、
DIR/libといった配置のディレクトリ構造をともに
サポートします。このため、展開した配布ディレクトリまたは
--with-fdftk=/usr/localにより
/usr/local/include および
/usr/local/lib のように使用するプラットフォー
ムに応じた適当なヘッダファイルおよびライブラリのどちらかを指定する
ことが可能です。
Win32ユーザへの注意:
Windows環境でこのモジュールを有効にするには、PHP/Win32バイナリパッ
ケージのDLLフォルダからfdftk.dllを
使用するWindowsマシンのSYSTEM32フォルダ
(例: C:\WINNT\SYSTEM32または C:\WINDOWS\SYSTEM32)
へコピーする必要があります。
この拡張モジュールは設定ディレクティブを全く定義しません。
これらの定数は、この拡張モジュールで定義されており、
この拡張モジュールがPHP内部にコンパイルされているか実行時に動的にロー
ドされるかのどちらかの場合のみ使用可能です。
以下の例でフォームデータの評価に関して説明します。
例 1. FDF ドキュメントの評価
<?php
$fdf = fdf_open_string($HTTP_FDF_DATA);
$volume = fdf_get_value($fdf, "volume");
echo "The volume field has the value '<B>$volume</B>'<BR>";
$date = fdf_get_value($fdf, "date");
echo "The date field has the value '<B>$date</B>'<BR>";
$comment = fdf_get_value($fdf, "comment");
echo "The comment field has the value '<B>$comment</B>'<BR>";
if(fdf_get_value($fdf, "show_publisher") == "On") {
$publisher = fdf_get_value($fdf, "publisher");
echo "The publisher field has the value '<B>$publisher</B>'<BR>";
} else
echo "Publisher shall not be shown.<BR>";
if(fdf_get_value($fdf, "show_preparer") == "On") {
$preparer = fdf_get_value($fdf, "preparer");
echo "The preparer field has the value '<B>$preparer</B>'<BR>";
} else
echo "Preparer shall not be shown.<BR>";
fdf_close($fdf);
?>
|
|
add a note
User Contributed Notes
Forms Data Format 関数
08-Mar-2005 01:56
For the example below, I have not b een able to have it populate checkbox fields.
g8z at yahoo dot com
18-Oct-2004 06:46
This is for users who are looking for a way to merge HTML form data with a PDF Form, then output the PDF Form with data populated in it, to a web browser.
This is a pure PHP solution which does NOT require the FDF toolkit. Contributed by www.TUFaT.com
<?php
$form = ';
function create_fdf ($pdffile, $strings, $keys)
{
$fdf = "%FDF-1.2\n%����\n";
$fdf .= "1 0 obj \n<< /FDF << /Fields [\n";
foreach ($strings as $key => $value)
{
$key = addcslashes($key, "\n\r\t\\()");
$value = addcslashes($value, "\n\r\t\\()");
$fdf .= "<< /T ($key) /V ($value) >> \n";
}
foreach ($keys as $key => $value)
{
$key = addcslashes($key, "\n\r\t\\()");
$fdf .= "<< /T ($key) /V /$value >> \n";
}
$fdf .= "]\n/F ($pdffile) >>";
$fdf .= ">>\nendobj\ntrailer\n<<\n";
$fdf .= "/Root 1 0 R \n\n>>\n";
$fdf .= "%%EOF";
return $fdf;
}
$strings = array(
'date' => '10/17/2004',
'full_name' => 'Joe Doe',
'phone_num' => '123-4567',
'company' => 'ACME Widgets',
'amount' => 'USD 100.00'
);
$keys = array('
gender' => 'male','is_adult' => 'Off','urgent' => 'On');
header('Content-type: application/vnd.fdf');
echo create_fdf($form, $strings, $keys);
?>
sid at accesspdf dot com
07-Oct-2004 06:09
Basic FDF data is easy to create using native PHP; you don't need Adobe's FDF Toolkit. I wrote a function for this purpose called forge_fdf(). You can download it from:
I created it for my book, PDF Hacks. An example of forge_fdf() in action can be viewed online:
Download the full code for this online example from:
Note how the PDF form data is submitted back to the server via POST rather than FDF. No need to parse FDF.
Cheers-
Sid Steward
ronsantiago at hotmail dot com
03-Oct-2004 04:18
Use these functions instead if you want to create an FDF file without installing the FDF toolkit. You would use it the same way as the fdf_* functions. BTW, I only wrote the basic library functions for creating FDFs.
define('ntk_FDFValue', 0);
define('ntk_FDFStatus', 1);
define('ntk_FDFFile', 2);
define('ntk_FDFID', 3);
define('ntk_FDFFf', 5);
define('ntk_FDFSetFf', 6);
define('ntk_FDFClearFf', 7);
define('ntk_FDFFlags', 8);
define('ntk_FDFSetF', 9);
define('ntk_FDFClrF', 10);
define('ntk_FDFAP', 11);
define('ntk_FDFAS', 12);
define('ntk_FDFAction', 13);
define('ntk_FDFAA', 14);
define('ntk_FDFAPRef', 15);
define('ntk_FDFIF', 16);
define('ntk_FDFEnter', 0);
define('ntk_FDFExit', 1);
define('ntk_FDFDown', 2);
define('ntk_FDFUp', 3);
define('ntk_FDFFormat', 4);
define('ntk_FDFValidate', 5);
define('ntk_FDFKeystroke', 6);
define('ntk_FDFCalculate', 7);
define('ntk_FDFNormalAP', 1);
define('ntk_FDFRolloverAP', 2);
define('ntk_FDFDownAP', 3);
function ntk_fdf_header() {
header('Content-type: application/vnd.fdf');
}
function ntk_fdf_create() {
$fdf['header'] = "%FDF-1.2\n%����\n1 0 obj \n<< /FDF ";
$fdf['trailer'] = ">>\nendobj\ntrailer\n<<\n/Root 1 0 R \n\n>>\n%%EOF";
return $fdf;
}
function ntk_fdf_close(&$fdf) {
unset($fdf);
}
function ntk_fdf_set_file(&$fdf, $pdfFile) {
$fdf['file'] = $pdfFile;
}
function ntk_fdf_set_target_frame(&$fdf, $target) {
$fdf['target'] = $target;
}
function ntk_fdf_set_value(&$fdf, $fieldName, $fieldValue) {
$fdf['values'] = array_merge($fdf['values'], array($fieldName => $fieldValue));
}
function ntk_fdf_add_doc_javascript(&$fdf, $scriptName, $script) {
$fdf['docscripts'] = array_merge($fdf['docscripts'], array($scriptName => $script));
}
function ntk_fdf_set_javascript_action(&$fdf, $fieldName, $trigger, $script) {
$fdf['fieldscripts'] = array_merge($fdf['fieldscripts'], array($fieldName => array($script, $trigger)));
}
function ntk_fdf_save(&$fdf, $fdfFile = null) {
$search = array('\\', '(', ')');
$replace = array('\\\\', '\(', '\)');
$fdfStr = $fdf['header'];
$fdfStr.= "<< ";
if(isset($fdf['file'])) {
$fdfStr.= "/F (".$fdf['file'].") ";
}
if(isset($fdf['target'])) {
$fdfStr.= "/Target (".$fdf['target'].") ";
}
if(isset($fdf['docscripts'])) {
$fdfStr.= "/JavaScript << /Doc [\n";
// populate the doc level javascripts
foreach($fdf['docscripts'] as $key => $value) {
$fdfStr.= "(".str_replace($search, $replace, $key).")(".str_replace($search, $replace, $value).")";
}
$fdfStr.= "\n] >>\n";
}
if(isset($fdf['values']) || isset($fdf['fieldscripts'])) {
// field level
$fdfStr.= "/Fields [\n";
if(isset($fdf['fieldscripts'])) {
// populate the field level javascripts
foreach($fdf['fieldscripts'] as $key => $val) {
$fdfStr .= "<< /A << /S /JavaScript /JS (".str_replace($search, $replace, $val[0]).") >> /T (".str_replace($search, $replace, $key).") >>\n";
}
}
if(isset($fdf['values'])) {
// populate the fields
foreach($fdf['values'] as $key => $value) {
$fdfStr .= "<< /V (".str_replace($search, $replace, $value).") /T (".str_replace($search, $replace, $key).") >>\n";
}
}
$fdfStr.= "]\n";
}
$fdfStr.= ">>";
$fdfStr.= $fdf['trailer'];
if($fdfFile) {
$handle = fopen($fdfFile, 'w');
fwrite($handle, $fdfStr);
fclose($handle);
}
else {
echo $fdfStr;
}
}
m1tk4 at hotmail dot com
01-Jul-2004 05:34
If you want to add FDF support without rebuilding your RedHat EL3 / Fedora PHP RPMs, see instructions at
software at yvanrodrigues dot com
25-Mar-2004 10:16
Do not use version 6 of the fdftk.dll (windows) with PHP4.3.4, use the one that comes with PHP.
If you use the newer DLL fdf_create will not return a valid handle.
fleischer at mail dot com
18-Dec-2003 03:40
The code suggested by greg@... and adam@... is extremely helpful, but I've found out (the hard way) that unclosed parentheses within strings contained in the input array ($values in greg's code or $pdf_data in adam's) will cause Acrobat to issue an error to the effect that the file is corrupted. In other words, if there are strings such as "a) my first point; b) my second point" in the input array, the resulting PDF/FDF file will be considered corrupted by Acrobat. This apparently happens because all the field names in the structure of an FDF file are enclosed in parentheses.
The solution I've devised is to escape all opening and closing parentheses with a backslash, which in turn means you need to escape all backslashes. The code below does all that.
Erik
---------------
function output_fdf ($pdf_file, $pdf_data)
{
$fdf = "%FDF-1.2\n%����\n";
$fdf .= "1 0 obj \n<< /FDF ";
$fdf .= "<< /Fields [\n";
$search = array('\\', '(', ')');
$replace = array('\\\\', '\(', '\)');
foreach ($pdf_data as $key => $val)
{
$clean_key = str_replace($search, $replace, $key);
$clean_val = str_replace($search, $replace, $val);
$fdf .= "<< /V ($clean_val)/T ($clean_key) >> \n";
}
$fdf .= "]\n/F ($pdf_file) >>";
$fdf .= ">>\nendobj\ntrailer\n<<\n";
$fdf .= "/Root 1 0 R \n\n>>\n";
$fdf .= "%%EOF";
return $fdf;
}
mirage at rateaprof dot com
08-May-2003 12:33
If you get the new fdftkv5.tar.gz from adobe's site (per the link above), you'll get some totally new spacing and capitalization of files. To make the current 4.3.1 configure, you need to do a few things.
untar fdftkv5.tar.gz into /usr/local
cd /usr/local
#for ease of use
ln -s FDFToolkit\ for\ UNIX fdf
cd fdf
ln -s Headers\ And\ Libraries HeadersAndLibraries
#may need to modify the following for your OS
ln -s LINUX linux
cd linux/C
ln -s LIBFDFTK.SO libfdftk.so
cd ..
cd ..
ln -s Headers headers
cd headers
ln -s FDFTK.H fdftk.h
And that should get you going... and to whoever is maintaining the configure script, please be aware there are changes in the FDF Toolkit.
Teemu
05-Mar-2003 02:17
Maybe you have to use Header-function that your browser will regonize xfdf-file. Like this:
Header( "Content-type: application/vnd.adobe.xfdf");
jeff at cowart dot net
20-Jan-2003 04:06
I have tried to use the scripts above by adam and Toppi and I have been unable to get them to work unless I save the generated fdf file and then open it manually in acrobat.
Toppi at i-Mehl dot De
26-Nov-2002 08:18
I tried a lot with FDF -> PDF and merging these documents...
in my opinon xfdf is more handy than fdf... for those who'd like to try: feel free to use this little function to generate an xfdf document from an array.
ToPPi
function array2xfdf($xfdf_data, $pdf_file) {
// Creates an XFDF File from a 2 dimensional
// Array Format: "array ("key1" => "content1", "key2" => "content2");
$xfdf = "<?xml version='1.0' encoding='UTF-8'?>\n";
$xfdf .= "<xfdf xmlns=' xml:space='preserve'>\n";
$xfdf .= "<fields>\n";
// Loop -> Array to XFDF Data
foreach ($xfdf_data as $key => $val) {
$xfdf .= "<field name='".$key."'>\n";
$xfdf .= "<value>".$val."</value>\n";
$xfdf .= "</field>\n";
};
// XFDF "Footer"
$xfdf .= "</fields>";
$xfdf .= "<f href='".$pdf_file."'/>";
$xfdf .= "</xfdf>";
return $xfdf;
}
mitka at actdev.com
21-Oct-2002 11:24
IMPORTANT:
If you handled the FDF POSTs via $HTTP_RAW_POST_DATA as in user contributed scripts above, it's good to know that once you decide to rebuild PHP with FDFToolkit support, $HTTP_RAW_POST_DATA will be undefined.
Good news - $HTTP_FDF_DATA _will_ be defined instead. (Look at the example above).To get the user contributed scripts working in both plain PHP and PHP+FDFToolkit use
$HTTP_RAW_POST_DATA . $HTTP_FDF_DATA
where $HTTP_RAW_POST_DATA mentioned.
Dimitri Tarassenko
adam at andemyte dot com
02-Aug-2002 06:30
Here is yet another example of generating pre-filled PDFs without using the FDF functions. This function takes two args: a URL to the PDF (like "" and an array with all the field's values.
/*
WARNING!! THIS FUNCTION SENDS HTTP HEADERS! It MUST be called before
any content is spooled to the browser, or the function will fail!
void output_fdf (string $pdf_file, array $pdf_data)
$pdf_file: a string containing a URL path to a PDF file on the
server. This PDF MUST exist and contain fields with
the names referenced by $pdf_data for this function
to work.
$pdf_data: an array of any fields in $pdf_file that you want to
populate, of the form key=>val; where the field
name is the key, and the field's value is in val.
*/
function output_fdf ($pdf_file, $pdf_data) {
$fdf = "%FDF-1.2\n%����\n";
$fdf .= "1 0 obj \n<< /FDF ";
$fdf .= "<< /Fields [\n";
foreach ($pdf_data as $key => $val)
$fdf .= "<< /V ($val)/T ($key) >> \n";
$fdf .= "]\n/F ($pdf_file) >>";
$fdf .= ">>\nendobj\ntrailer\n<<\n";
$fdf .= "/Root 1 0 R \n\n>>\n";
$fdf .= "%%EOF";
/* Now we display the FDF data which causes Acrobat to start */
header ("Content-Type: application/vnd.fdf");
print $fdf;
}
noah at NOSPAMbrandfidelity dot com
09-Mar-2002 04:26
function parse($file) {
if (!preg_match_all("/<<\s*\/V([^>]*)>>/x",
$file,$out,PREG_SET_ORDER))
return;
for ($i=0;$i<count($out);$i++) {
$pattern = "<<.*/V\s*(.*)\s*/T\s*(.*)\s*>>";
$thing = $out[$i][1];
if (eregi($pattern,$out[$i][0],$regs)) {
$key = $regs[2];
$val = $regs[1];
$key = preg_replace("/^\s*\(/","",$key);
$key = preg_replace("/\)$/","",$key);
$key = preg_replace("/\\\/","",$key);
$val = preg_replace("/^\s*\(/","",$val);
$val = preg_replace("/\)$/","",$val);
$matches[$key] = $val;
}
}
return $matches;
}
gregDELETETHIS at laundrymat dot tv
21-Dec-2001 11:06
Here is an easy script to output fdf data to the browser without using the fdf toolkit or creating an actual fdf file on the server.
By the way acrobat is very picky about line breaks so you must leave the "\n" in the script. The script reads the variables posted to it from a form use POST and creates a fdf file from them. The field names posted to this script must match the field names in the pdf. Acrobat will ignore any that don't match.
<?php
$url="";
$values=$HTTP_POST_VARS;
$fdfdata = "%FDF-1.2\n%����\n";
$fdfdata .= "1 0 obj \n<< /FDF ";
$fdfdata .= "<< /Fields [\n";
foreach($values as $key=>$val)
{
$fdfdata.="<< /V ($val)/T ($key) >> ";
}
$fdfdata .= "]\n";
$fdfdata .= "/F ($url) >>";
$fdfdata .= ">>\nendobj\ntrailer\n<<\n/Root 1 0 R\n>>\n";
$fdfdata .= "%%EOF";
header ("Content-Type: application/vnd.fdf");
print $fdfdata;
?>
You can use javascript in the pdf to read the values from a GET method posted directly to the pdf. you can see both methods here:
joe at koontz dot net
28-Apr-2000 11:42
The simplest thing to do is get the FDF data from $HTTP_RAW_POST_DATA. (unless you have the server library installed none of the fdf data gets parsed!) This is typical of what you get:
%FDF-1.2
1 0 obj
<<
/FDF << /Fields [ << /V (0)/T (amount0)>> << /V (0)/T (amount1)>> << /V (0)/T (amount2)>>
<< /V (0)/T (amount3)>> << /V (0)/T (amount4)>> << /V (0)/T (amount5)>>
<< /V (0)/T (amount6)>> << /V (0)/T (amount7)>> << /V (0)/T (amount8)>>
<< /V (0)/T (amount9)>> << /V /0102 /T (chase_bk)>> << /V (0)/T (count)>>
<< /V (0)/T (invtotal)>> << /V (12/21/2000)/T (sent_ap)>> << /V /Off /T (spec_hand)>>
<< /V (041232)/T (transit_no)>> << /V (THIS FORM IS NOT COMPLETE!!!)/T (X)>>
]
/F ()>>
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF
kill everything before the [ and then parse it down into key value pairs.
I wrote this to create an FDF, make sure you do a
header("Content-type: application/vnd.fdf");
before you echo the returned value to the user.
function FDFput(,$FDFpage)
{
$A = "%FDF-1.2\n1 0 obj\n<< \n/FDF << /Fields [ \n";
$C = " ] \n" ;
if ($FDFpage>"" ) {$C .=" /F ($FDFpage)>>\n";}
$C .= ">>\n>> \nendobj\ntrailer\n\n<</Root 1 0 R>>\n%%EOF\n";
$B = "";
reset($FDFData);
while (list($key, $val) = each($FDFData))
{
if (strlen(trim($val)) > 0 && is_string($key))
{
$B .= "<</T ($key) /V (". $val . ")>>\n";
//echo "<</T ($key) /V (". $val . ")>>\n";
}
}
return $A.$B.$C;
}
It ain't perfect - but it works. (I use HTML for posting to the server, FDF to the browser)
joe
| |