Created
July 27, 2023 03:40
-
-
Save ngekoding/8a0c24fc84b307633066b65235b8501f to your computer and use it in GitHub Desktop.
PDF version converter using Ghoshscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
use Symfony\Component\Process\Exception\ProcessFailedException; | |
use Symfony\Component\Process\Process; | |
class Pdf_version_converter | |
{ | |
private $gsPath; | |
private $baseCommand = '%s -sDEVICE=pdfwrite -dCompatibilityLevel=%s -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=%s %s'; | |
public function __construct() | |
{ | |
$this->gsPath = getenv('GHOSTSCRIPT_PATH') ?: 'gs'; | |
} | |
public function run($originalFile, $newFile, $newVersion) | |
{ | |
$command = sprintf($this->baseCommand, $this->gsPath, $newVersion, escapeshellarg($newFile), escapeshellarg($originalFile)); | |
$process = new Process($command); | |
$process->run(); | |
if (!$process->isSuccessful()) { | |
throw new ProcessFailedException($process); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment