Skip to content

Commit 04db3fe

Browse files
authored
Merge pull request #4 from SynergiTech/ditchstdin
avoid using stdin
2 parents 571cb7f + 79fba5b commit 04db3fe

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

src/PDF.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class PDF
2424
'printBackground' => true, //override binary default
2525
);
2626

27+
/**
28+
* @var array list of filehandles created
29+
*/
30+
private $filehandles = array();
31+
2732
/**
2833
* @var array list of possible options from binary
2934
*/
@@ -317,8 +322,6 @@ public function generateFromHtml(string $html, string $output, array $options =
317322

318323
/**
319324
* Execute the result of the compiled options.
320-
* - if the content is pre-rendered HTML, it has to be passed
321-
* in through STDIN to avoid command length issues
322325
*
323326
* @param array $options the compiled collection of options
324327
*
@@ -328,12 +331,6 @@ private function make(array $options) : string
328331
{
329332
$command = array($this->binary, $this->type);
330333

331-
$content = null;
332-
if (array_key_exists('content', $options)) {
333-
$content = $options['content'];
334-
unset($options['content']);
335-
}
336-
337334
foreach ($options as $option => $value) {
338335
if ($option == 'sandbox') {
339336
// only mess with sandbox if it is explicitly disabled
@@ -345,18 +342,18 @@ private function make(array $options) : string
345342
continue;
346343
}
347344

348-
if ($option == 'headerContent' || $option == 'footerContent') {
349-
$tmpfile = tmpfile();
345+
if ($option == 'headerContent' || $option == 'content' || $option == 'footerContent') {
346+
$tmpfile = tempnam(sys_get_temp_dir(), 'chromepdf-' . $option);
347+
rename($tmpfile, $tmpfile .= '.html'); // the temporary file needs to have extension html
350348

351-
//Create a long term reference to file handle to avoid garbage collection
352-
$this->{$option} = $tmpfile;
349+
$this->filehandles[] = $tmpfile;
353350

354-
fwrite($tmpfile, $value);
351+
file_put_contents($tmpfile, $value);
355352

356-
$option = str_replace('Content', 'Template', $option);
353+
$option = ($option == 'content') ? 'file' : str_replace('Content', 'Template', $option);
357354

358355
$command[] = '--' . $option;
359-
$command[] = stream_get_meta_data($tmpfile)['uri'];
356+
$command[] = $tmpfile;
360357
continue;
361358
}
362359

@@ -377,16 +374,17 @@ private function make(array $options) : string
377374

378375
$process = new Process($command);
379376

380-
if ($content !== null) {
381-
$process->setInput((string) $content);
382-
}
383-
384377
$process->run();
385378

386379
if (! $process->isSuccessful()) {
387380
throw new ProcessFailedException($process);
388381
}
389382

383+
if (count($this->filehandles) > 0) {
384+
array_map('unlink', $this->filehandles);
385+
$this->filehandles = array();
386+
}
387+
390388
return $process->getOutput();
391389
}
392390
}

0 commit comments

Comments
 (0)