-
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
0 parents
commit dd148cc
Showing
8 changed files
with
227 additions
and
0 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,8 @@ | ||
# Path-based git attributes | ||
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html | ||
|
||
# Ignore all test and documentation with "export-ignore". | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/tests export-ignore |
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,4 @@ | ||
composer.lock | ||
phpunit.xml | ||
.idea/ | ||
vendor/ |
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,31 @@ | ||
{ | ||
"name": "tobiasdierich/php-qpdf", | ||
"description": "A PDF manipulation library based on QPDF", | ||
"keywords": ["pdf", "qpdf"], | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Tobias Dierich", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.4.0", | ||
"mikehaertl/php-shellcommand": "^1.5.0", | ||
"mikehaertl/php-tmpfile": "^1.1.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": ">9.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"tobiasdierich\\qpdf\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
defaultTestSuite="all"> | ||
<testsuites> | ||
<testsuite name="all"> | ||
<directory suffix="Test.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,133 @@ | ||
<?php | ||
|
||
namespace tobiasdierich\qpdf; | ||
|
||
use mikehaertl\shellcommand\Command; | ||
use mikehaertl\tmp\File; | ||
|
||
class Pdf | ||
{ | ||
const TMP_PREFIX = 'tmp_php_qpdf_'; | ||
|
||
/** | ||
* @var \mikehaertl\shellcommand\Command | ||
*/ | ||
protected $command; | ||
|
||
/** | ||
* @var \mikehaertl\tmp\File | ||
*/ | ||
protected $tmpFile; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $error; | ||
|
||
/** | ||
* PDF constructor. | ||
* | ||
* @param string $pdf | ||
*/ | ||
public function __construct($pdf) | ||
{ | ||
$this->getCommand() | ||
->addArg($pdf, null, true); | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* | ||
* @return \tobiasdierich\qpdf\Pdf | ||
*/ | ||
public function background($file) | ||
{ | ||
$this->getCommand() | ||
->addArg('--underlay', $file, true) | ||
->addArg('--repeat=', 1) | ||
->addArg('--'); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function execute() | ||
{ | ||
$command = $this->getCommand(); | ||
if ($command->getExecuted()) { | ||
return false; | ||
} | ||
|
||
$outputFilename = $this->getTmpFile()->getFileName(); | ||
|
||
$command->addArg($outputFilename, null, true); | ||
|
||
if (!$command->execute()) { | ||
$this->error = $command->getError(); | ||
|
||
if ($outputFilename && !(file_exists($outputFilename) && filesize($outputFilename) !== 0)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @return bool|false|string | ||
*/ | ||
public function toString() | ||
{ | ||
if (!$this->getCommand()->getExecuted() && !$this->execute()) { | ||
return false; | ||
} | ||
|
||
return file_get_contents($this->getTmpFile()->getFileName()); | ||
} | ||
|
||
/** | ||
* @return \mikehaertl\shellcommand\Command | ||
*/ | ||
public function getCommand() | ||
{ | ||
if ($this->command === null) { | ||
$this->command = new Command([ | ||
'command' => 'qpdf', | ||
]); | ||
} | ||
|
||
return $this->command; | ||
} | ||
|
||
/** | ||
* @return \mikehaertl\tmp\File | ||
*/ | ||
public function getTmpFile() | ||
{ | ||
if ($this->tmpFile === null) { | ||
$this->tmpFile = new File('', '.pdf', self::TMP_PREFIX); | ||
} | ||
|
||
return $this->tmpFile; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getError() | ||
{ | ||
return $this->error; | ||
} | ||
|
||
/** | ||
* @param string $pdf | ||
* | ||
* @return \tobiasdierich\qpdf\Pdf | ||
*/ | ||
public static function create($pdf) | ||
{ | ||
return new static($pdf); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use tobiasdierich\qpdf\Pdf; | ||
|
||
class PdfTest extends TestCase | ||
{ | ||
/** | ||
* @var \tobiasdierich\qpdf\Pdf | ||
*/ | ||
private $pdf; | ||
|
||
public function testCanSetBackground() | ||
{ | ||
$document1 = $this->getDocument1(); | ||
$document2 = $this->getDocument2(); | ||
|
||
$this->pdf = new Pdf($document1); | ||
$this->assertInstanceOf('tobiasdierich\qpdf\Pdf', $this->pdf->background($document2)); | ||
$this->assertTrue($this->pdf->execute()); | ||
$this->assertFileExists($this->pdf->getTmpFile()->getFileName()); | ||
|
||
|
||
$tmpFile = $this->pdf->getTmpFile()->getFileName(); | ||
$this->assertEquals("qpdf '$document1' '--underlay' '$document2' '--repeat'='1' '--' '$tmpFile'", (string) $this->pdf->getCommand()); | ||
} | ||
|
||
protected function getDocument1() | ||
{ | ||
return __DIR__ . '/files/document1.pdf'; | ||
} | ||
|
||
protected function getDocument2() | ||
{ | ||
return __DIR__ . '/files/document2.pdf'; | ||
} | ||
} |
Binary file not shown.
Binary file not shown.