Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
* Re-arrange structure
* Compatibility with version 4.0 (mac OS)
* Readme update
rcknr authored May 27, 2019
1 parent beb9544 commit 5196aa0
Showing 14 changed files with 187 additions and 306 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.*
!.gitignore
vendor/*
composer.phar
composer.lock
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
- Adapted version for EV Conversion Worker

Simple PHP wrapper to the pdfinfo unix tool.

Inspired by http://stackoverflow.com/questions/14644353/get-the-number-of-pages-in-a-pdf-document/14644354
@@ -10,7 +8,7 @@ pdfinfo is an unix tool helping extract information from pdf files.

http://linuxcommand.org/man_pages/pdfinfo1.html

You can get page count, title, author..etc via the tool.
You can get metadata, page count and rotation for every page, etc.

# Installation

@@ -26,7 +24,7 @@ sudo apt-get install poppler-utils
## 2. Install the library
You can just download the file to your project, or install it via composer:
```
composer require "howtomakeaturn/pdfinfo:1.*"
composer require apilayer/pdfinfo
```

# Usage
@@ -45,7 +43,7 @@ This library throws 4 kind of exceptions to represent the official exit codes.
* PDFPermissionException
* OtherException

Check the [official documentation](http://linuxcommand.org/man_pages/pdfinfo1.html) for more information.
Check the [official documentation](https://www.xpdfreader.com/pdfinfo-man.html) for more information.



@@ -64,7 +62,14 @@ Currently this library supports the following metadata:
* pages
* encrypted
* pageSize
* pageSizes
* fileSize
* optimized
* PDFVersion
* pageRot
* pageRots

Environment variables configuration:

* `PDFINFO_BIN` - location of PDFInfo executable
* `PDFINFO_PAGE_LIMIT` - last page to process, defaults to 999
11 changes: 4 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
{
"name": "howtomakeaturn/pdfinfo",
"name": "apilayer/pdfinfo",
"type": "library",
"description": "simple php wrapper for pdfinfo",
"description": "pdfinfo PHP wrapper",
"keywords": ["pdf", "pdfinfo"],
"license": "MIT",
"require-dev": {
"filp/whoops": "1.1.4"
},
"autoload": {
"psr-0": {
"Howtomakeaturn\\PDFInfo": "src/"
"psr-4": {
"apilayer\\PDFInfo\\": "src/"
}
}
}
22 changes: 22 additions & 0 deletions examples/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
require_once('../vendor/autoload.php');

use apilayer\PDFInfo\PDFInfo;

$pdf = new PDFInfo('files/Sample.pdf');

echo 'Title: ', $pdf->title, '<hr />', PHP_EOL;
echo 'Author: ', $pdf->author, '<hr />', PHP_EOL;
echo 'Creator: ', $pdf->creator, '<hr />', PHP_EOL;
echo 'Producer: ', $pdf->producer, '<hr />', PHP_EOL;
echo 'Creation date: ', $pdf->creationDate, '<hr />', PHP_EOL;
echo 'Last modified date: ', $pdf->modDate, '<hr />', PHP_EOL;
echo 'Tagged: ', $pdf->tagged, '<hr />', PHP_EOL;
echo 'Form: ', $pdf->form, '<hr />', PHP_EOL;
echo 'Pages: ', $pdf->pages, '<hr />', PHP_EOL;
echo 'Encrypted: ', $pdf->encrypted, '<hr />', PHP_EOL;
echo 'Page size: ', $pdf->pageSize, '<hr />', PHP_EOL;
echo 'Page rotation: ', $pdf->pageRot, '<hr />', PHP_EOL;
echo 'File size: ', $pdf->fileSize, '<hr />', PHP_EOL;
echo 'Optimized: ', $pdf->optimized, '<hr />', PHP_EOL;
echo 'PDF Version: ', $pdf->PDFVersion, '<hr />', PHP_EOL;
39 changes: 0 additions & 39 deletions examples/example1.php

This file was deleted.

39 changes: 0 additions & 39 deletions examples/example2.php

This file was deleted.

39 changes: 0 additions & 39 deletions examples/example3.php

This file was deleted.

41 changes: 0 additions & 41 deletions examples/example4.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Howtomakeaturn\PDFInfo\Exceptions;
namespace apilayer\PDFInfo\Exceptions;

use \Exception;

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Howtomakeaturn\PDFInfo\Exceptions;
namespace apilayer\PDFInfo\Exceptions;

use \Exception;

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Howtomakeaturn\PDFInfo\Exceptions;
namespace apilayer\PDFInfo\Exceptions;

use \Exception;

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Howtomakeaturn\PDFInfo\Exceptions;
namespace apilayer\PDFInfo\Exceptions;

use \Exception;

132 changes: 0 additions & 132 deletions src/Howtomakeaturn/PDFInfo/PDFInfo.php

This file was deleted.

145 changes: 145 additions & 0 deletions src/PDFInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace apilayer\PDFInfo;

/*
* Inspired by http://stackoverflow.com/questions/14644353/get-the-number-of-pages-in-a-pdf-document/14644354
* @author howtomakeaturn
*/

