PHP code example of daandesmedt / phpghostscript

1. Go to this page and download the library: Download daandesmedt/phpghostscript library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

daandesmedt / phpghostscript example snippets


use daandesmedt\PHPGhostscript\Devices\DeviceTypes;
use daandesmedt\PHPGhostscript\Devices\JPEG;


$ghostscript = new Ghostscript();
// JPEG
$ghostscript->setDevice(DeviceTypes::JPEG);
$ghostscript->setDevice(DeviceTypes::JPEG_GREY);
// PNG
$ghostscript->setDevice(DeviceTypes::PNG4);
$ghostscript->setDevice(DeviceTypes::PNG_256);
$ghostscript->setDevice(DeviceTypes::PNG_16M);
$ghostscript->setDevice(DeviceTypes::PNG_ALPHA);
$ghostscript->setDevice(DeviceTypes::PNG_GREY);
$ghostscript->setDevice(DeviceTypes::PNG_MONO);
$ghostscript->setDevice(DeviceTypes::PNG_MONO_D);
// PDF
$ghostscript->setDevice(DeviceTypes::PDF);

// or create device    
$device  = new JPEG();

// set device
$ghostscript->setDevice($device);

// Create JPEG device    
$device  = new JPEG();
// set JPEG device specific quality
$device->setQuality(100);



use daandesmedt\PHPGhostscript\Ghostscript;
use daandesmedt\PHPGhostscript\Devices\JPEG;
use daandesmedt\PHPGhostscript\Devices\DeviceTypes;
use daandesmedt\PHPGhostscript\Devices\JPEGGrey;
use daandesmedt\PHPGhostscript\Devices\PNG;

$ghostscript = new Ghostscript();
$ghostscript
    // set Ghostscript binary path
    ->setBinaryPath('C:\Program Files\gs\gs9.27\bin\gswin64.exe')
    
    // set output device 
    ->setDevice(DeviceTypes::JPEG)
    
    // set input & output file
    ->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'SinglePageHorizontal.pdf')
    ->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'SinglePageHorizontal.jpg');

// render
if (true === $ghostscript->render()) {
    echo 'success';
} else {
    echo 'error';
}

// file produce output files as : 'export-1.jpg', ... 'export-10.jpg', ... 
$ghostscript->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'export-%01d.jpg');

// file produce output files as : 'export-001.jpg', ... 'export-010.jpg', ... 
$ghostscript->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'export-%03d.jpg');

$ghostscript->setPages(int $startPage, int $endPage);

$ghostscript->setPageStart(int $page);
$ghostscript->setPageEnd(int $page);

$ghostscript->clearPageRange();

$ghostscript->setAntiAliasing(
    Ghostscript::ANTIALIASING_HIGH || 
    Ghostscript::ANTIALIASING_LOW ||
    Ghostscript::ANTIALIASING_NONE
);

$ghostscript->setTextAntiAliasing(
    Ghostscript::ANTIALIASING_HIGH ||
    Ghostscript::ANTIALIASING_LOW  ||
    Ghostscript::ANTIALIASING_NONE
);

$ghostscript->setGraphicsAntiAliasing(
    Ghostscript::ANTIALIASING_HIGH || 
    Ghostscript::ANTIALIASING_LOW  ||
    Ghostscript::ANTIALIASING_NONE
);

$ghostscript->setResolution(int $hdpi, int $vdpi = null);

$ghostscript->setUseCie(bool $useCie);

$ghostscript->setBox(
    Ghostscript::BOX_BLEED,
    Ghostscript::BOX_TRIM,
    Ghostscript::BOX_ART,
    Ghostscript::BOX_CROP,
    Ghostscript::BOX_NONE
);

$ghostscript = new Ghostscript();
try {
    $ghostscript
        ->setBinaryPath('C:\Program Files\gs\gs9.27\bin\gswin64.exe')
        ->setDevice(DeviceTypes::JPEG)
        // Force excetion - invalid file ; supports only for PDF & PS
        ->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'invalidfile.docx')
        ->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'export.jpg');
    if (true === $ghostscript->render()) {
        echo 'success';
    } else {
        echo 'error';
    }
} catch (InvalidArgumentException $e) {
    var_dump($e->getMessage());
} catch (GhostscriptException $e) {
    var_dump($e->getMessage());
}