From e376dc157655ded24c61e098199586f3325d63c1 Mon Sep 17 00:00:00 2001 From: Thorsten Witteler Date: Thu, 9 Nov 2017 16:32:48 +0100 Subject: [PATCH] fix filling forms in multiple files. (wrong buffer usage, invalid offsets). fix deprecated coding (php 7.x). --- README.md | 5 +-- fpdm.php | 91 +++++++++++++++++++++++++++---------------------------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index dfb13a0..ba92ba0 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # Form filling The FPDM class allows to fill out PDF forms, i.e. populate fields of a PDF file. It is **developed by Olivier Plathey**, author of the [FDPF Library](http://www.fpdf.org/), and has been released as [Skript 93](http://www.fpdf.org/en/script/script93.php). -I created this repository for two reasons: -- make the current FPDM source available via [composer](https://packagist.org/packages/codeshell/fpdm) +I created this repository for the following reasons: +- make the current FPDM source available via [composer](https://packagist.org/packages/tmw/fpdm) - fix compatibility issues with PHP 7.x +- bugfixing This repository only contains the separate php class written for form filling. If you are looking for a repository containing the main FPDF Library, please head over to [github.com/Setasign/FPDF](https://github.com/Setasign/FPDF). diff --git a/fpdm.php b/fpdm.php index c9a1bbb..f1e2c7c 100644 --- a/fpdm.php +++ b/fpdm.php @@ -64,45 +64,45 @@ class FPDM { //@@@@@@@@@ - 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 - var $pdf_output; //string: full pathname to the resulting filled pdf - - var $pdf_entries; //array: Holds the content of the pdf file as array - var $fdf_content; //string: holds the content of the fdf file - var $fdf_parse_needed;//boolean: false will use $fields data else extract data from fdf content - var $value_entries; //array: a map of values to faliclitate access and changes - - var $positions; //array, stores what object id is at a given position n ($positions[n]=) + 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 + var $pdf_output = ''; //string: full pathname to the resulting filled pdf + + var $pdf_entries = array(); //array: Holds the content of the pdf file as array + var $fdf_content = ''; //string: holds the content of the fdf file + var $fdf_parse_needed = false;//boolean: false will use $fields data else extract data from fdf content + var $value_entries = array(); //array: a map of values to faliclitate access and changes + + var $positions = array(); //array, stores what object id is at a given position n ($positions[n]=) - var $offsets; //array of offsets for objects, index is the object's id, starting at 1 - var $pointer; //integer, Current line position in the pdf file during the parsing + var $offsets = array(); //array of offsets for objects, index is the object's id, starting at 1 + var $pointer = 0; //integer, Current line position in the pdf file during the parsing - var $shifts; //array, Shifts of objects in the order positions they appear in the pdf, starting at 0. - var $shift; //integer, Global shift file size due to object values size changes + var $shifts = array(); //array, Shifts of objects in the order positions they appear in the pdf, starting at 0. + var $shift = 0; //integer, Global shift file size due to object values size changes - var $streams; //Holds streams configuration found during parsing - var $streams_filter; //Regexp to decode filter streams + var $streams = ''; //Holds streams configuration found during parsing + var $streams_filter = ''; //Regexp to decode filter streams - var $safe_mode; //boolean, if set, ignore previous offsets do no calculations for the new xref table, seek pos directly in file - var $check_mode; //boolean, Use this to track offset calculations errors in corrupteds pdfs files for sample - var $halt_mode; //if true, stops when offset error is encountered + var $safe_mode = false; //boolean, if set, ignore previous offsets do no calculations for the new xref table, seek pos directly in file + var $check_mode = false; //boolean, Use this to track offset calculations errors in corrupteds pdfs files for sample + var $halt_mode = false; //if true, stops when offset error is encountered - var $info; //array, holds the info properties - var $fields; //array that holds fields-Data parsed from FDF + var $info = array(); //array, holds the info properties + var $fields = array(); //array that holds fields-Data parsed from FDF - var $verbose; //boolean , a debug flag to decide whether or not to show internal process - var $verbose_level; //integer default is 1 and if greater than 3, shows internal parsing as well + var $verbose = false; //boolean , a debug flag to decide whether or not to show internal process + var $verbose_level = 1; //integer default is 1 and if greater than 3, shows internal parsing as well - var $support; //string set to 'native' for fpdm or 'pdftk' for pdf toolkit - var $flatten_mode; //if true, flatten field data as text and remove form fields (NOT YET SUPPORTED BY FPDM) - var $compress_mode; //boolean , pdftk feature only to compress streams - var $uncompress_mode; //boolean pdftk feature only to uncompress streams - var $security; //Array holding securtity settings + var $support = ''; //string set to 'native' for fpdm or 'pdftk' for pdf toolkit + var $flatten_mode = false; //if true, flatten field data as text and remove form fields (NOT YET SUPPORTED BY FPDM) + var $compress_mode = false; //boolean , pdftk feature only to compress streams + var $uncompress_mode = false; //boolean pdftk feature only to uncompress streams + var $security = array(); //Array holding securtity settings //(password owner nad user, encrypt (set to 40 or 128 or 0), allow ] see pdfk help - var $needAppearancesTrue; //boolean, indicates if /NeedAppearances is already set to true - var $isUTF8; //boolean (true for UTF-8, false for ISO-8859-1) + var $needAppearancesTrue = false; //boolean, indicates if /NeedAppearances is already set to true + var $isUTF8 = false; //boolean (true for UTF-8, false for ISO-8859-1) /** * Constructor @@ -112,7 +112,7 @@ class FPDM { *@param string $fdf_source Source-Filename *@param boolean $verbose , optional false per default */ - function FPDM() { + function __construct() { //============== $args=func_get_args(); @@ -617,7 +617,7 @@ function Output($dest='', $name=''){ $this->Error($ret["return"]); } - $this->buffer=$this->get_buffer($pdf_file); + //$this->buffer=$this->get_buffer($pdf_file); $dest=strtoupper($dest); @@ -649,13 +649,13 @@ function Output($dest='', $name=''){ header('Content-Type: application/pdf'); if(headers_sent()) $this->Error('Some data has already been output, can\'t send PDF file'); - header('Content-Length: '.strlen($this->buffer)); + header('Content-Length: '.strlen($this->get_buffer())); header('Content-Disposition: inline; filename="'.$name.'"'); header('Cache-Control: private, max-age=0, must-revalidate'); header('Pragma: public'); ini_set('zlib.output_compression','0'); } - echo $this->buffer; + echo $this->get_buffer(); break; case 'D': //Download file @@ -664,7 +664,7 @@ function Output($dest='', $name=''){ header('Content-Type: application/x-download'); if(headers_sent()) $this->Error('Some data has already been output, can\'t send PDF file'); - header('Content-Length: '.strlen($this->buffer)); + header('Content-Length: '.strlen($this->get_buffer())); header('Content-Disposition: attachment; filename="'.$name.'"'); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past @@ -676,7 +676,7 @@ function Output($dest='', $name=''){ header('Cache-Control: private, max-age=0, must-revalidate'); header('Pragma: public,no-cache'); ini_set('zlib.output_compression','0'); - echo $this->buffer; + echo $this->get_buffer(); break; case 'F': //Save to local file @@ -685,12 +685,12 @@ function Output($dest='', $name=''){ if(!$f) $this->Error('Unable to create output file: '.$name.' (currently opened under Acrobat Reader?)'); - fwrite($f,$this->buffer,strlen($this->buffer)); + fwrite($f,$this->get_buffer(),strlen($this->get_buffer())); fclose($f); break; case 'S': //Return as a string - return $this->buffer; + return $this->get_buffer(); default: $this->Error('Incorrect output destination: '.$dest); } @@ -1111,7 +1111,7 @@ function get_xref_start_value() { /** * Read the offset of the xref table directly from file content * - * @note content has been previously been defined in $this->buffer + * @note content has been previously been defined in $this->get_buffer() * @param int $object_id an object id, a integer value starting from 1 * @return int the wished xrefstart offset value */ @@ -1138,7 +1138,7 @@ function get_offset_object_value($object_id) { static $positions=null; static $shifts=null; - if(is_null($offsets)) { //...variables content set once. This is the beauty of php :) + //if(is_null($offsets)) { //...variables content set once. This is the beauty of php :) //!NOTE: xref table is ordered by object id (position's object is not defined linearly in the pdf !) $positions=$this->_get_positions_ordered(); @@ -1146,7 +1146,7 @@ function get_offset_object_value($object_id) { $offsets=$this->_get_offsets_starting_from_zero(); //Shifts are already 0 indexed, don't change. $shifts=$this->shifts; - } + //} $p=$positions[$object_id]; $offset=$offsets[$p]; @@ -1158,13 +1158,13 @@ function get_offset_object_value($object_id) { /** * Reads the offset of the xref table directly from file content * - * @note content has been previously been defined in $this->buffer + * @note content has been previously been defined in $this->get_buffer() * @param int $object_id an object id, a integer value starting from 1 * @return int the wished offset */ function read_offset_object_value($object_id) { //------------------------------ - $buffer=$this->buffer; + $buffer=$this->get_buffer(); $previous_object_footer='';//'endobj' or comment; $object_header=$previous_object_footer.'\n'.$object_id.' 0 obj'; $chars = preg_split('/'.$object_header.'/', $buffer, -1, PREG_SPLIT_OFFSET_CAPTURE); @@ -1266,7 +1266,7 @@ function fix_xref_table() { $extract_offset_value_from_file=($this->safe_mode||$this->check_mode); //Get new file content (ie with values changed) - $this->buffer=$this->get_buffer(); + //$this->get_buffer()=$this->get_buffer(); for($i=0;$i<$xLen;$i++) { @@ -2112,5 +2112,4 @@ function Error($msg) { } -unset($__tmp); -?> \ No newline at end of file +unset($__tmp); \ No newline at end of file