Skip to content

Commit

Permalink
Rewrite to use fpdf and fpdi
Browse files Browse the repository at this point in the history
  • Loading branch information
fetzi committed Mar 29, 2020
1 parent 8a89550 commit 1fd1f78
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 55 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: pecl
extensions: imagick

- uses: actions/checkout@v2

Expand All @@ -32,8 +30,5 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Remove Imagick policy
run: sudo rm /etc/ImageMagick-6/policy.xml

- name: Run test suite
run: composer run-script test
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
],
"require": {
"php": ">=7.2",
"ext-imagick": "*"
"setasign/fpdf": "^1.8",
"setasign/fpdi": "^2.3"
},
"require-dev": {
"phpunit/phpunit": "^8.0 || ^9.0",
Expand Down
15 changes: 2 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
![](https://github.com/karriereat/pdf-merge/workflows/test/badge.svg)
![](https://github.com/karriereat/pdf-merge/workflows/lint/badge.svg)

# Pdf Merge Solution based on Imagick
# Pdf Merge Solution for PHP

This package is a wrapper for the `Imagick` class that provides an elegant API for merging PDF files.
This package is a wrapper for the `Fpdi` class that provides an elegant API for merging PDF files.

## Installation

Expand All @@ -28,17 +28,6 @@ $pdfMerge->merge('/path/to/output.pdf');

Please note, that the `merge` method will throw an `NoFilesDefinedException` if no files where added.

### Setting custom Imagick Options
To be able to set options in the `Imagick` instance used you can pass an array on constructing the `PdfMerge` instance.

```php
$pdfMerge = new PdfMerge([
'density' => '200',
]);
```

To find all possible options please see the [Imagick class documentation](https://www.php.net/manual/en/imagick.setoption.php).

### Check for file existence
You can check if a file was already added for merging by calling:

Expand Down
26 changes: 12 additions & 14 deletions src/PdfMerge.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Karriere\PdfMerge\Exceptions\FileNotFoundException;
use Karriere\PdfMerge\Exceptions\NoFilesDefinedException;
use setasign\Fpdi\Fpdi;

class PdfMerge
{
Expand Down Expand Up @@ -71,24 +72,21 @@ public function merge(string $outputFilename): bool
throw new NoFilesDefinedException();
}

$pdf = new \Imagick();

foreach ($this->imagickOptions as $key => $value) {
$pdf->setOption($key, $value);
}
$pdf = new Fpdi();

foreach ($this->files as $file) {
$fh = fopen($file, 'r');
$pdf->readImageFile($fh);
fclose($fh);
$pageCount = $pdf->setSourceFile($file);

for ($i = 1; $i <= $pageCount; $i++) {
$pageId = $pdf->ImportPage($i);
$size = $pdf->getTemplateSize($pageId);
$pdf->AddPage($size['orientation'], $size);
$pdf->useImportedPage($pageId);
}
}

$pdf->setImageFormat('pdf');

$fh = fopen($outputFilename, 'w');
$res = $pdf->writeImagesFile($fh);
fclose($fh);
$pdf->Output('F', $outputFilename);

return $res;
return true;
}
}
34 changes: 12 additions & 22 deletions tests/PdfMergeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Karriere\PdfMerge\Exceptions\NoFilesDefinedException;
use Karriere\PdfMerge\PdfMerge;
use PHPUnit\Framework\TestCase;
use setasign\Fpdi\Fpdi;

class PdfMergeTest extends TestCase
{
Expand Down Expand Up @@ -54,20 +55,6 @@ public function it_can_generate_a_merged_file()
$this->assertPDFEquals(__DIR__ . '/files/expected/output.pdf', $outputFile);
}

/** @test */
public function it_can_handle_imagick_options()
{
$pdfMerge = new PdfMerge(['density' => '150']);
$file = __DIR__ . '/files/dummy.pdf';
$outputFile = sys_get_temp_dir() . '/output-150.pdf';

$pdfMerge->add($file);
$pdfMerge->add($file);

$this->assertTrue($pdfMerge->merge($outputFile));
$this->assertPDFEquals(__DIR__ . '/files/expected/output-150.pdf', $outputFile);
}

/** @test */
public function it_fails_on_generate_when_no_files_were_added()
{
Expand All @@ -79,18 +66,21 @@ public function it_fails_on_generate_when_no_files_were_added()

private static function assertPDFEquals(string $expected, string $actual): void
{
$actual = new \Imagick($actual);
$actual->resetIterator();
self::assertEquals(
filesize($expected),
filesize($actual),
'The file size of the PDF does not equal the file size from the expected output.'
);

$expected = new \Imagick($expected);
$expected->resetIterator();
$pdf = new Fpdi();

list(,$delta) = $actual->compareImages($expected, 1);
$expectedPageCount = $pdf->setSourceFile($expected);
$actualPageCount = $pdf->setSourceFile($actual);

self::assertEquals(
0.0,
$delta,
'The actual PDF is visually not equal to the expected PDF.'
$expectedPageCount,
$actualPageCount,
'The page count of the PDF does not equal the page count from the expected output.'
);
}
}
Binary file removed tests/files/expected/output-150.pdf
Binary file not shown.
Binary file modified tests/files/expected/output.pdf
Binary file not shown.

0 comments on commit 1fd1f78

Please sign in to comment.