Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdierich committed Jun 16, 2020
0 parents commit dd148cc
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitattributes
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
composer.lock
phpunit.xml
.idea/
vendor/
31 changes: 31 additions & 0 deletions composer.json
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"
}
}
}
12 changes: 12 additions & 0 deletions phpunit.xml.dist
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>
133 changes: 133 additions & 0 deletions src/Pdf.php
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);
}
}
39 changes: 39 additions & 0 deletions tests/PdfTest.php
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 added tests/files/document1.pdf
Binary file not shown.
Binary file added tests/files/document2.pdf
Binary file not shown.

0 comments on commit dd148cc

Please sign in to comment.