Skip to content

Instantly share code, notes, and snippets.

@ngekoding
Created July 27, 2023 03:40
Show Gist options
  • Save ngekoding/8a0c24fc84b307633066b65235b8501f to your computer and use it in GitHub Desktop.
Save ngekoding/8a0c24fc84b307633066b65235b8501f to your computer and use it in GitHub Desktop.
PDF version converter using Ghoshscript
<?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