Skip to content

Commit dcf019a

Browse files
authored
Merge pull request #4 from enboig/fix_multiple_pages
When no page is set, full document is merged.
2 parents 593ca09 + 524fb4e commit dcf019a

File tree

6 files changed

+73
-13
lines changed

6 files changed

+73
-13
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ $ghostscript->setPageEnd(int $page);
170170
```
171171

172172

173+
## Clear specified page range
174+
175+
```php
176+
$ghostscript->clearPageRange();
177+
```
178+
179+
173180
## Subsample antialiasing
174181

175182
These options control the use of subsample antialiasing. Their use is highly recommended for producing high quality rasterizations of the input files.

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
"require": {
2828
"php": ">= 7.0.0",
2929
"mikehaertl/php-shellcommand": "^1.2"
30+
},
31+
"scripts": {
32+
"test": "vendor/bin/phpunit --verbose --configuration phpunit.xml"
3033
}
3134
}

src/PHPGhostscript/Ghostscript.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,17 @@ class Ghostscript
102102

103103
/**
104104
* Page start
105-
*
106-
* @var int
105+
*
106+
* @var ?int
107107
*/
108-
private $pageStart = 1;
108+
private $pageStart = null;
109109

110110
/**
111111
* Page end
112112
*
113-
* @var int
113+
* @var ?int
114114
*/
115-
private $pageEnd = 1;
115+
private $pageEnd = null;
116116

117117
/**
118118
* Box mode used for rendering
@@ -390,7 +390,7 @@ public function getOutputFile() : string
390390
*
391391
* @return self
392392
*/
393-
public function setPageStart(int $page) : Ghostscript
393+
public function setPageStart(?int $page): Ghostscript
394394
{
395395
$this->pageStart = $page;
396396
return $this;
@@ -402,7 +402,7 @@ public function setPageStart(int $page) : Ghostscript
402402
*
403403
* @return int $pageStart
404404
*/
405-
public function getPageStart() : int
405+
public function getPageStart(): ?int
406406
{
407407
return $this->pageStart;
408408
}
@@ -415,7 +415,7 @@ public function getPageStart() : int
415415
*
416416
* @return self
417417
*/
418-
public function setPageEnd(int $page) : Ghostscript
418+
public function setPageEnd(?int $page): Ghostscript
419419
{
420420
$this->pageEnd = $page;
421421
return $this;
@@ -427,7 +427,7 @@ public function setPageEnd(int $page) : Ghostscript
427427
*
428428
* @return int $pageEnd
429429
*/
430-
public function getPageEnd() : int
430+
public function getPageEnd(): ?int
431431
{
432432
return $this->pageEnd;
433433
}
@@ -448,6 +448,19 @@ public function setPages(int $startPage, int $endPage) : Ghostscript
448448
return $this;
449449
}
450450

451+
/**
452+
* Clears the page range
453+
*
454+
* @return self
455+
*/
456+
public function clearPageRange() : Ghostscript
457+
{
458+
$this->setPageStart(null);
459+
$this->setPageEnd(null);
460+
return $this;
461+
}
462+
463+
451464

452465
/**
453466
* Set the render box mode
@@ -515,8 +528,14 @@ public function render()
515528
$command->addArg('-dSAFER');
516529
$command->addArg('-dNOPLATFONTS');
517530
$command->addArg('-sOutputFile=' . $this->outputFile);
518-
$command->addArg('-dFirstPage=' . $this->pageStart);
519-
$command->addArg('-dLastPage=' . $this->pageEnd);
531+
532+
if ($this->pageStart !== null) {
533+
$command->addArg('-dFirstPage=' . $this->pageStart);
534+
}
535+
536+
if ($this->pageEnd !== null) {
537+
$command->addArg('-dLastPage=' . $this->pageEnd);
538+
}
520539

521540
if (!$this->isVectorDevice()) {
522541
$command->addArg('-dTextAlphaBits=' . $this->getTextAntiAliasing());

tests/PHPGhostscript/PHPGhostscriptTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ public function testSettingPages()
110110
{
111111
$ghostscript = new Ghostscript();
112112

113-
$this->assertEquals(1, $ghostscript->getPageStart());
113+
$this->assertEquals(null, $ghostscript->getPageStart());
114114
$ghostscript->setPageStart(20);
115115
$this->assertEquals(20, $ghostscript->getPageStart());
116116

117-
$this->assertEquals(1, $ghostscript->getPageEnd());
117+
$this->assertEquals(null, $ghostscript->getPageEnd());
118118
$ghostscript->setPageEnd(99);
119119
$this->assertEquals(99, $ghostscript->getPageEnd());
120120

@@ -171,6 +171,37 @@ public function testMerging()
171171
$this->assertTrue($result);
172172
}
173173

174+
public function testClearPageRange() {
175+
$ghostscript = new Ghostscript();
176+
$this->assertNull($ghostscript->getPageStart());
177+
$this->assertNull($ghostscript->getPageEnd());
178+
179+
$ghostscript->setPageStart(2);
180+
$ghostscript->setPageEnd(10);
181+
$this->assertEquals(2, $ghostscript->getPageStart());
182+
$this->assertEquals(10, $ghostscript->getPageEnd());
183+
184+
$ghostscript->clearPageRange();
185+
$this->assertNull($ghostscript->getPageStart());
186+
$this->assertNull($ghostscript->getPageEnd());
187+
}
188+
189+
public function testMerging2()
190+
{
191+
$outputFile = __DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'merge2.pdf';
192+
$ghostscript = new Ghostscript();
193+
$ghostscript
194+
->setBinaryPath($this->binaryPath)
195+
->setDevice(DeviceTypes::PDF)
196+
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'a2.pdf')
197+
->setInputFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'b1.pdf')
198+
->setOutputFile($outputFile);
199+
$result = $ghostscript->render();
200+
$this->assertTrue(file_exists($outputFile));
201+
$this->assertEquals("application/pdf", mime_content_type($outputFile));
202+
$this->assertTrue($result);
203+
}
204+
174205
public function testClearInputFiles()
175206
{
176207
$ghostscript = new Ghostscript();

tests/PHPGhostscript/assets/a2.pdf

7.63 KB
Binary file not shown.

tests/PHPGhostscript/assets/b1.pdf

6.87 KB
Binary file not shown.

0 commit comments

Comments
 (0)