From 79fba5b1e9cb26f90c76342aae32ae5df5d0d055 Mon Sep 17 00:00:00 2001 From: William Hall Date: Tue, 30 Jul 2019 10:47:01 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20avoid=20using=20stdin=20due=20to?= =?UTF-8?q?=20problems=20in=20process=20library?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/PDF.php | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/PDF.php b/src/PDF.php index 0828a60..556e0b4 100644 --- a/src/PDF.php +++ b/src/PDF.php @@ -24,6 +24,11 @@ class PDF 'printBackground' => true, //override binary default ); + /** + * @var array list of filehandles created + */ + private $filehandles = array(); + /** * @var array list of possible options from binary */ @@ -317,8 +322,6 @@ public function generateFromHtml(string $html, string $output, array $options = /** * Execute the result of the compiled options. - * - if the content is pre-rendered HTML, it has to be passed - * in through STDIN to avoid command length issues * * @param array $options the compiled collection of options * @@ -328,12 +331,6 @@ private function make(array $options) : string { $command = array($this->binary, $this->type); - $content = null; - if (array_key_exists('content', $options)) { - $content = $options['content']; - unset($options['content']); - } - foreach ($options as $option => $value) { if ($option == 'sandbox') { // only mess with sandbox if it is explicitly disabled @@ -345,18 +342,18 @@ private function make(array $options) : string continue; } - if ($option == 'headerContent' || $option == 'footerContent') { - $tmpfile = tmpfile(); + if ($option == 'headerContent' || $option == 'content' || $option == 'footerContent') { + $tmpfile = tempnam(sys_get_temp_dir(), 'chromepdf-' . $option); + rename($tmpfile, $tmpfile .= '.html'); // the temporary file needs to have extension html - //Create a long term reference to file handle to avoid garbage collection - $this->{$option} = $tmpfile; + $this->filehandles[] = $tmpfile; - fwrite($tmpfile, $value); + file_put_contents($tmpfile, $value); - $option = str_replace('Content', 'Template', $option); + $option = ($option == 'content') ? 'file' : str_replace('Content', 'Template', $option); $command[] = '--' . $option; - $command[] = stream_get_meta_data($tmpfile)['uri']; + $command[] = $tmpfile; continue; } @@ -377,16 +374,17 @@ private function make(array $options) : string $process = new Process($command); - if ($content !== null) { - $process->setInput((string) $content); - } - $process->run(); if (! $process->isSuccessful()) { throw new ProcessFailedException($process); } + if (count($this->filehandles) > 0) { + array_map('unlink', $this->filehandles); + $this->filehandles = array(); + } + return $process->getOutput(); } }