Skip to content

Commit

Permalink
Added PageExtractor Class
Browse files Browse the repository at this point in the history
  • Loading branch information
petericebear committed Aug 9, 2019
1 parent 940d0c0 commit a75444f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

All notable changes to `pdf-tools` will be documented in this file.

### 0.3 - 2019-08-09
Note when you want to use the PageExtractor you would have ghostscript installed on your OS.

- Added PageExtractor which can make smaller single-page PDF file

### 0.2 - 2019-07-21

- Added JPG Exporter

### 0.1 - 2019-07-14

- Initial release
- Get information from a PDF file
- Separate a single PDF with multiple pages to multiple single pages
- Get text from a PDF file
- Get text from a PDF file
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This library depends on poppler utils which can be installed by the following.
Debian / Ubuntu
```bash
apt install poppler-utils
apt install ghostscript
```

OSX
Expand All @@ -32,6 +33,15 @@ $pdf = new ThreeWS\PdfTools\Info('location_to_pdf.pdf');
var_dump($pdf->toArray());
```

### Jpg

Convert a PDF page to a JPG file.

```php
$pdf = new ThreeWS\PdfTools\Jpg('location_to_pdf.pdf', 60, 'y');
var_dump($pdf->convert());
```

### Separate

Convert a PDF file with multiple pages into multiple single page PDF's using a pattern with replacement of %d.
Expand All @@ -47,4 +57,4 @@ Extract Text from a PDF file.
```php
$pdf = new ThreeWS\PdfTools\Text('location_to_pdf.pdf');
var_dump($pdf->convert());
```
```
60 changes: 60 additions & 0 deletions src/PageExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace ThreeWS\PdfTools;

use ThreeWS\PdfTools\Exceptions\OpenOutputException;
use ThreeWS\PdfTools\Exceptions\OpenPDFException;
use ThreeWS\PdfTools\Exceptions\OtherException;
use ThreeWS\PdfTools\Exceptions\PDFPermissionException;

class PageExtractor
{
protected $file;
protected $outputfile;
protected $page;

public function __construct($file, $page, $outputfile = null)
{
$this->file = $file;
$this->outputfile = $outputfile;
$this->page = $page;
}

public function convert()
{
if (is_null($this->outputfile)) {
if (strtolower(substr($this->file, -4)) == '.pdf') {
$parts = explode('.pdf', $this->file);
$outputfile = $parts[0].'-'.escapeshellarg($this->page).'.pdf';
} else {
$outputfile = escapeshellarg($this->file).'-'.escapeshellarg($this->page);
}
} else {
$outputfile = escapeshellarg($this->outputfile);
}

$file = escapeshellarg($this->file);
$page = escapeshellarg($this->page);

exec("ps2pdf -dFirstPage=$page -dLastPage=$page -dPDFSETTINGS=/ebook -dUseFlateCompression=true -dOptimize=true $file $outputfile", $output, $returnVar);

switch ($returnVar) {
case 1:
throw new OpenPDFException("Error opening PDF file: {$this->file}.");
break;
case 2:
throw new OpenOutputException();
break;
case 3:
throw new PDFPermissionException();
break;
case 99:
throw new OtherException();
break;
default:
break;
}

return $returnVar;
}
}

0 comments on commit a75444f

Please sign in to comment.