commitall

This commit is contained in:
Sampanna Rimal
2024-07-10 18:28:19 +05:45
parent 140abda4e6
commit 9cd05ef3cb
15723 changed files with 4818733 additions and 0 deletions

View File

@ -0,0 +1,125 @@
<?php
/****************************************************************
*@file fdf.php
*@name Generates fdf files
*@internal fdf bridge to forge_fdf
*@package fpdftk
*@date 2010-12-05
*@author 0livier
*@version 2.1
*@note
* V2.1 (05.12.2010) Adaptation for FPDM
* V2.0 (27.10.2010) USe of URL TOOLBOX package
* V1.0 (22.10.2010) First working release
******************************************************************/
require ("forge_fdf.php");
if (!defined('URL_TOOLBOX')) die("Requires the URL_TOOLBOX package!");
/**
*Resolves relative pdf urls to absolute
*
*@note pdf paths MUST BE ABSOLUTE in the fdf file or http scheme because when path contains .. then fdf fails
*@param String $pdf_url any url
*@return String $url the absolute url
**/
function resolve_pdf_url($pdf_url) {
//----------------------------------
$url=resolve_url($pdf_url);
return $url;
}
/**
*Generates a form definition file (fdf)
*
*@note error message is dumped into syslog if supported
*@todo Expand support not only to fdf_data_strings (I don't need this feature)
*@param String $pdf_url
*@param Array $pdf_data the array that holds fields datas (field_name => field_value
*@param String $output_mode
* 'D' : WARNING!! By default, THIS FUNCTION SENDS HTTP HEADERS! It MUST be called before
* any content is spooled to the browser, or the function will fail!
* 'S' : Return the fdf file generated as a string
* <fdf_file> fullpathname to where the fdf file content has to be saved.
*@return mixed ret the return value which can be:
* -a boolean true when output_mode is set to 'D'
* -a text the fdf content when output_mode is set to 'S'
* -an array holding success flag with either the fdf size or the error message
**/
function output_fdf($pdf_url,$pdf_data,$output_mode='D') {
//---------------------------------------------------------
// Ensures pdf path is absolute
$pdf_form_url=resolve_pdf_url($pdf_url);
// string data, used for text fields, combo boxes and list boxes
$fdf_data_strings=$pdf_data;
// name data, used for checkboxes and radio buttons
// (e.g., /Yes and /Off for true and false)
$fdf_data_names=array();
//fields security and accessibility attributes
$fields_hidden=array();
$fields_readonly=array();
$fdf=forge_fdf( $pdf_form_url,
$fdf_data_strings,
$fdf_data_names,
$fields_hidden,
$fields_readonly );
switch($output_mode) {
case "D"://Send the fdf header so acrobat recognize it.
header ("Content-Type: application/vnd.fdf");
print $fdf;
$ret=true;
break;
case "S"://String
$ret=$fdf;
break;
default:// write the file out
$error_fdf_access='';
$fdf_file=$output_mode;
$fdf_dir=dirname($fdf_file);
//Parano<6E>d access mode with syslog in background as watchdog for errors
if(file_exists($fdf_dir)) {
if(is_writable($fdf_dir)) {
if(!is_writable($fdf_file)&&false) { //Create
$error_fdf_access="can not write fdf file ($fdf_file), disk full or missing rights?";
}
}else {
$error_fdf_access="can not write into fdf's directory ($fdf_dir)";
}
}else {
$error_fdf_access="can not access to fdf's directory ($fdf_dir)";
}
$success=false;
if($error_fdf_access !="") {
$err="output_fdf : Unable to create fdf file '".$fdf_file."'<br> because $error_fdf_access.";
} else {
if($fp=fopen($fdf_file,'w')){
$err=fwrite($fp,$fdf,strlen($fdf));
if(function_exists('syslog')) syslog(LOG_WARNING,"FDF file '".$output_mode."' written successfully ($err bytes)");
$success=true;
}else{
$err="output_fdf : Unable to generate file '".$output_mode."', disk full or corrupted?.";
}
fclose($fp);
}
$ret=array("success"=>$success,"return"=>$err);
}
return $ret;
}
?>

View File

