Skip to content

Commit 4c2bb3a

Browse files
committed
Merge branch 'release/1.4.0'
2 parents e6b0584 + bcccc06 commit 4c2bb3a

File tree

8 files changed

+164
-26
lines changed

8 files changed

+164
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [1.4.0] - 2020-10-15
8+
### Changed
9+
- `PdfMerge` class can now automatically instantiate the merger class
10+
711
## [1.3.0] - 2020-10-04
812
### Changed
913
- No temporary files necessary for pdf merging anymore

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Requirements
1111

1212
Usage
1313
-----
14+
15+
#### Create PDF document from HTML contents
1416
```php
1517
// Create a new pdf instance.
1618
$pdf = Vianetz\Pdf\Model\PdfFactory::general()->create();
@@ -27,6 +29,23 @@ $pdf->addDocument($document);
2729
$pdf->saveToFile('test.pdf');
2830
```
2931

32+
#### Merge a PDF file and a PDF string into one PDF file
33+
```php
34+
// Load some random PDF contents
35+
$pdfString = file_get_contents('test1.pdf');
36+
37+
// Setup things
38+
$pdf = Vianetz\Pdf\Model\PdfFactory::general()->create();
39+
$pdfMerge = Vianetz\Pdf\Model\PdfMerge::create();
40+
41+
// Do the merge.
42+
$pdfMerge->mergePdfString($pdfString);
43+
$pdfMerge->mergePdfFile('test2.pdf');
44+
45+
// Save the result PDF to file result.pdf.
46+
$pdfMerge->saveToFile('result.pdf');
47+
```
48+
3049
Frequently Asked Questions
3150
--------------------------
3251
Please find the Frequently Asked Questions [on our website](https://www.vianetz.com/en/faq).

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 8
2+
level: max
33
fileExtensions:
44
- php
55
paths:

src/Model/Pdf.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
use Vianetz\Pdf\NoDataException;
2929

30-
class Pdf
30+
class Pdf implements PdfInterface
3131
{
3232
/**
3333
* The generator instance.
@@ -77,46 +77,37 @@ final public function __construct(
7777
MergerInterface $merger
7878
) {
7979
$this->generator = $generator;
80-
$this->pdfMerge = PdfMerge::createWithMerger($merger);
80+
$this->pdfMerge = PdfMerge::create($merger);
8181
$this->config = $config;
8282
$this->eventManager = $eventManager;
8383
}
8484

8585
/**
86-
* Get pdf file contents as string.
87-
*
88-
* @api
89-
* @return string
86+
* {@inheritDoc}
9087
*/
9188
final public function getContents()
9289
{
9390
if ($this->contents === null) {
94-
$this->contents = $this->renderPdfContentsForAllDocuments();
91+
$this->renderPdfContentsForAllDocuments();
92+
$this->contents = $this->pdfMerge->getContents();
9593
}
9694

9795
return $this->contents;
9896
}
9997

10098
/**
101-
* Save pdf contents to file.
102-
*
103-
* @param string $fileName
104-
*
105-
* @api
106-
* @return boolean true in case of success
99+
* {@inheritDoc}
107100
*/
108101
final public function saveToFile($fileName)
109102
{
110103
$pdfContents = $this->getContents();
111104

112-
return (@file_put_contents($fileName, $pdfContents) !== false);
105+
return @file_put_contents($fileName, $pdfContents) !== false;
113106
}
114107

115108
/**
116109
* Add a new document to generate.
117110
*
118-
* @param DocumentInterface $documentModel
119-
*
120111
* @api
121112
* @return Pdf
122113
*/
@@ -164,7 +155,7 @@ public function getConfig()
164155
/**
165156
* Return merged pdf contents of all documents and save it to single temporary files.
166157
*
167-
* @return string
158+
* @return void
168159
* @throws \Vianetz\Pdf\NoDataException
169160
*/
170161
private function renderPdfContentsForAllDocuments()
@@ -200,7 +191,5 @@ private function renderPdfContentsForAllDocuments()
200191
if (! $hasData) {
201192
throw new NoDataException('No data to print.');
202193
}
203-
204-
return $this->pdfMerge->getResult();
205194
}
206195
}

