Skip to content

Commit 04c20ca

Browse files
committed
Merge branch 'release/3.0.0'
2 parents e5ace70 + 427feda commit 04c20ca

File tree

8 files changed

+102
-100
lines changed

8 files changed

+102
-100
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ 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+
## [3.0.0] - 2021-06-01
8+
### Changed
9+
- Refactored logic for pdf attachment files
10+
- Switched from deprecated FPDF library to TCPDF
11+
712
## [2.2.0] - 2021-02-05
813
### Added
914
- Added support for PHP 8
15+
### Changed
1016
- Updated PDF libraries
1117

1218
## [2.1.0] - 2020-12-19

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"php": "^5.6|^7|^8",
2929
"dompdf/dompdf": "^0.8|^1.0",
3030
"setasign/fpdi": "^2.0",
31-
"setasign/fpdf": "^1.8|^2"
31+
"tecnickcom/tcpdf": "^6.4"
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^7",

src/Model/Document.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,15 @@
2121

2222
class Document implements DocumentInterface
2323
{
24-
/**
25-
* @var string
26-
*/
24+
/** @var string */
2725
private $htmlContents;
2826

29-
/**
30-
* @var string
31-
*/
27+
/** @var string */
3228
private $pdfBackgroundFile = '';
3329

34-
/**
35-
* @var string
36-
*/
30+
/** @var string */
3731
private $pdfBackgroundFileForFirstPage = '';
3832

39-
/**
40-
* @var string
41-
*/
42-
private $pdfAttachmentFile = '';
43-
4433
/**
4534
* @param string $htmlContents
4635
*
@@ -94,23 +83,6 @@ public function setPdfBackgroundFileForFirstPage($pdfFile)
9483
$this->pdfBackgroundFileForFirstPage = $pdfFile;
9584
}
9685

97-
/**
98-
* @return string
99-
*/
100-
public function getPdfAttachmentFile()
101-
{
102-
return $this->pdfAttachmentFile;
103-
}
104-
105-
/**
106-
* @param string $pdfFile
107-
* @return void
108-
*/
109-
public function setPdfAttachmentFile($pdfFile)
110-
{
111-
$this->pdfAttachmentFile = $pdfFile;
112-
}
113-
11486
/**
11587
* @return string
11688
*/

src/Model/DocumentInterface.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22
/**
3-
* Pdf document interface class
4-
*
53
* @section LICENSE
64
* This file is created by vianetz <[email protected]>.
75
* The code is distributed under the GPL license.
@@ -20,7 +18,7 @@
2018
namespace Vianetz\Pdf\Model;
2119

2220
/**
23-
* Interface DocumentInterface
21+
* @todo rename to HtmlDocumentInterface
2422
*/
2523
interface DocumentInterface
2624
{
@@ -53,17 +51,6 @@ public function getPdfBackgroundFileForFirstPage();
5351
*/
5452
public function setPdfBackgroundFileForFirstPage($pdfFile);
5553

56-
/**
57-
* @return string
58-
*/
59-
public function getPdfAttachmentFile();
60-
61-
/**
62-
* @param string $pdfFile
63-
* @return void
64-
*/
65-
public function setPdfAttachmentFile($pdfFile);
66-
6754
/**
6855
* @return string
6956
*/

src/Model/Merger/Fpdi.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,59 +22,48 @@
2222
namespace Vianetz\Pdf\Model\Merger;
2323

2424
use setasign\Fpdi\PdfParser\StreamReader;
25+
use setasign\Fpdi\Tcpdf\Fpdi as FpdiModel;
2526
use Vianetz\Pdf\Model\Config;
2627

2728
final class Fpdi extends AbstractMerger
2829
{
29-
/**
30-
* @var string
31-
*/
30+
/** @var string */
3231
const OUTPUT_MODE_STRING = 'S';
3332

34-
/**
35-
* @var string
36-
*/
33+
/** @var string */
3734
const OUTPUT_FORMAT_LANDSCAPE = 'L';
3835

39-
/**
40-
* @var string
41-
*/
36+
/** @var string */
4237
const OUTPUT_FORMAT_PORTRAIT = 'P';
4338

44-
/**
45-
* The FPDI model instance.
46-
*
47-
* @var \setasign\Fpdi\Fpdi
48-
*/
39+
/** @var FpdiModel */
4940
private $fpdiModel;
5041

51-
/**
52-
* @var string
53-
*/
42+
/** @var string */
5443
private $orientation = self::OUTPUT_FORMAT_PORTRAIT;
5544

56-
/**
57-
* @var string
58-
*/
45+
/** @var string */
5946
private $paper = 'a4';
6047

6148
public function __construct(\Vianetz\Pdf\Model\Config $config = null)
6249
{
63-
$this->fpdiModel = new \setasign\Fpdi\Fpdi();
64-
6550
if (empty($config)) {
6651
$config = new \Vianetz\Pdf\Model\Config();
6752
}
6853

69-
$this->fpdiModel->SetAuthor($config->getPdfAuthor());
70-
$this->fpdiModel->SetTitle($config->getPdfTitle());
71-
7254
if ($config->getPdfOrientation() === Config::PAPER_ORIENTATION_PORTRAIT) {
7355
$this->orientation = self::OUTPUT_FORMAT_PORTRAIT;
7456
} elseif ($config->getPdfOrientation() === Config::PAPER_ORIENTATION_LANDSCAPE) {
7557
$this->orientation = self::OUTPUT_FORMAT_LANDSCAPE;
7658
}
7759

60+
$this->fpdiModel = new FpdiModel($this->orientation, 'mm', $this->paper);
61+
62+
$this->fpdiModel->SetAuthor($config->getPdfAuthor());
63+
$this->fpdiModel->SetTitle($config->getPdfTitle());
64+
$this->fpdiModel->setPrintHeader(false);
65+
$this->fpdiModel->setPrintFooter(false);
66+
7867
$this->paper = $config->getPdfSize();
7968
}
8069

@@ -109,7 +98,7 @@ public function importPageFromPdfString($pdfString, $pageNumber)
10998
*/
11099
public function getPdfContents()
111100
{
112-
return $this->fpdiModel->Output(self::OUTPUT_MODE_STRING);
101+
return $this->fpdiModel->Output('', self::OUTPUT_MODE_STRING);
113102
}
114103

115104
/**

src/Model/Pdf.php

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ class Pdf implements PdfInterface
3636
*/
3737
private $generator;
3838

39-
/**
40-
* @var \Vianetz\Pdf\Model\PdfMerge
41-
*/
39+
/** @var \Vianetz\Pdf\Model\PdfMerge */
4240
private $pdfMerge;
4341

4442
/**
@@ -51,18 +49,14 @@ class Pdf implements PdfInterface
5149
/**
5250
* Initialize empty array for PDF documents to print.
5351
*
54-
* @var array<\Vianetz\Pdf\Model\DocumentInterface>
52+
* @var array<\Vianetz\Pdf\Model\DocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface>
5553
*/
56-
private $documents = array();
54+
private $documents = [];
5755

58-
/**
59-
* @var Config
60-
*/
56+
/** @var Config */
6157
protected $config;
6258

63-
/**
64-
* @var \Vianetz\Pdf\Model\EventManagerInterface
65-
*/
59+
/** @var \Vianetz\Pdf\Model\EventManagerInterface */
6660
protected $eventManager;
6761

6862
/**
@@ -108,10 +102,12 @@ final public function saveToFile($fileName)
108102
/**
109103
* Add a new document to generate.
110104
*
105+
* @param \Vianetz\Pdf\Model\DocumentInterface|\Vianetz\Pdf\Model\PdfDocumentInterface
106+
*
111107
* @api
112108
* @return Pdf
113109
*/
114-
final public function addDocument(DocumentInterface $documentModel)
110+
final public function addDocument($documentModel)
115111
{
116112
$this->documents[] = $documentModel;
117113
// Reset cached pdf contents.
@@ -162,30 +158,31 @@ private function renderPdfContentsForAllDocuments()
162158
{
163159
$hasData = false;
164160
foreach ($this->documents as $documentInstance) {
165-
if (! $documentInstance instanceof DocumentInterface) {
166-
continue;
167-
}
168-
169161
$this->eventManager->dispatch('vianetz_pdf_document_render_before', ['document' => $documentInstance]);
170-
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_before', ['document' => $documentInstance]);
171162

172-
$pdfContents = $this->generator->renderPdfDocument($documentInstance);
173-
if (empty($pdfContents)) {
174-
continue;
175-
}
163+
if ($documentInstance instanceof DocumentInterface) {
164+
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_before', ['document' => $documentInstance]);
176165

177-
$hasData = true;
166+
$pdfContents = $this->generator->renderPdfDocument($documentInstance);
167+
if (empty($pdfContents)) {
168+
continue;
169+
}
178170

179-
$this->eventManager->dispatch('vianetz_pdf_document_render_after', ['document' => $documentInstance]);
180-
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_after', ['document' => $documentInstance]);
171+
$hasData = true;
172+
173+
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_render_after', ['document' => $documentInstance]);
181174

182-
$this->pdfMerge->mergePdfString($pdfContents, $documentInstance->getPdfBackgroundFile(), $documentInstance->getPdfBackgroundFileForFirstPage());
175+
$this->pdfMerge->mergePdfString($pdfContents, $documentInstance->getPdfBackgroundFile(), $documentInstance->getPdfBackgroundFileForFirstPage());
183176

184-
if (! empty($documentInstance->getPdfAttachmentFile())) {
185-
$this->pdfMerge->mergePdfFile($documentInstance->getPdfAttachmentFile());
177+
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_merge_after', [
178+
'merger' => $this->pdfMerge,
179+
'document' => $documentInstance,
180+
]);
181+
} elseif ($documentInstance instanceof PdfDocumentInterface) {
182+
$this->pdfMerge->mergePdfFile($documentInstance->getPdfFile());
186183
}
187184

188-
$this->eventManager->dispatch('vianetz_pdf_' . $documentInstance->getDocumentType() . '_document_merge_after', ['merger' => $this->pdfMerge, 'document' => $documentInstance]);
185+
$this->eventManager->dispatch('vianetz_pdf_document_render_after', ['document' => $documentInstance]);
189186
}
190187

191188
if (! $hasData) {

src/Model/PdfDocument.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Vianetz\Pdf\Model;
4+
5+
final class PdfDocument implements PdfDocumentInterface
6+
{
7+
/** @var string */
8+
private $pdfFile;
9+
10+
public function getPdfFile()
11+
{
12+
return $this->pdfFile;
13+
}
14+
15+
public function setPdfFile($pdfFile)
16+
{
17+
$this->pdfFile = $pdfFile;
18+
}
19+
}

src/Model/PdfDocumentInterface.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Model;
19+
20+
interface PdfDocumentInterface
21+
{
22+
/**
23+
* @return string
24+
*/
25+
public function getPdfFile();
26+
27+
/**
28+
* @param string $pdfFile
29+
* @return void
30+
*/
31+
public function setPdfFile($pdfFile);
32+
}

0 commit comments

Comments
 (0)