Skip to content

Commit

Permalink
add custom checkbox parser (opt-in mode)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeshell committed Jun 11, 2019
1 parent 1eddba7 commit 0375dd9
Showing 1 changed file with 114 additions and 4 deletions.
118 changes: 114 additions & 4 deletions src/fpdm.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@
* V1.0 (03.11.2010) First working release *
*******************************************************************************/

global $FPDM_FILTERS, $FPDM_REGEXPS; //needs explicit global scope, otherwise autoloading will br incomplete.
global $FPDM_FILTERS, $FPDM_REGEXPS; //needs explicit global scope, otherwise autoloading will be incomplete.
$FPDM_FILTERS=array(); //holds all supported filters
$FPDM_REGEXPS= array(
//FIX: parse checkbox definition
"/AS"=>"/\/AS\s+\/(\w+)$/",
"name"=>"/\/(\w+)/",
// "/AP_D_SingleLine"=>"/\/D\s+\/(\w+)\s+\d+\s+\d+\s+R\s+\/(\w+)$/",
//ENDFIX
"/Type"=>"/\/Type\s+\/(\w+)$/",
"/Subtype" =>"/^\/Subtype\s+\/(\w+)$/"
);
Expand Down Expand Up @@ -66,7 +71,7 @@

class FPDM {
//@@@@@@@@@

var $useCheckboxParser = false; //boolean: allows activation of custom checkbox parser (not available in original FPDM source)

var $pdf_source = ''; //string: full pathname to the input pdf , a form file
var $fdf_source = ''; //string: full pathname to the input fdf , a form data file
Expand Down Expand Up @@ -867,7 +872,10 @@ function set_field_value($type,$name,$value) {

$offset_shift=$this->set_field_tooltip($name,$value);

} else {//if(isset($this->value_entries["$name"]["values"]["$type"])) {
} elseif ($this->useCheckboxParser && isset($this->value_entries["$name"]['infos']['checkbox_state'])) { //FIX: set checkbox value
$offset_shift=$this->set_field_checkbox($name, $value);
//ENDFIX
} else {//if(isset($this->value_entries["$name"]["values"]["$type"])) {
// echo $this->value_entries["$name"]["values"]["$type"];
/* $field_value_line=$this->value_entries["$name"]["values"]["$type"];
$field_value_maxlen=$this->value_entries["$name"]["constraints"]["maxlen"];
Expand Down Expand Up @@ -921,6 +929,60 @@ function set_field_tooltip($name,$value) {
return $offset_shift;
}

//FIX: parse checkbox definition
/**
*Changes the checkbox state.
*
*@param string $name name of the field to change the state
*@param string $value the new state to set
*@return int offset_shift the size variation
**/
public function set_field_checkbox($name, $value)
{
//------------------------------------
$offset_shift=0;
$verbose_set=($this->verbose&&($this->verbose_level>1));
//Get the line(s) of the misc field values
if (isset($this->value_entries["$name"])) {
if (isset($this->value_entries["$name"]["infos"]["checkbox_state_line"])
&& isset($this->value_entries["$name"]["infos"]["checkbox_no"])
&& isset($this->value_entries["$name"]["infos"]["checkbox_yes"])) {
$field_checkbox_line=$this->value_entries["$name"]["infos"]["checkbox_state_line"];
if ($field_checkbox_line) {
if ($verbose_set) {
echo "<br>Change checkbox of the field $name at line $field_checkbox_line to value [$value]";
}
$state = $this->value_entries["$name"]["infos"]["checkbox_no"];
if ($value) {
$state = $this->value_entries["$name"]["infos"]["checkbox_yes"];
}
$CurLine =$this->pdf_entries[$field_checkbox_line];
$OldLen=strlen($CurLine);
$CurLine = '/AS /'.$state;
$NewLen=strlen($CurLine);
$Shift=$NewLen-$OldLen;
$this->shift=$this->shift+$Shift;
//Saves
$this->pdf_entries[$field_checkbox_line]=$CurLine;
return $Shift;
// $offset_shift=$this->_set_field_value($field_checkbox_line, $state);
} else {
if ($verbose_set) {
echo "<br>Change checkbox value aborted, parsed checkbox definition incomplete.";
}
}
} else {
if ($verbose_set) {
echo "<br>Change checkbox value aborted, the field $name has no checkbox definition.";
}
}
} else {
$this->Error("set_field_checkbox failed as the field $name does not exist");
}
return $offset_shift;
}
//ENDFIX

/**
*Dumps the line entries
*
Expand Down Expand Up @@ -1566,7 +1628,14 @@ function parsePDFEntries(&$lines){

$Counter=0;
$obj=0; //this is an invalid object id, we use it to know if we are into an object
$type='';
//FIX: parse checkbox definition
$ap_d_yes='';
$ap_d_no='';
$ap_line=0;
$ap_d_line=0;
$as='';
//ENDFIX
$type='';
$subtype='';
$name='';
$value='';
Expand Down Expand Up @@ -1642,6 +1711,13 @@ function parsePDFEntries(&$lines){

$object=null;
$obj=0;
//FIX: parse checkbox definition
$ap_d_yes='';
$ap_d_no='';
$ap_line=0;
$ap_d_line=0;
$as='';
//ENDFIX
$type='';
$subtype='';
$name='';
Expand Down Expand Up @@ -1768,6 +1844,40 @@ function parsePDFEntries(&$lines){
//=== DEFINITION ====
//preg_match("/^\/Type\s+\/(\w+)$/",$CurLine,$match)
$match=array();
//FIX: parse checkbox definition
if($this->useCheckboxParser && ('' == $ap_d_yes || '' == $ap_d_no || '' == $as)) {
if (!$ap_line && '/AP' == substr($CurLine, 0, 3)) {
if ($verbose_parsing) {
echo("<br>Found AP Line '<i>$Counter</i>'");
}
$ap_line = $Counter;
} elseif (!$ap_d_line && '/D' == substr($CurLine, 0, 2)) {
if ($verbose_parsing) {
echo("<br>Found D Line '<i>$Counter</i>'");
}
$ap_d_line = $Counter;
} elseif (($ap_line==$Counter-4)&&($ap_d_line==$Counter-2)&&($ap_d_yes=='')&&$this->extract_pdf_definition_value("name", $CurLine, $match)) {
$ap_d_yes=$match[1];
if ($verbose_parsing) {
echo("<br>Object's checkbox_yes is '<i>$ap_d_yes</i>'");
}
$object["infos"]["checkbox_yes"]=$ap_d_yes;
} elseif (($ap_line==$Counter-5)&&($ap_d_line==$Counter-3)&&($ap_d_no=='')&&$this->extract_pdf_definition_value("name", $CurLine, $match)) {
$ap_d_no=$match[1];
if ($verbose_parsing) {
echo("<br>Object's checkbox_no is '<i>$ap_d_no</i>'");
}
$object["infos"]["checkbox_no"]=$ap_d_no;
} elseif (($as=='')&&$this->extract_pdf_definition_value("/AS", $CurLine, $match)) {
$as=$match[1];
if ($verbose_parsing) {
echo("<br>Object's AS is '<i>$as</i>'");
}
$object["infos"]["checkbox_state"]=$as;
$object["infos"]["checkbox_state_line"]=$Counter;
}
}
//ENDFIX
if(($type=='')||($subtype=='')||($name=="")) {

if(($type=='')&&$this->extract_pdf_definition_value("/Type",$CurLine,$match)) {
Expand Down

0 comments on commit 0375dd9

Please sign in to comment.