Skip to content

Commit 593ca09

Browse files
authored
Merge pull request #2 from enboig/master
Added support for merging PDFs.
2 parents 4db7a91 + 28ff2cf commit 593ca09

File tree

6 files changed

+75
-33
lines changed

6 files changed

+75
-33
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ PHPGhostscript
33

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

6-
Use `PHPGhostscript` for rendering PDF / PostScript™ files to various image types (jpeg / png) or export to PDF with page range (single page / multipage).
6+
Use `PHPGhostscript` for rendering PDF / PostScript™ files to various image types (jpeg / png) or export to PDF with page range (single page / multipage).
7+
8+
Setting multiple input files merge them.
9+
710
Easy to use and OOP interfaced.
811

912

examples/manipulatePDF.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'pdf_%2d.pdf')
1717
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'MultiPageHorizontal.pdf')
18+
// Multipe inputs -> merged output
19+
// ->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'MultiPageVertical.pdf')
1820

1921
->setPageStart(2)
2022
->setPageEnd(3)

src/PHPGhostscript/Ghostscript.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33

44
/**
@@ -15,7 +15,7 @@
1515
use mikehaertl\shellcommand\Command;
1616
use daandesmedt\PHPGhostscript\Devices\DeviceInterface;
1717
use daandesmedt\PHPGhostscript\Devices\DeviceTypes;
18-
18+
use daandesmedt\PHPGhostscript\Devices\JPEG;
1919

2020
class Ghostscript
2121
{
@@ -87,11 +87,11 @@ class Ghostscript
8787
];
8888

8989
/**
90-
* Current file
90+
* Current files
9191
*
92-
* @var string
92+
* @var string[]
9393
*/
94-
private $file;
94+
private $files = [];
9595

9696
/**
9797
* Output file
@@ -141,7 +141,7 @@ public function __construct(string $binaryPath = null)
141141
$this->setBinaryPath($binaryPath);
142142
}
143143

144-
$this->setDevice(DeviceTypes::JPEG);
144+
$this->setDevice(new JPEG);
145145
}
146146

147147

@@ -332,21 +332,31 @@ public function setInputFile($file) : Ghostscript
332332
}
333333
}
334334

335-
$this->file = $file;
335+
$this->files[] = $file;
336336
return $this;
337337
}
338338

339339

340340
/**
341-
* Get inputfile
341+
* Get inputfiles
342342
*
343-
* @return string
343+
* @return array
344344
*/
345-
public function getInputFile() : string
345+
public function getInputFiles() : array
346346
{
347-
return $this->file;
347+
return $this->files;
348348
}
349349

350+
/**
351+
* Clear inputfiles
352+
*
353+
* @return self
354+
*/
355+
public function clearInputFiles() : Ghostscript
356+
{
357+
$this->files = [];
358+
return $this;
359+
}
350360

