|
|
 |
XXXI. Funzioni Forms Data FormatIntroduzione
Il Forms Data Format (FDF) è un formato per la gestione
di form all'interno di documenti PDF. Si dovrebbe leggere la documentazione
al link http://partners.adobe.com/asn/acrobat/forms.jsp
per avere maggiori informazioni su cosa sia FDF e su come usarlo in generale.
L'idea di base è che FDF sia simile ai form HTML. Fondamentalmente la differenza consiste
nel formato con cui i dati sono inviati al server quando viene
premuto il bottone di sottomissione del form (che, ovviamente, è in Form Data Format)
e il formato del form stesso (che è il Portable Document Format, PDF).
L'elaborazione dei dati FDF è una delle caratteristiche delle funzioni
fdf.Ma ve ne sono altre. Una di queste consiste nel prendere un form PDF
e compilarne i campi senza modificare il form .
In questo caso si dovrebbe creare il documento FDF
(fdf_create()) impostare i valori per ciascun campo
(fdf_set_value()) e associarlo al form PDF
(fdf_set_file()). Infine viene inviato al browser
browser con MimeType application/vnd.fdf. Acrobat Reader
riconosce il MimeType, legge il form PDF associato
e completa i campi con i dati dal documento FDF.
Se si apre un documento FDF con un editor di testo si troveranno
degli oggetti con nome FDF. Tali oggetti
possono contenere diversi campi tipo Fields,
F, Status etc..
I campi più comuni sono Fields che puntano
alla lista dei campi di input, e F che contiene
il nome del file PDF a cui appartengono questi dati. Questi campi
sono definiti dalla documentazione FDF come chiave /F (/F-Key) e chiave /Status (/Status-Key)
Le modifiche a questi chiavi posso essere svolte con funzioni tipo
fdf_set_file() e
fdf_set_status(). I campi sono modificati dalle funzioni
fdf_set_value(), fdf_set_opt() etc..
Requisiti
Occorre avere disponibile il toolkit FDF SDK, scaricabile da
http://partners.adobe.com/asn/acrobat/forms.jsp.
Dal PHP 4.3 occorre avere almeno l'SDK versione 5.0.
Le librerie FDF sono disponibili in formato binario
solo per le piattaforme supportate da Adobe quali Win32, Linux, Solaris e AIX.
Installazione
Occorre compilare il PHP con
--with-fdftk[=DIR].
Nota:
Se si hanno problemi nella configurazione del PHP con supporto fdftk, verificare
che i file fdftk.h e libfdftk.so
siano nel posto corretto.
Lo script di configurazione supporta sia la struttura delle directory
della distribuzione FDFSDK, sia l'usuale struttura DIR/include /
DIR/lib, pertanto si può puntare o
direttamente alla directory della distribuzione decompressa o
posizionare il file con l'header e la libreria appropriata per la piattaforma,
ad es. /usr/local/include e
/usr/local/lib ed eseguire configure con
--with-fdftk=/usr/local.
Nota per gli utenti Win32:
Per potere abilitare questo modulo sui sistemi Windows, occorre copiare
fdftk.dll dalla cartella DLL del rilascio
PHP7Win32 alla cartella SYSTEM32 della macchina Windows. (Es. C:\WINNT\SYSTEM32
oppure C:\WINDOWS\SYSTEM32).
Configurazione di RuntimeQuesta estensione non definisce
alcuna direttiva di configurazione in php.ini Tipi di risorsefdf
La maggior parte delle funzioni richiedono una risorsa fdf
come primo parametro. La risorsa fdf è un puntatore
al file fdf aperto. Le risorse fdf possono essere
ottenute dalle funzioni fdf_create(),
fdf_open() e fdf_open_string().
Costanti predefinite
Queste costanti sono definite da questa estensione e
sono disponibili solo se l'estensione è stata compilata
nel PHP o se è stata caricata dinamicamente a runtime.
Esempi
Il seguente esempio illustra l'elaborazione dei dati di un form.
Esempio 1. Elaborazione di un documento FDF |
<?php
$fdf = fdf_open_string($HTTP_FDF_DATA);
$volume = fdf_get_value($fdf, "volume");
echo "TIl campo volume ha valore '<b>$volume</b>'<br />";
$date = fdf_get_value($fdf, "date");
echo "Il campo date ha valore '<b>$date</b>'<br />";
$comment = fdf_get_value($fdf, "comment");
echo "Il campo commento ha valore '<b>$comment</b>'<br />";
if (fdf_get_value($fdf, "show_publisher") == "On") {
$publisher = fdf_get_value($fdf, "publisher");
echo "Il campo publisher ha valore '<b>$publisher</b>'<br />";
} else
echo "Publisher non deve essere visualizzato.<br />";
if (fdf_get_value($fdf, "show_preparer") == "On") {
$preparer = fdf_get_value($fdf, "preparer");
echo "Il campo preparer ha valore '<b>$preparer</b>'<br />";
} else
echo "Preparer non deve essere visualizzato.<br />";
fdf_close($fdf);
?>
|
|
add a note
User Contributed Notes
Funzioni Forms Data Format
software at yvanrodrigues dot com
25-Mar-2004 08: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 01: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 11: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 12: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 02: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 06: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='http://ns.adobe.com/xfdf/' 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 10: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 05: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 "http://domain.com/path/to/form.pdf" 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 02: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 09: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="http://www.some_url.com/form.pdf";
$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: http://laundrymat.tv/pdf/
joe at koontz dot net
28-Apr-2000 10: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 (http://x.com/forms/AA00390q.pdf)>>
>>
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
| |