src/Model/PdfInterface.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Pdf interface class
4+
*
5+
* @section LICENSE
6+
* This file is created by vianetz <[email protected]>.
7+
* The code is distributed under the GPL license.
8+
*
9+
* If you did not receive a copy of the license and are unable to
10+
* obtain it through the world-wide-web, please send an email
11+
* to [email protected] so we can send you a copy immediately.
12+
*
13+
* @package Vianetz\Pdf
14+
* @author Christoph Massmann, <[email protected]>
15+
* @link https://www.vianetz.com
16+
* @copyright Copyright (c) since 2006 vianetz - Dipl.-Ing. C. Massmann (https://www.vianetz.com)
17+
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GENERAL PUBLIC LICENSE
18+
*/
19+
20+
namespace Vianetz\Pdf\Model;
21+
22+
interface PdfInterface
23+
{
24+
/**
25+
* @return string
26+
* @throws \Vianetz\Pdf\NoDataException
27+
*/
28+
public function getContents();
29+
30+
/**
31+
* @param string $fileName
32+
*
33+
* @api
34+
* @return bool true in case of success
35+
* @throws \Vianetz\Pdf\NoDataException
36+
*/
37+
public function saveToFile($fileName);
38+
}

src/Model/PdfMerge.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,43 @@
1717

1818
namespace Vianetz\Pdf\Model;
1919

20-
class PdfMerge
20+
use Vianetz\Pdf\Model\Merger\Fpdi;
21+
use Vianetz\Pdf\NoDataException;
22+
23+
class PdfMerge implements PdfInterface
2124
{
2225
/** @var \Vianetz\Pdf\Model\MergerInterface */
2326
private $merger;
2427

25-
public function __construct(MergerInterface $merger)
28+
/** @var int */
29+
private $pageCount = 0;
30+
31+
public function __construct(MergerInterface $merger = null)
2632
{
33+
if ($merger === null) {
34+
$merger = new Fpdi();
35+
}
36+
2737
$this->merger = $merger;
2838
}
2939

3040
/**
3141
* @return \Vianetz\Pdf\Model\PdfMerge
3242
*/
33-
public static function createWithMerger(MergerInterface $merger)
43+
public static function create(MergerInterface $merger = null)
3444
{
3545
return new self($merger);
3646
}
3747

48+
/**
49+
* @deprecated
50+
* @return \Vianetz\Pdf\Model\PdfMerge
51+
*/
52+
public static function createWithMerger(MergerInterface $merger)
53+
{
54+
return self::create($merger);
55+
}
56+
3857
/**
3958
* {@inheritDoc}
4059
*
@@ -58,8 +77,8 @@ public function mergePdfFile($pdfFile, $pdfBackgroundFile = null, $pdfBackground
5877
* {@inheritDoc}
5978
*
6079
* @param string $pdfString
61-
* @param null|string $pdfBackgroundFile
62-
* @param null|string $pdfBackgroundFileForFirstPage
80+
* @param string|null $pdfBackgroundFile
81+
* @param string|null $pdfBackgroundFileForFirstPage
6382
*
6483
* @return void
6584
*/
@@ -68,6 +87,7 @@ public function mergePdfString($pdfString, $pdfBackgroundFile = null, $pdfBackgr
6887
$pageCount = $this->merger->countPages($pdfString);
6988
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
7089
$this->merger->addPage();
90+
$this->pageCount++;
7191

7292
if ($pageNumber === 1 && ! empty($pdfBackgroundFileForFirstPage)) {
7393
$this->merger->importBackgroundTemplateFile($pdfBackgroundFileForFirstPage);
@@ -80,10 +100,35 @@ public function mergePdfString($pdfString, $pdfBackgroundFile = null, $pdfBackgr
80100
}
81101

82102
/**
103+
* @deprecated use self::getContents() instead
104+
*
83105
* @return string
106+
* @throws \Vianetz\Pdf\NoDataException
84107
*/
85108
public function getResult()
86109
{
110+
return $this->getContents();
111+
}
112+
113+
/**
114+
* {@inheritDoc}
115+
*/
116+
final public function getContents()
117+
{
118+
if ($this->pageCount === 0) {
119+
throw new NoDataException('No data to print.');
120+
}
121+
87122
return $this->merger->getPdfContents();
88123
}
124+
125+
/**
126+
* {@inheritDoc}
127+
*/
128+
final public function saveToFile($fileName)
129+
{
130+
$pdfContents = $this->getContents();
131+
132+
return @file_put_contents($fileName, $pdfContents) !== false;
133+
}
89134
}

src/Test/PdfMergeTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @section LICENSE
4+
* This file is created by vianetz <[email protected]>.
5+
* The code is distributed under the GPL license.
6+
*
7+
* If you did not receive a copy of the license and are unable to
8+
* obtain it through the world-wide-web, please send an email
9+
* to [email protected] so we can send you a copy immediately.
10+
*
11+
* @package Vianetz\Pdf
12+
* @author Christoph Massmann, <[email protected]>
13+
* @link https://www.vianetz.com
14+
* @copyright Copyright (c) since 2006 vianetz - Dipl.-Ing. C. Massmann (https://www.vianetz.com)
15+
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GENERAL PUBLIC LICENSE
16+
*/
17+
18+
namespace Vianetz\Pdf\Test;
19+
20+
use PHPUnit\Framework\TestCase;
21+
use Vianetz\Pdf\Model\PdfMerge;
22+
use Vianetz\Pdf\NoDataException;
23+
24+
final class PdfMergeTest extends TestCase
25+
{
26+
/**
27+
* @return \Vianetz\Pdf\Model\PdfMerge
28+
*/
29+
private function getPdfMergeMock()
30+
{
31+
return PdfMerge::create();
32+
}
33+
34+
/**
35+
* @return void
36+
*/
37+
public function testMergeGeneratesEmptyPdfIfNoContentsAdded()
38+
{
39+
$pdfMergeMock = $this->getPdfMergeMock();
40+
$this->expectException(NoDataException::class);
41+
42+
$pdfMergeMock->getContents();
43+
}
44+
}

src/Test/PdfTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
use PHPUnit\Framework\TestCase;
2121
use Vianetz\Pdf\Model\Config;
22-
use Vianetz\Pdf\Model\EventManagerInterface;
2322
use Vianetz\Pdf\Model\Generator\AbstractGenerator;
2423
use Vianetz\Pdf\Model\PdfFactory;
2524
use Vianetz\Pdf\NoDataException;

0 commit comments

Comments
 (0)