@ -0,0 +1,146 @@
<?php
/* forge_fdf, by Sid Steward
version 1.0
visit: www.pdfhacks.com/forge_fdf/
For text fields, combo boxes and list boxes, add
field values as a name => value pair to $fdf_data_strings.
For check boxes and radio buttons, add field values
as a name => value pair to $fdf_data_names. Typically,
true and false correspond to the (case sensitive)
names "Yes" and "Off".
Any field added to the $fields_hidden or $fields_readonly
array must also be a key in $fdf_data_strings or
$fdf_data_names; this might be changed in the future
Any field listed in $fdf_data_strings or $fdf_data_names
that you want hidden or read-only must have its field
name added to $fields_hidden or $fields_readonly; do this
even if your form has these bits set already
PDF can be particular about CR and LF characters, so I
spelled them out in hex: CR == \x0d : LF == \x0a
*/
function escape_pdf_string( $ss )
{
$ss_esc= '';
$ss_len= strlen( $ss );
for( $ii= 0; $ii< $ss_len; ++$ii ) {
if( ord($ss{$ii})== 0x28 || // open paren
ord($ss{$ii})== 0x29 || // close paren
ord($ss{$ii})== 0x5c ) // backslash
{
$ss_esc.= chr(0x5c).$ss{$ii}; // escape the character w/ backslash
}
else if( ord($ss{$ii}) < 32 || 126 < ord($ss{$ii}) ) {
$ss_esc.= sprintf( "\\%03o", ord($ss{$ii}) ); // use an octal code
}
else {
$ss_esc.= $ss{$ii};
}
}
return $ss_esc;
}
/**
$key = addcslashes($key, "\n\r\t\\()");
$val = addcslashes($val, "\n\r\t\\()");
**/
function escape_pdf_name( $ss )
{
$ss_esc= '';
$ss_len= strlen( $ss );
for( $ii= 0; $ii< $ss_len; ++$ii ) {
if( ord($ss{$ii}) < 33 || 126 < ord($ss{$ii}) ||
ord($ss{$ii})== 0x23 ) // hash mark
{
$ss_esc.= sprintf( "#%02x", ord($ss{$ii}) ); // use a hex code
}
else {
$ss_esc.= $ss{$ii};
}
}
return $ss_esc;
}
/**
* Generates the fdf code
*
*@param String $pdf_form_url: 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.
*@param Array $fdf_data_strings: an array of any fields in $pdf_form_url 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.
*@return String
**/
function forge_fdf( $pdf_form_url,
$fdf_data_strings,
$fdf_data_names,
$fields_hidden,
$fields_readonly )
{
$fdf = "%FDF-1.2\x0d%\xe2\xe3\xcf\xd3\x0d\x0a"; // header
$fdf.= "1 0 obj\x0d<< "; // open the Root dictionary
$fdf.= "\x0d/FDF << "; // open the FDF dictionary
$fdf.= "/Fields [ "; // open the form Fields array
// string data, used for text fields, combo boxes and list boxes
foreach( $fdf_data_strings as $key => $value ) {
$fdf.= "<< /V (".escape_pdf_string($value).")".
"/T (".escape_pdf_string($key).") ";
if( in_array( $key, $fields_hidden ) )
$fdf.= "/SetF 2 ";
else
$fdf.= "/ClrF 2 ";
if( in_array( $key, $fields_readonly ) )
$fdf.= "/SetFf 1 ";
else
$fdf.= "/ClrFf 1 ";
$fdf.= ">> \x0d";
}
// name data, used for checkboxes and radio buttons
// (e.g., /Yes and /Off for true and false)
foreach( $fdf_data_names as $key => $value ) {
$fdf.= "<< /V /".escape_pdf_name($value).
" /T (".escape_pdf_string($key).") ";
if( in_array( $key, $fields_hidden ) )
$fdf.= "/SetF 2 ";
else
$fdf.= "/ClrF 2 ";
if( in_array( $key, $fields_readonly ) )
$fdf.= "/SetFf 1 ";
else
$fdf.= "/ClrFf 1 ";
$fdf.= ">> \x0d";
}
$fdf.= "] \x0d"; // close the Fields array
// the PDF form filename or URL, if given
if( $pdf_form_url ) {
$fdf.= "/F (".escape_pdf_string($pdf_form_url).") \x0d";
}
$fdf.= ">> \x0d"; // close the FDF dictionary
$fdf.= ">> \x0dendobj\x0d"; // close the Root dictionary
// trailer; note the "1 0 R" reference to "1 0 obj" above
$fdf.= "trailer\x0d<<\x0d/Root 1 0 R \x0d\x0d>>\x0d";
$fdf.= "%%EOF\x0d\x0a";
return $fdf;
}
?>