-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Edd Turtle
committed
Nov 24, 2020
1 parent
616ef83
commit cd01b8c
Showing
8 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.idea | ||
composer.lock | ||
vendor | ||
!.gitkeep | ||
tests/output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# qpdf-php | ||
PHP wrapper for the qpdf library | ||
# Qpdf PHP | ||
|
||
PHP wrapper for the qpdf library. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "eddturtle/qpdf-php", | ||
"description": "PHP wrapper for the qpdf library", | ||
"type": "composer-plugin", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Edd Turtle", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php" : ">=7.2", | ||
"mikehaertl/php-shellcommand": "^1.5.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~8.0||~9.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"EddTurtle\\Qpdf\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"EddTurtle\\Qpdf\\Tests\\": "tests/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
cacheResult="false" | ||
colors="true"> | ||
<testsuites> | ||
<testsuite name="DirectUpload Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace EddTurtle\Qpdf; | ||
|
||
use mikehaertl\shellcommand\Command; | ||
|
||
class Pdf | ||
{ | ||
|
||
/** | ||
* @var Command the command instance that executes pdftk | ||
*/ | ||
protected $command; | ||
|
||
protected $error; | ||
protected $output; | ||
|
||
protected $options = [ | ||
'command' => 'qpdf', | ||
]; | ||
|
||
protected $pages = []; | ||
|
||
public function __construct($file = null, $options = []) | ||
{ | ||
$this->options += $options; | ||
$this->getCommand(); | ||
} | ||
|
||
public function getCommand() | ||
{ | ||
if ($this->command === null) { | ||
$this->command = new Command($this->options); | ||
} | ||
return $this->command; | ||
} | ||
|
||
public function execute() | ||
{ | ||
$command = $this->getCommand(); | ||
if ($command->getExecuted()) { | ||
return false; | ||
} | ||
//var_dump($command->getExecCommand()); | ||
if (!$command->execute()) { | ||
$this->error = $command->getError(); | ||
} | ||
$this->output = $command->getOutput(); | ||
return true; | ||
} | ||
|
||
public function getError() | ||
{ | ||
return $this->error; | ||
} | ||
|
||
public function getVersion() | ||
{ | ||
$this->getCommand() | ||
->addArg('--version'); | ||
if ($this->execute()) { | ||
return $this->output; | ||
} | ||
return false; | ||
} | ||
|
||
public function addPage($pdfName) | ||
{ | ||
if (!file_exists($pdfName)) { | ||
$this->error = "Added page '" . $pdfName . "' does not exist"; | ||
return false; | ||
} | ||
$this->pages[] = $pdfName; | ||
return $this; | ||
} | ||
|
||
public function getPages() | ||
{ | ||
return $this->pages; | ||
} | ||
|
||
public function merge($target) | ||
{ | ||
$cmd = $this->getCommand(); | ||
$cmd->addArg('--empty', null, false); | ||
$cmd->addArg('--pages', $this->pages); | ||
$cmd->addArg('--', null, false); | ||
$cmd->addArg( $target, null, false); | ||
return $this->execute(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace EddTurtle\Qpdf\Tests; | ||
|
||
use EddTurtle\Qpdf\Pdf; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PdfTest extends TestCase | ||
{ | ||
|
||
public function testGetVersion() | ||
{ | ||
$pdf = new Pdf(); | ||
$version = $pdf->getVersion(); | ||
|
||
// Test it works | ||
$this->assertStringContainsString('qpdf version', $version); | ||
|
||
// Test no errors | ||
$this->assertEmpty($pdf->getError()); | ||
} | ||
|
||
public function testMerge() | ||
{ | ||
$pdf = new Pdf(); | ||
$pdf->addPage(__DIR__ . "/files/TestPdf.pdf"); | ||
$pdf->addPage(__DIR__ . "/files/TestPdf2.pdf"); | ||
$pdf->merge(__DIR__ . "/output/test-merge.pdf"); | ||
$this->assertEmpty($pdf->getError()); | ||
} | ||
|
||
} |
Binary file not shown.
Binary file not shown.