From a75444f1c7616fb4d96902c822a7cbf6eac45922 Mon Sep 17 00:00:00 2001 From: Peter Steenbergen Date: Fri, 9 Aug 2019 14:30:28 +0200 Subject: [PATCH] Added PageExtractor Class --- CHANGELOG.md | 11 +++++++- README.md | 12 ++++++++- src/PageExtractor.php | 60 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/PageExtractor.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b0667be..6186fdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 \ No newline at end of file +- Get text from a PDF file diff --git a/README.md b/README.md index 8bfb4ab..88977a6 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. @@ -47,4 +57,4 @@ Extract Text from a PDF file. ```php $pdf = new ThreeWS\PdfTools\Text('location_to_pdf.pdf'); var_dump($pdf->convert()); -``` \ No newline at end of file +``` diff --git a/src/PageExtractor.php b/src/PageExtractor.php new file mode 100644 index 0000000..3295807 --- /dev/null +++ b/src/PageExtractor.php @@ -0,0 +1,60 @@ +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; + } +}