Skip to content

Commit

Permalink
Add first version ⭐
Browse files Browse the repository at this point in the history
  • Loading branch information
Edd Turtle committed Nov 24, 2020
1 parent 616ef83 commit cd01b8c
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
composer.lock
vendor
!.gitkeep
tests/output
5 changes: 3 additions & 2 deletions README.md
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.
29 changes: 29 additions & 0 deletions composer.json
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/"
}
}
}
12 changes: 12 additions & 0 deletions phpunit.xml
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>
92 changes: 92 additions & 0 deletions src/Pdf.php
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();
}

}
32 changes: 32 additions & 0 deletions tests/PdfTest.php
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 added tests/files/TestPdf.pdf
Binary file not shown.
Binary file added tests/files/TestPdf2.pdf
Binary file not shown.

0 comments on commit cd01b8c

Please sign in to comment.