351361
/**
352362
* Set output file
@@ -527,13 +537,14 @@ public function render()
527537
$command->addArg($arg);
528538
}
529539

530-
$command->addArg(' ' . $this->file);
540+
foreach ($this->files as $file) {
541+
$command->addArg(' ' . $file);
542+
}
531543

532544
if ($command->execute() && $command->getExitCode() == 0) {
533545
return true;
534546
} else {
535547
throw new GhostscriptException('Ghostscript was unable to transcode');
536548
}
537549
}
538-
539-
}
550+
}

tests/PHPGhostscript/PHPGhostscriptTest.php

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,47 +93,48 @@ public function testInexistentInputFile()
9393
public function testInputFile()
9494
{
9595
$ghostscript = new Ghostscript();
96-
$ghostscript->setInputFile( __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
97-
$this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps', $ghostscript->getInputFile());
98-
96+
$ghostscript->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
97+
$this->assertEquals([__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps'], $ghostscript->getInputFiles());
98+
$ghostscript->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
99+
$this->assertEquals([__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps', __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps'], $ghostscript->getInputFiles());
99100
}
100101

101102
public function testOutputFile()
102-
{
103+
{
103104
$ghostscript = new Ghostscript();
104-
$ghostscript->setOutputFile( __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
105+
$ghostscript->setOutputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
105106
$this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps', $ghostscript->getOutputFile());
106107
}
107108

108109
public function testSettingPages()
109110
{
110111
$ghostscript = new Ghostscript();
111112

112-
$this->assertEquals(1,$ghostscript->getPageStart());
113+
$this->assertEquals(1, $ghostscript->getPageStart());
113114
$ghostscript->setPageStart(20);
114-
$this->assertEquals(20,$ghostscript->getPageStart());
115+
$this->assertEquals(20, $ghostscript->getPageStart());
115116

116-
$this->assertEquals(1,$ghostscript->getPageEnd());
117+
$this->assertEquals(1, $ghostscript->getPageEnd());
117118
$ghostscript->setPageEnd(99);
118-
$this->assertEquals(99,$ghostscript->getPageEnd());
119+
$this->assertEquals(99, $ghostscript->getPageEnd());
119120

120-
$ghostscript->setPages(44,88);
121-
$this->assertEquals(44,$ghostscript->getPageStart());
122-
$this->assertEquals(88,$ghostscript->getPageEnd());
121+
$ghostscript->setPages(44, 88);
122+
$this->assertEquals(44, $ghostscript->getPageStart());
123+
$this->assertEquals(88, $ghostscript->getPageEnd());
123124
}
124125

125126
public function testSetBox()
126127
{
127128
$ghostscript = new Ghostscript();
128129
$this->assertNull($ghostscript->getBox());
129130
$ghostscript->setBox(Ghostscript::BOX_CROP);
130-
$this->assertEquals(Ghostscript::BOX_CROP,$ghostscript->getBox());
131+
$this->assertEquals(Ghostscript::BOX_CROP, $ghostscript->getBox());
131132
$ghostscript->setBox(Ghostscript::BOX_ART);
132-
$this->assertEquals(Ghostscript::BOX_ART,$ghostscript->getBox());
133+
$this->assertEquals(Ghostscript::BOX_ART, $ghostscript->getBox());
133134
$ghostscript->setBox(Ghostscript::BOX_ART);
134-
$this->assertEquals(Ghostscript::BOX_ART,$ghostscript->getBox());
135+
$this->assertEquals(Ghostscript::BOX_ART, $ghostscript->getBox());
135136
$ghostscript->setBox(Ghostscript::BOX_BLEED);
136-
$this->assertEquals(Ghostscript::BOX_BLEED,$ghostscript->getBox());
137+
$this->assertEquals(Ghostscript::BOX_BLEED, $ghostscript->getBox());
137138
$ghostscript->setBox(Ghostscript::BOX_NONE);
138139
$this->assertNull($ghostscript->getBox());
139140
$ghostscript->setBox('BOX_NOT_HERE');
@@ -147,11 +148,36 @@ public function testRender()
147148
$ghostscript
148149
->setBinaryPath($this->binaryPath)
149150
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'MultiPageHorizontal.pdf')
150-
->setOutputFile($outputFile);
151+
->setOutputFile($outputFile);
151152
$result = $ghostscript->render();
152153
$this->assertTrue(file_exists($outputFile));
153154
$this->assertEquals("image/jpeg", mime_content_type($outputFile));
154155
$this->assertTrue($result);
155156
}
156157

157-
}
158+
public function testMerging()
159+
{
160+
$outputFile = __DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'merge.pdf';
161+
$ghostscript = new Ghostscript();
162+
$ghostscript
163+
->setBinaryPath($this->binaryPath)
164+
->setDevice(DeviceTypes::PDF)
165+
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'lorem.pdf')
166+
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'ipsum.pdf')
167+
->setOutputFile($outputFile);
168+
$result = $ghostscript->render();
169+
$this->assertTrue(file_exists($outputFile));
170+
$this->assertEquals("application/pdf", mime_content_type($outputFile));
171+
$this->assertTrue($result);
172+
}
173+
174+
public function testClearInputFiles()
175+
{
176+
$ghostscript = new Ghostscript();
177+
$ghostscript->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps');
178+
$this->assertEquals([__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'Helicopter.ps'], $ghostscript->getInputFiles());
179+
$ghostscript->clearInputFiles();
180+
$this->assertEquals([], $ghostscript->getInputFiles());
181+
}
182+
183+
}

tests/PHPGhostscript/assets/ipsum.pdf

7 KB
Binary file not shown.

tests/PHPGhostscript/assets/lorem.pdf

7.05 KB
Binary file not shown.

0 commit comments

Comments
 (0)