Skip to content

Commit

Permalink
Merge pull request #4 from jameswilddev/feature/page-selection
Browse files Browse the repository at this point in the history
Allow selection of individual page indices.
  • Loading branch information
jameswilddev authored Apr 11, 2023
2 parents 76b5f06 + e470657 commit 84010f9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,22 @@ A path to a Ghostscript executable can be passed into the `PdfStitcher` construc
```php
new PdfStitcher('a/path/to/a/gs/executable')
```

### Including only specific pages from a PDF

You may optionally give an array of page indices when adding a PDF:

```php
(new PdfStitcher)
->addPdf('firstDocument.pdf', [0, 2, 3, 5])
->save('destinationDocument.pdf');
```

Only the page numbers you list will be included.

The following will be rejected:

- Non-integer page numbers.
- Page numbers less than zero.
- Duplicate page numbers (e.g. 1, 3, 3, 5, 7).
- Misordered page numbers (e.g. 1, 5, 3, 7).
37 changes: 32 additions & 5 deletions src/PdfStitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
class PdfStitcher
{
/**
* Array of input PDF files to be stitched together.
* Array of arguments to be given to "gs".
*
* @var array
*/
private $inputFiles = [];
private $arguments = [];

/**
* A path to a Ghostscript executable to use instead of the default "gs".
Expand All @@ -39,9 +39,10 @@ public function __construct($overriddenGhostscriptExecutablePath = null)
* Add a PDF to the list of files to be stitched together.
*
* @param string $filePath
* @param ?array $pageIndices
* @return PdfStitcher
*/
public function addPdf(string $filePath): self
public function addPdf(string $filePath, ?array $pageIndices = null): self
{
if (!file_exists($filePath) || !is_readable($filePath)) {
throw new InvalidArgumentException('Specified file does not exist or can not be read: '.$filePath);
Expand All @@ -55,7 +56,33 @@ public function addPdf(string $filePath): self
throw new InvalidArgumentException('Specified file is not a PDF: '.$filePath);
}

$this->inputFiles[] = $filePath;
if ($pageIndices !== null) {
if (count($pageIndices) > 0) {
$previous = -1;

foreach ($pageIndices as $pageIndex) {
if (! ctype_digit(strval($pageIndex)) ) {
throw new InvalidArgumentException('Invalid page index "'.$pageIndex.'".');
}

if ($pageIndex === $previous) {
throw new InvalidArgumentException('Duplicate page index "'.$pageIndex.'".');
}

if ($pageIndex < $previous) {
throw new InvalidArgumentException('Misordered page index "'.$pageIndex.'".');
}

$previous = $pageIndex;
}

$this->arguments[] = '-sPageList='.implode(',', array_map(fn (int $pageIndex) => $pageIndex + 1, $pageIndices));
$this->arguments[] = Utils::quote($filePath);
}
} else {
$this->arguments[] = '-sPageList=1-';
$this->arguments[] = Utils::quote($filePath);
}

return $this;
}
Expand Down Expand Up @@ -109,7 +136,7 @@ private function getShellCommand($filePath): string
}

$command .= ' -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile='.Utils::quote($filePath).' ';
$command .= implode(' ', array_map([Utils::class, 'quote'], $this->inputFiles));
$command .= implode(' ', $this->arguments);

return $command;
}
Expand Down

0 comments on commit 84010f9

Please sign in to comment.