use stdClass;

class PDFInfo
{
protected $file;
public $attributes, $output;
public static $bin;

/**
* PDFInfo constructor.
* @param $file
* @throws Exceptions\OpenOutputException
* @throws Exceptions\OpenPDFException
* @throws Exceptions\OtherException
* @throws Exceptions\PDFPermissionException
*/
public function __construct($file)
{
$this->attributes = new stdClass;
$this->file = $file;

$this->loadOutput();

$this->parseOutput();
}

public function __get($field)
{
return property_exists($this->attributes, $field) ?
$this->attributes->{$field} :
null;
}

public function getBinary()
{
if (empty(static::$bin)) {
static::$bin = trim(trim(getenv('PDFINFO_BIN'), '\\/" \'')) ?: 'pdfinfo';
}

return static::$bin;
}

/**
* @throws Exceptions\OpenOutputException
* @throws Exceptions\OpenPDFException
* @throws Exceptions\OtherException
* @throws Exceptions\PDFPermissionException
*/
private function loadOutput()
{
$cmd = escapeshellarg($this->getBinary()); // escapeshellarg to work with Windows paths with spaces.

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

$page_limit = intval(getenv('PDFINFO_PAGE_LIMIT')) ?: 999;

// Parse entire output
// Surround with double quotes if file name has spaces
exec("$cmd -l $page_limit $file", $output, $returnVar);

if ($returnVar === 1) {
throw new Exceptions\OpenPDFException();
} elseif ($returnVar === 2) {
throw new Exceptions\OpenOutputException();
} elseif ($returnVar === 3) {
throw new Exceptions\PDFPermissionException();
} elseif ($returnVar === 99) {
throw new Exceptions\OtherException();
}

$this->output = $output;
}

private function parseOutput()
{
foreach ($this->output as $output_line) {
list($key, $value) = explode(':', $output_line, 2);

if (preg_match('/\b(?<number>\d+)\b/', $key, $key_matches)) {
$key = str_replace($key_matches['number'], '', $key);
}

$key = $this->formatKey($key);
$value = $this->formatValue($value);

// Only set attributes once
if (!property_exists($this->attributes, $key)) {
$this->attributes->{$key} = $value;
}

// Attributes for multiple pages
if (isset($key_matches['number'])) {
if (!property_exists($this->attributes, "${key}s")) {
$this->attributes->{"${key}s"} = new stdClass;
}
$this->attributes->{"${key}s"}->{$key_matches['number']} = $value;
}
}

// Compatibility with version 4.0 which has page rotation data inside of page size
$rot_pattern = '/\(rot\w+\s(?<degrees>\d+)\s\w+\)$$/';
$rot_replace = '/\s+\([^\)]+\)$/';

if (is_null($this->pageRot) && $this->pageSize && preg_match($rot_pattern, $this->pageSize, $rot_matches)) {
$this->attributes->{'pageRot'} = $rot_matches['degrees'];
$this->attributes->{'pageSize'} = preg_replace($rot_replace, '', $this->pageSize);

// Also process attributes for all pages
if (property_exists($this->attributes, 'pageSizes')) {
foreach ($this->pageSizes as $page_number => $page_size) {
preg_match($rot_pattern, $page_size, $page_matches);
if (isset($page_matches['degrees'])) {
if (!property_exists($this->attributes, 'pageRots')) {
$this->attributes->{'pageRots'} = new stdClass;
}
$this->attributes->{'pageRots'}->{$page_number} = $page_matches['degrees'];
$this->attributes->{'pageSizes'}->{$page_number} = preg_replace($rot_replace, '',
$this->pageSizes->{$page_number});
}
}
}
}
}

private function formatKey($string)
{
return preg_replace_callback('/^([A-Z])(?![A-Z])/', function ($m) {
return strtolower($m[1]);
}, preg_replace('/\s+/', '', ucwords($string)));
}

private function formatValue($string)
{
return trim(preg_replace('/\s+/', ' ', $string));
}
}

0 comments on commit 5196aa0

Please sign in to comment.