Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ PHPGhostscript

A PHP wrapper for [Ghostscript](https://www.ghostscript.com) ; an interpreter for PostScript™ and Portable Document Format (PDF) files.

Use `PHPGhostscript` for rendering PDF / PostScript™ files to various image types (jpeg / png) or export to PDF with page range (single page / multipage).
Use `PHPGhostscript` for rendering PDF / PostScript™ files to various image types (jpeg / png) or export to PDF with page range (single page / multipage).

Setting multiple input files merge them.

Easy to use and OOP interfaced.


Expand Down
2 changes: 2 additions & 0 deletions examples/manipulatePDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'pdf_%2d.pdf')
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'MultiPageHorizontal.pdf')
// Multipe inputs -> merged output
// ->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'MultiPageVertical.pdf')

->setPageStart(2)
->setPageEnd(3)
Expand Down
39 changes: 25 additions & 14 deletions src/PHPGhostscript/Ghostscript.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php


/**
Expand All @@ -15,7 +15,7 @@
use mikehaertl\shellcommand\Command;
use daandesmedt\PHPGhostscript\Devices\DeviceInterface;
use daandesmedt\PHPGhostscript\Devices\DeviceTypes;

use daandesmedt\PHPGhostscript\Devices\JPEG;

class Ghostscript
{
Expand Down Expand Up @@ -87,11 +87,11 @@ class Ghostscript
];

/**
* Current file
* Current files
*
* @var string
* @var string[]
*/
private $file;
private $files = [];

/**
* Output file
Expand Down Expand Up @@ -141,7 +141,7 @@ public function __construct(string $binaryPath = null)
$this->setBinaryPath($binaryPath);
}

$this->setDevice(DeviceTypes::JPEG);
$this->setDevice(new JPEG);
}


Expand Down Expand Up @@ -332,21 +332,31 @@ public function setInputFile($file) : Ghostscript
}
}

$this->file = $file;
$this->files[] = $file;
return $this;
}


/**
* Get inputfile
* Get inputfiles
*
* @return string
* @return array
*/
public function getInputFile() : string
public function getInputFiles() : array
{
return $this->file;
return $this->files;
}

/**
* Clear inputfiles
*
* @return self
*/
public function clearInputFiles() : Ghostscript
{
$this->files = [];
return $this;
}

/**
* Set output file
Expand Down Expand Up @@ -527,13 +537,14 @@ public function render()
$command->addArg($arg);
}

$command->addArg(' ' . $this->file);
foreach ($this->files as $file) {
$command->addArg(' ' . $file);
}

if ($command->execute() && $command->getExitCode() == 0) {
return true;
} else {
throw new GhostscriptException('Ghostscript was unable to transcode');
}
}

}
}
62 changes: 44 additions & 18 deletions tests/PHPGhostscript/PHPGhostscriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,47 +93,48 @@ public function testInexistentInputFile()
public function testInputFile()
{
$ghostscript = new Ghostscript();
$ghostscript->setInputFile( __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
$this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps', $ghostscript->getInputFile());

$ghostscript->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
$this->assertEquals([__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps'], $ghostscript->getInputFiles());
$ghostscript->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
$this->assertEquals([__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps', __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps'], $ghostscript->getInputFiles());
}

public function testOutputFile()
{
{
$ghostscript = new Ghostscript();
$ghostscript->setOutputFile( __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
$ghostscript->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
$this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps', $ghostscript->getOutputFile());
}

public function testSettingPages()
{
$ghostscript = new Ghostscript();

$this->assertEquals(1,$ghostscript->getPageStart());
$this->assertEquals(1, $ghostscript->getPageStart());
$ghostscript->setPageStart(20);
$this->assertEquals(20,$ghostscript->getPageStart());
$this->assertEquals(20, $ghostscript->getPageStart());

$this->assertEquals(1,$ghostscript->getPageEnd());
$this->assertEquals(1, $ghostscript->getPageEnd());
$ghostscript->setPageEnd(99);
$this->assertEquals(99,$ghostscript->getPageEnd());
$this->assertEquals(99, $ghostscript->getPageEnd());

$ghostscript->setPages(44,88);
$this->assertEquals(44,$ghostscript->getPageStart());
$this->assertEquals(88,$ghostscript->getPageEnd());
$ghostscript->setPages(44, 88);
$this->assertEquals(44, $ghostscript->getPageStart());
$this->assertEquals(88, $ghostscript->getPageEnd());
}

public function testSetBox()
{
$ghostscript = new Ghostscript();
$this->assertNull($ghostscript->getBox());
$ghostscript->setBox(Ghostscript::BOX_CROP);
$this->assertEquals(Ghostscript::BOX_CROP,$ghostscript->getBox());
$this->assertEquals(Ghostscript::BOX_CROP, $ghostscript->getBox());
$ghostscript->setBox(Ghostscript::BOX_ART);
$this->assertEquals(Ghostscript::BOX_ART,$ghostscript->getBox());
$this->assertEquals(Ghostscript::BOX_ART, $ghostscript->getBox());
$ghostscript->setBox(Ghostscript::BOX_ART);
$this->assertEquals(Ghostscript::BOX_ART,$ghostscript->getBox());
$this->assertEquals(Ghostscript::BOX_ART, $ghostscript->getBox());
$ghostscript->setBox(Ghostscript::BOX_BLEED);
$this->assertEquals(Ghostscript::BOX_BLEED,$ghostscript->getBox());
$this->assertEquals(Ghostscript::BOX_BLEED, $ghostscript->getBox());
$ghostscript->setBox(Ghostscript::BOX_NONE);
$this->assertNull($ghostscript->getBox());
$ghostscript->setBox('BOX_NOT_HERE');
Expand All @@ -147,11 +148,36 @@ public function testRender()
$ghostscript
->setBinaryPath($this->binaryPath)
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'MultiPageHorizontal.pdf')
->setOutputFile($outputFile);
->setOutputFile($outputFile);
$result = $ghostscript->render();
$this->assertTrue(file_exists($outputFile));
$this->assertEquals("image/jpeg", mime_content_type($outputFile));
$this->assertTrue($result);
}

}
public function testMerging()
{
$outputFile = __DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'merge.pdf';
$ghostscript = new Ghostscript();
$ghostscript
->setBinaryPath($this->binaryPath)
->setDevice(DeviceTypes::PDF)
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'lorem.pdf')
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'ipsum.pdf')
->setOutputFile($outputFile);
$result = $ghostscript->render();
$this->assertTrue(file_exists($outputFile));
$this->assertEquals("application/pdf", mime_content_type($outputFile));
$this->assertTrue($result);
}

public function testClearInputFiles()
{
$ghostscript = new Ghostscript();
$ghostscript->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
$this->assertEquals([__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps'], $ghostscript->getInputFiles());
$ghostscript->clearInputFiles();
$this->assertEquals([], $ghostscript->getInputFiles());
}

}
Binary file added tests/PHPGhostscript/assets/ipsum.pdf
Binary file not shown.
Binary file added tests/PHPGhostscript/assets/lorem.pdf
Binary file not shown.