From 8b72a84501c7ea753fc1cef2e44fb057e5813f55 Mon Sep 17 00:00:00 2001 From: Dominic Date: Mon, 7 Nov 2016 15:02:30 +0000 Subject: [PATCH 1/2] Update composer.json --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index db978d5..4d24ac5 100644 --- a/composer.json +++ b/composer.json @@ -20,9 +20,9 @@ } }, "require": { - "php": ">=5.3.0", - "symfony/filesystem": "~2.3", - "symfony/process": "~2.3" + "php": ">=5.6.4", + "symfony/filesystem": "~3.1", + "symfony/process": "~3.1" }, "require-dev": { "phpunit/phpunit": "~4.5" From c1118686760b06d9eadbd46d6fa64377bde4d194 Mon Sep 17 00:00:00 2001 From: Dominic Jones Date: Tue, 15 Nov 2016 20:24:01 +0000 Subject: [PATCH 2/2] set GhostScript executable manually or determine based on OS/arch --- src/Converter/GhostscriptConverterCommand.php | 61 ++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/Converter/GhostscriptConverterCommand.php b/src/Converter/GhostscriptConverterCommand.php index 600981e..803b72e 100644 --- a/src/Converter/GhostscriptConverterCommand.php +++ b/src/Converter/GhostscriptConverterCommand.php @@ -22,15 +22,72 @@ class GhostscriptConverterCommand /** * @var Filesystem */ - protected $baseCommand = 'gs -sDEVICE=pdfwrite -dCompatibilityLevel=%s -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=%s %s'; + protected $baseCommand = '%s -sDEVICE=pdfwrite -dCompatibilityLevel=%s -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=%s %s'; + + /** + * @var string + */ + protected $executable; public function __construct() { } + public function setExecutable($executable) + { + $this->executable = $executable; + } + + private function getExecutable() + { + return $this->executable ?: $this->getExecutableByOS(); + } + + private function getExecutableByOS() + { + $os = PHP_OS; + + $executablesByOS = [ + 'WINNT' => [ + '32' => 'gswin32c', + '64' => 'gswin64c' + ], + 'LINUX' => [ + 'i686' => 'gs', + 'x86_64' => 'gs' + ], + ]; + + if ($os === 'WINNT') { + $out = []; + exec("wmic cpu get DataWidth", $out); + $bits = strstr(implode("", $out), "64") ? 64 : 32; + $architecture = $bits; + } else { + $architecture = shell_exec('arch'); + } + + $executable = $executablesByOS[$os][$architecture]; + + if (!$this->checkExecutableExists($executable)) { + throw new Exception("Cannot determine GhostScript exe"); + } + + return $executable; + } + + private function checkExecutableExists($executable) + { + if (!shell_exec('which ' . $executable)) { + return false; + } + + return true; + } + public function run($originalFile, $newFile, $newVersion) { - $command = sprintf($this->baseCommand, $newVersion, $newFile, $originalFile); + $command = sprintf($this->baseCommand, $this->getExecutable(), $newVersion, $newFile, $originalFile); $process = new Process($command); $process->run();