Skip to content

Commit 3a1d46c

Browse files
committed
added fpdf merger as new default (more lightweight), removed deprecated zendpdf merger, added zugferd fpdf merger
Signed-off-by: Christoph Massmann <[email protected]>
1 parent 571714e commit 3a1d46c

17 files changed

+239
-121
lines changed

README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
# vianetz PDF Library
1+
# vianetz Pdf Library
22

33
This library offers an easy-to-use API for PDF generation and merging.
4-
Internally it uses the DomPDF library for PDF generation and TCPDF for merging.
4+
Internally it uses the [DomPDF library](https://github.com/dompdf/dompdf) for PDF generation and [FPDI](https://github.com/Setasign/FPDI) for merging.
5+
6+
More information about this PDF API can also be found [on my website](https://www.vianetz.com/en/pdf-invoice-api-magento/).
57

68
## Usage
79

810
### Create PDF document from HTML contents
911

1012
```php
1113
// Create a new pdf instance.
12-
$pdf = Vianetz\Pdf\Model\PdfFactory::general()->create();
14+
$pdf = \Vianetz\Pdf\Model\PdfFactory::general()->create();
1315

1416
// Create the document. You can return any kind of HTML content here.
1517
$document = new \Vianetz\Pdf\Model\HtmlDocument('<strong>Hello</strong> World!');
1618

1719
// Add our document to the pdf. You can add as many documents as you like
1820
// as they will all be merged into one PDF file.
19-
$pdf->addDocument($document);
21+
$pdf->add($document);
2022

2123
// Save the resulting PDF to file test.pdf - That's it :-)
2224
$pdf->saveToFile('test.pdf');
@@ -28,15 +30,14 @@ $pdf->saveToFile('test.pdf');
2830
$pdfString = file_get_contents('test1.pdf');
2931

3032
// Setup things
31-
$pdf = Vianetz\Pdf\Model\PdfFactory::general()->create();
32-
$pdfMerge = Vianetz\Pdf\Model\PdfMerge::create();
33+
$pdfMerge = \Vianetz\Pdf\Model\PdfMerge::create();
3334

3435
// Do the merge.
35-
$pdfMerge->mergePdfString($pdfString);
36+
$pdfMerge->mergePdfString($pdfString, 'background.pdf');
3637
$pdfMerge->mergePdfFile('test2.pdf');
3738

3839
// Save the result PDF to file result.pdf.
39-
$pdfMerge->saveToFile('result.pdf');
40+
file_put_contents($fileName, $pdfMerge->toPdf());
4041
```
4142

4243
### Tips & Tricks
@@ -50,12 +51,6 @@ Please find the Frequently Asked Questions [on my website](https://www.vianetz.c
5051
If you have any issues or suggestions with this extension, please do not hesitate to
5152
[contact me](https://www.vianetz.com/en/contacts).
5253

53-
## Credits
54-
Of course this extension would not have been possible without the great open source eco-system.
55-
Therewith credits go to:
56-
- [DomPDF](https://github.com/dompdf/dompdf)
57-
- [FPDI](https://github.com/Setasign/FPDI)
58-
5954
## License
6055
[GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.html)
6156
See also LICENSE file.

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@
2727
"require": {
2828
"php": "^7.4|^8",
2929
"dompdf/dompdf": "^2.0|^3.0",
30-
"setasign/fpdi": "^2.0",
31-
"tecnickcom/tcpdf": "^6.4"
30+
"setasign/fpdi": "^2.6",
31+
"setasign/fpdf": "^1.8"
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^9|^10|^11",
3535
"phpstan/phpstan": "^1.8",
3636
"phpstan/phpstan-phpunit": "^1.1"
3737
},
3838
"suggest": {
39+
"horstoeko/zugferd": "Create Zugferd documents with this merger library",
3940
"vianetz/signaturportal-api": "Use this library to sign your generated PDF documents.",
40-
"zf1/zend-pdf": "Necessary if you want to use Zend_Pdf for merging. By default this package uses the fpdi merger."
41+
"tecnickcom/tcpdf": "Necessary if you want to use TCPDF for merging. By default this package uses the fpdf merger."
4142
},
4243
"config": {
4344
"allow-plugins": {

src/Exception.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
3-
* Pdf Exception Class
4-
*
55
* @section LICENSE
66
* This file is created by vianetz <[email protected]>.
77
* The code is distributed under the GPL license.

src/FileNotFoundException.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
declare(strict_types=1);
3+
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;
21+
22+
class FileNotFoundException extends \Exception
23+
{
24+
}

src/Model/Generator/AbstractGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ abstract class AbstractGenerator implements Pdfable // @todo rename Generator to
3737
public const DEBUG_FILE_NAME = 'vianetz_pdf_generator_debug.html';
3838
protected Config $config;
3939

40-
abstract public function convert(string $html): self;
40+
abstract public function import(string $html): self;
4141

4242
public function __construct(?Config $config = null)
4343
{

src/Model/Generator/Dompdf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class Dompdf extends AbstractGenerator
3434
private \Dompdf\Dompdf $domPdf;
3535

3636
/** @throws \Exception */
37-
public function convert(string $html): self
37+
public function import(string $html): self
3838
{
3939
$this->writeDebugFile($html);
4040

src/Model/Merger/AbstractMerger.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@
1919

2020
namespace Vianetz\Pdf\Model\Merger;
2121

22+
use Vianetz\Pdf\FileNotFoundException;
2223
use Vianetz\Pdf\Model\MergerInterface;
2324

2425
abstract class AbstractMerger implements MergerInterface
2526
{
26-
/**
27-
* Add the background pdf (if enabled and file exists).
28-
*/
27+
/** @inheritDoc */
2928
public function importBackgroundTemplateFile(string $pdfBackgroundFile): void
3029
{
3130
if (empty($pdfBackgroundFile) || ! file_exists($pdfBackgroundFile)) {
32-
return;
31+
throw new FileNotFoundException('pdf background template file does not exist');
3332
}
3433

3534
$this->importPageFromFile($pdfBackgroundFile, 1); // We assume the background pdf has only one page.

src/Model/Merger/Fpdf.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
declare(strict_types=1);
3+
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\Merger;
21+
22+
use setasign\Fpdi\Fpdi as FpdfFpdi;
23+
use setasign\Fpdi\PdfParser\StreamReader;
24+
use Vianetz\Pdf\Model\Config;
25+
26+
class Fpdf extends AbstractMerger
27+
{
28+
private const OUTPUT_MODE_STRING = 'S';
29+
private const OUTPUT_FORMAT_LANDSCAPE = 'L';
30+
private const OUTPUT_FORMAT_PORTRAIT = 'P';
31+
protected FpdfFpdi $fpdiModel;
32+
private string $orientation = self::OUTPUT_FORMAT_PORTRAIT;
33+
private string $paper;
34+
35+
public function __construct(?Config $config = null, ?FpdfFpdi $fpdiModel = null)
36+
{
37+
$config ??= new Config();
38+
39+
if ($config->getPdfOrientation() === Config::PAPER_ORIENTATION_PORTRAIT) {
40+
$this->orientation = self::OUTPUT_FORMAT_PORTRAIT;
41+
} elseif ($config->getPdfOrientation() === Config::PAPER_ORIENTATION_LANDSCAPE) {
42+
$this->orientation = self::OUTPUT_FORMAT_LANDSCAPE;
43+
}
44+
45+
$this->paper = $config->getPdfSize();
46+
47+
$this->fpdiModel = $fpdiModel ?? new FpdfFpdi($this->orientation, 'mm', $this->paper);
48+
49+
$this->fpdiModel->SetAuthor($config->getPdfAuthor());
50+
$this->fpdiModel->SetTitle($config->getPdfTitle());
51+
$this->fpdiModel->setCreator('https://github.com/vianetz/pdf-generator');
52+
}
53+
54+
/**
55+
* Import the specified page number from the given file into the current pdf model.
56+
*
57+
* @param string|resource|StreamReader $file
58+
*
59+
* @throws \setasign\Fpdi\PdfParser\PdfParserException|\setasign\Fpdi\PdfReader\PdfReaderException
60+
*/
61+
public function importPageFromFile($file, int $pageNumber): void
62+
{
63+
$this->fpdiModel->setSourceFile($file);
64+
$pageId = $this->fpdiModel->importPage($pageNumber);
65+
$this->fpdiModel->useTemplate($pageId);
66+
}
67+
68+
public function importPageFromPdfString(string $pdfString, int $pageNumber): void
69+
{
70+
$this->importPageFromFile($this->createPdfStream($pdfString), $pageNumber);
71+
}
72+
73+
public function toPdf(): string
74+
{
75+
return $this->fpdiModel->Output('', self::OUTPUT_MODE_STRING);
76+
}
77+
78+
public function countPages(string $pdfString): int
79+
{
80+
return $this->fpdiModel->setSourceFile($this->createPdfStream($pdfString));
81+
}
82+
83+
public function addPage(): self
84+
{
85+
$this->fpdiModel->addPage($this->orientation, $this->paper);
86+
87+
return $this;
88+
}
89+
90+
public function addAttachment(string $fileName): self
91+
{
92+
throw new \RuntimeException('not implemented');
93+
}
94+
95+
private function createPdfStream(string $pdfString): StreamReader
96+
{
97+
return StreamReader::createByString($pdfString);
98+
}
99+
}

src/Model/Merger/Fpdi.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
declare(strict_types=1);
33

44
/**
5-
* This class is responsible for merging individual PDF documents and eventually adding the background PDF template file.
6-
*
75
* @section LICENSE
86
* This file is created by vianetz <[email protected]>.
97
* The code is distributed under the GPL license.
@@ -25,12 +23,11 @@
2523
use setasign\Fpdi\Tcpdf\Fpdi as TcpdfFpdi;
2624
use Vianetz\Pdf\Model\Config;
2725

28-
final class Fpdi extends AbstractMerger
26+
final class Fpdi extends AbstractMerger // @todo rename to tcpdf
2927
{
3028
private const OUTPUT_MODE_STRING = 'S';
3129
private const OUTPUT_FORMAT_LANDSCAPE = 'L';
3230
private const OUTPUT_FORMAT_PORTRAIT = 'P';
33-
3431
private TcpdfFpdi $fpdiModel;
3532
private string $orientation = self::OUTPUT_FORMAT_PORTRAIT;
3633
private string $paper = 'a4';
@@ -45,24 +42,23 @@ public function __construct(?Config $config = null)
4542
$this->orientation = self::OUTPUT_FORMAT_LANDSCAPE;
4643
}
4744

45+
$this->paper = $config->getPdfSize();
46+
4847
$this->fpdiModel = new TcpdfFpdi($this->orientation, 'mm', $this->paper);
4948

5049
$this->fpdiModel->SetAuthor($config->getPdfAuthor());
5150
$this->fpdiModel->SetTitle($config->getPdfTitle());
52-
$this->fpdiModel->setCreator('vianetz PDF Generator (https://github.com/vianetz/pdf-generator)');
51+
$this->fpdiModel->setCreator('https://github.com/vianetz/pdf-generator');
5352
$this->fpdiModel->setPrintHeader(false);
5453
$this->fpdiModel->setPrintFooter(false);
55-
56-
$this->paper = $config->getPdfSize();
5754
}
5855

5956
/**
6057
* Import the specified page number from the given file into the current pdf model.
6158
*
6259
* @param string|resource|StreamReader $file
6360
*
64-
* @throws \setasign\Fpdi\PdfParser\PdfParserException
65-
* @throws \setasign\Fpdi\PdfReader\PdfReaderException
61+
* @throws \setasign\Fpdi\PdfParser\PdfParserException|\setasign\Fpdi\PdfReader\PdfReaderException
6662
*/
6763
public function importPageFromFile($file, int $pageNumber): void
6864
{
@@ -93,7 +89,12 @@ public function addPage(): self
9389
return $this;
9490
}
9591

96-
private function createPdfStream(string $pdfString): \setasign\Fpdi\PdfParser\StreamReader
92+
public function addAttachment(string $fileName): self
93+
{
94+
throw new \RuntimeException('not implemented');
95+
}
96+
97+
private function createPdfStream(string $pdfString): StreamReader
9798
{
9899
return StreamReader::createByString($pdfString);
99100
}

src/Model/Merger/Zendpdf.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)