Skip to content

Commit

Permalink
Update src/export/pdf/pdftk.php to run exec pdftk from global install…
Browse files Browse the repository at this point in the history
… dir. Update src/FPDM.php to read correct input file dir for output.
  • Loading branch information
Crov committed Dec 13, 2018
1 parent b68ee26 commit 499fee2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 58 deletions.
28 changes: 14 additions & 14 deletions src/FPDM.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,9 @@ public function Merge($flatten = false)
if ($count_fields) {
foreach ($fields as $name => $value) {
$this->set_field_value("current", $name, $value);
// $value=''; //Strategy applies only to current value, clear others
// $this->set_field_value("default",$name,$value);
// $this->set_field_value("tooltip",$name,$value);
// $value=''; //Strategy applies only to current value, clear others
// $this->set_field_value("default",$name,$value);
// $this->set_field_value("tooltip",$name,$value);
}
}
//===========================================================
Expand Down Expand Up @@ -544,7 +544,7 @@ public function Output($name = '', $dest = '')
require_once("export/pdf/pdftk.php");//Of course don't forget to bridge to PDFTK!

$tmp_file = false;
$pdf_file = resolve_path(fix_path(dirname(__FILE__) . '/' . $this->pdf_source)); //string: full pathname to the input pdf , a form file
$pdf_file = $this->pdf_source; //string: full pathname to the input pdf , a form file

if ($this->fdf_source) { //FDF file provided
$fdf_file = resolve_path(fix_path(dirname(__FILE__) . '/' . $this->fdf_source));
Expand Down Expand Up @@ -586,7 +586,7 @@ public function Output($name = '', $dest = '')

$ret = pdftk($pdf_file, $fdf_file, array("security" => $security, "output_modes" => $output_modes));

if ($tmp_file) @unlink($fdf_file); //Clear cache
if ($tmp_file) unlink($fdf_file); //Clear cache

if ($ret["success"]) {
$pdf_file = $ret["return"];
Expand Down Expand Up @@ -838,8 +838,8 @@ protected function set_field_value($type, $name, $value)
$offset_shift = $this->set_field_tooltip($name, $value);

} 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"];
// 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"];
if($field_value_maxlen) //Truncates the size if needed
Expand All @@ -852,8 +852,8 @@ protected function set_field_value($type, $name, $value)
else
$offset_shift = $this->_set_field_value2($this->value_entries[$name]["infos"]["name_line"], $value, true);
}
// }else
// $this->Error("set_field_value failed as invalid valuetype $type for object $object_id");
// }else
// $this->Error("set_field_value failed as invalid valuetype $type for object $object_id");


//offset size shift will affect the next objects offsets taking into accound the order they appear in the file--
Expand Down Expand Up @@ -1604,14 +1604,14 @@ public function parsePDFEntries(&$lines)
$lines["$name"] = $object;
if ($verbose_parsing) $this->dumpContent("$type $subtype (obj id=$obj) is a text annotation of name '$name', saves it.");
}//else
// $this->Error("$type $subtype (obj id=$obj) is a text annotation without a name, this cannot be.");
// $this->Error("$type $subtype (obj id=$obj) is a text annotation without a name, this cannot be.");


$values = $object["values"];

//Sanity values checks, watchdog.
// if(!array_key_exists("current",$values)) $this->Error("Cannot find value (/V) for field $name");
// if(!array_key_exists("default",$values)) $this->Error("Cannot find default value (/DV) for field $name");
// if(!array_key_exists("current",$values)) $this->Error("Cannot find value (/V) for field $name");
// if(!array_key_exists("default",$values)) $this->Error("Cannot find default value (/DV) for field $name");

} else
if ($verbose_parsing) $this->dumpContent("Object $type $subtype (obj id=$obj) is not supported");
Expand Down Expand Up @@ -1815,13 +1815,13 @@ public function parsePDFEntries(&$lines)
}

//TODO: Fetch the XObject..and change Td <> Tj
/* if(preg_match("/^\/AP/",$CurLine,$values)) {
/* if(preg_match("/^\/AP/",$CurLine,$values)) {
//die("stop");
$CurLine=''; //clear link to Xobject
$entries[$Counter]=$CurLine;
}*/

// }
// }

}

Expand Down
50 changes: 6 additions & 44 deletions src/export/pdf/pdftk.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,53 +94,15 @@ function pdftk($pdf_file,$fdf_file,$settings) {
$cmd="pdftk"; //For linux and mac
}

$dircmd=fix_path(dirname(__file__));

if(file_exists("$dircmd/$cmd")) {

$pdf_out=FPDM_CACHE."pdf_flatten.pdf";
$pdf_out=FPDM_CACHE."pdf_flatten.pdf";

$cmdline="$dircmd/$cmd \"$pdf_file\" fill_form \"$fdf_file\" output \"$pdf_out\" $output_modes $security"; //direct to ouptut

//echo htmlentities("$cmdline , $descriptorspec, $cwd, $env");

if(PHP5_ENGINE) { // Php5
$process = proc_open($cmdline, $descriptorspec, $pipes, $cwd, $env);
}else { //Php4
$process = proc_open($cmdline, $descriptorspec, $pipes);
}

if (is_resource($process)) {

if(PHP5_ENGINE) {
$err=stream_get_contents($pipes[2]);
}else { //Php4
$err= "";
while (($str = fgets($pipes[2], 4096))) {
$err.= "$str\n";
}
}

fclose($pipes[2]);

//Its important to close the pipes before proc_close call to avoid dead locks
$return_value = proc_close($process);

}else {
$err="No more resource to execute the command";
}

}else {
$err="Sorry but pdftk binary is not provided / Cette fonctionnalite requiere pdftk non fourni ici<ol>";
$err.="<li>download it from / telecharger ce dernier a partir de <br><blockquote><a href=\"http://www.pdflabs.com/docs/install-pdftk/\">pdflabs</a></blockquote>";
$err.="<li>copy the executable in this directory / Copier l'executable dans<br><blockquote><b>$dircmd</b></blockquote>" ;
$err.="<li>set \$cmd to match binary name in / configurer \$cmd pour qu'il corresponde dans le fichier<br><blockquote><b>".__file__."</b></blockquote></ol>";
}
exec("pdftk \"$pdf_file\" fill_form \"$fdf_file\" output \"$pdf_out\" $output_modes $security", $output, $return);

if($err) {
$ret=array("success"=> false,"return"=>$err);
}else
// Return will return non-zero upon an error
if(!$return) {
$ret=array("success"=> true,"return"=>$pdf_out);
}else
$ret=array("success"=> false,"return"=>$err);

return $ret;
}
Expand Down

0 comments on commit 499fee2

Please sign in to comment.