Skip to content

Commit

Permalink
Merge pull request #3 from jameswilddev/master
Browse files Browse the repository at this point in the history
Fix runtime errors not being reported.
  • Loading branch information
jameswilddev authored Mar 27, 2023
2 parents 9c44c91 + b1b366d commit 76b5f06
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/PdfStitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function save(string $filePath): void
throw new InvalidArgumentException('Specified file\'s directory does not exist: '.$filePath);
}

shell_exec($this->getShellCommand($filePath));
$this->runShellCommand($this->getShellCommand($filePath));
}

/**
Expand Down Expand Up @@ -123,4 +123,36 @@ private function ghostscriptInstalled(): bool
{
return !empty(shell_exec('command -v gs'));
}

private function runShellCommand(string $command): void
{
$process = proc_open(
$command,
[
['pipe', 'r'],
['pipe', 'w'],
['pipe', 'w'],
],
$pipes
);

if ($process === false) {
throw new RuntimeException('Failed to open a process to stitch PDFs.');
}

// STDIN.
fclose($pipes[0]);

$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);

$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);

$exitCode = proc_close($process);

if ($exitCode !== 0 || $stderr !== '') {
throw new RuntimeException('Failed to run shell command "'.$command.'"; exit code '.$exitCode.', stdout "'.$stdout.'", stderr "'.$stderr.'".');
}
}
}

0 comments on commit 76b5f06

Please sign in to comment.