Commit 28fd13fe authored by Cyril Vazquez's avatar Cyril Vazquez
Browse files

Merge branch 'fix/15068_PDF_output_memory_usage' into 'release/2.6.8'

Replace string buffer with stream buffer

See merge request !587
parents 85b6cc35 b6567360
Pipeline #9713 failed with stages
in 48 seconds
......@@ -15,6 +15,7 @@ protected $page; // current page number
protected $n; // current object number
protected $offsets; // array of object offsets
protected $buffer; // buffer holding in-memory PDF
protected $fp; // buffer holding in-memory PDF
protected $pages; // array containing pages
protected $state; // current document state
protected $compress; // compression flag
......@@ -79,7 +80,8 @@ function __construct($orientation='P', $unit='mm', $size='A4')
$this->state = 0;
$this->page = 0;
$this->n = 2;
$this->buffer = '';
//$this->buffer = '';
$this->buffer = fopen('php://temp', 'w+');
$this->pages = array();
$this->PageInfo = array();
$this->fonts = array();
......@@ -999,6 +1001,7 @@ function Output($dest='', $name='', $isUTF8=false)
$dest = 'I';
if($name=='')
$name = 'doc.pdf';
rewind($this->buffer);
switch(strtoupper($dest))
{
case 'I':
......@@ -1012,7 +1015,8 @@ function Output($dest='', $name='', $isUTF8=false)
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
}
echo $this->buffer;
//echo $this->buffer;
echo stream_get_contents($this->buffer);
break;
case 'D':
// Download file
......@@ -1021,16 +1025,23 @@ function Output($dest='', $name='', $isUTF8=false)
header('Content-Disposition: attachment; '.$this->_httpencode('filename',$name,$isUTF8));
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
echo $this->buffer;
//echo $this->buffer;
echo stream_get_contents($this->buffer);
break;
case 'F':
// Save to local file
if(!file_put_contents($name,$this->buffer))
//if(!file_put_contents($name, $this->buffer))
// $this->Error('Unable to create output file: '.$name);
$fp = fopen($name, 'w');
if(!stream_copy_to_stream($this->buffer, $fp))
$this->Error('Unable to create output file: '.$name);
fclose($fp);
break;
case 'S':
// Return as a string
return $this->buffer;
//return $this->buffer;
return stream_get_contents($this->buffer);
default:
$this->Error('Incorrect output destination: '.$dest);
}
......@@ -1461,12 +1472,14 @@ public function _out($s)
protected function _put($s)
{
$this->buffer .= $s."\n";
//$this->buffer .= $s."\n";
fwrite($this->buffer, $s."\n");
}
protected function _getoffset()
{
return strlen($this->buffer);
//return strlen($this->buffer);
return ftell($this->buffer);
}
protected function _newobj($n=null)
......
......@@ -139,9 +139,11 @@ class Fpdi extends FpdfTpl
protected function _put($s, $newLine = true)
{
if ($newLine) {
$this->buffer .= $s . "\n";
//$this->buffer .= $s . "\n";
fwrite($this->buffer, $s . "\n");
} else {
$this->buffer .= $s;
//$this->buffer .= $s;
fwrite($this->buffer, $s);
}
}
}
......@@ -163,11 +163,21 @@ class StreamReader
*/
public function getBuffer($atOffset = true)
{
$end = ftell($this->buffer);
if (false === $atOffset) {
return $this->buffer;
//return $this->buffer;
rewind($this->buffer);
$string = stream_get_contents($this->buffer);
fseek($this->buffer, $end);
return (string) $string;
}
$string = substr($this->buffer, $this->offset);
//$string = substr($this->buffer, $this->offset);
fseek($this->buffer, $this->offset);
$string = stream_get_contents($this->buffer);
fseek($this->buffer, $end);
return (string) $string;
}
......@@ -191,7 +201,13 @@ class StreamReader
return false;
}
return $this->buffer[$position];
//return $this->buffer[$position];
$end = ftell($this->buffer);
fseek($this->buffer, $position);
$byte = fread($this->buffer, 1);
fseek($this->buffer, $end);
return $byte;
}
/**
......@@ -226,7 +242,13 @@ class StreamReader
}
$this->offset = $offset + 1;
return $this->buffer[$offset];
//return $byte;
$end = ftell($this->buffer);
fseek($this->buffer, $offset);
$byte = fread($this->buffer, 1);
fseek($this->buffer, $end);
return $byte;
}
/**
......@@ -261,7 +283,12 @@ class StreamReader
return false;
}
$bytes = substr($this->buffer, $offset, $length);
//$bytes = substr($this->buffer, $offset, $length);
$end = ftell($this->buffer);
fseek($this->buffer, $offset);
$bytes = fread($this->buffer, $length);
fseek($this->buffer, $end);
$this->offset = $offset + $length;
return $bytes;
......@@ -403,8 +430,14 @@ class StreamReader
fseek($this->stream, $pos);
$this->position = $pos;
$this->buffer = $length > 0 ? fread($this->stream, $length) : '';
$this->bufferLength = strlen($this->buffer);
//$this->buffer = $length > 0 ? fread($this->stream, $length) : '';
//$this->bufferLength = strlen($this->buffer);
$this->buffer = fopen('php://temp', 'w+');
if ($length > 0) {
$string = fread($this->stream, $length);
$this->bufferLength = fwrite($this->buffer, $string);
}
$this->offset = 0;
// If a stream wrapper is in use it is possible that
......@@ -412,8 +445,12 @@ class StreamReader
// increaseLength()-method to correct that behavior
if ($this->bufferLength < $length && $this->increaseLength($length - $this->bufferLength)) {
// increaseLength parameter is $minLength, so cut to have only the required bytes in the buffer
$this->buffer = substr($this->buffer, 0, $length);
$this->bufferLength = strlen($this->buffer);
//$this->buffer = substr($this->buffer, 0, $length);
//$this->bufferLength = strlen($this->buffer);
$newBuffer = fopen('php://temp', 'w+');
stream_copy_to_stream($this->buffer, $newBuffer, $length);
$this->buffer = $newBuffer;
$this->bufferLength = $length;
}
}
......@@ -452,8 +489,10 @@ class StreamReader
$newLength = $this->bufferLength + $length;
do {
$this->buffer .= fread($this->stream, $newLength - $this->bufferLength);
$this->bufferLength = strlen($this->buffer);
//$this->buffer .= fread($this->stream, $newLength - $this->bufferLength);
$addedLength = fwrite($this->buffer, fread($this->stream, $newLength - $this->bufferLength));
//$this->bufferLength = strlen($this->buffer);
$this->bufferLength += $addedLength;
} while (($this->bufferLength !== $newLength) && !feof($this->stream));
return true;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment