Skip to content

Commit 70e4d60

Browse files
committed
small refactorings, made debug file appending, improved README
Signed-off-by: Christoph Massmann <[email protected]>
1 parent 6d97b76 commit 70e4d60

File tree

4 files changed

+50
-127
lines changed

4 files changed

+50
-127
lines changed

README.md

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
vianetz Pdf Library
2-
=====================
1+
# vianetz Pdf Library
32

4-
Description
5-
-----------
6-
This library offers an easy-to-use API for PDF generation and merging.
3+
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.
75

8-
Requirements
9-
------------
10-
- PHP >= 7
6+
## Usage
117

12-
Usage
13-
-----
14-
15-
#### Create PDF document from HTML contents
8+
### Create PDF document from HTML contents
169
```php
1710
// Create a new pdf instance.
1811
$pdf = Vianetz\Pdf\Model\PdfFactory::general()->create();
@@ -29,7 +22,7 @@ $pdf->addDocument($document);
2922
$pdf->saveToFile('test.pdf');
3023
```
3124

32-
#### Merge a PDF file and a PDF string into one PDF file
25+
### Merge a PDF file and a PDF string into one PDF file
3326
```php
3427
// Load some random PDF contents
3528
$pdfString = file_get_contents('test1.pdf');
@@ -46,30 +39,24 @@ $pdfMerge->mergePdfFile('test2.pdf');
4639
$pdfMerge->saveToFile('result.pdf');
4740
```
4841

49-
Frequently Asked Questions
50-
--------------------------
51-
Please find the Frequently Asked Questions [on our website](https://www.vianetz.com/en/faq).
42+
### Tips & Tricks
43+
44+
- The string literal `__PDF_TPC__` will be replaced with the total page count
45+
46+
## Frequently Asked Questions
47+
Please find the Frequently Asked Questions [on my website](https://www.vianetz.com/en/faq).
5248

53-
Support
54-
-------
49+
## Support
5550
If you have any issues or suggestions with this extension, please do not hesitate to
5651
[contact me](https://www.vianetz.com/en/contacts).
5752

58-
Credits
59-
-------
53+
## Credits
6054
Of course this extension would not have been possible without the great open source eco-system.
6155
Therewith credits go to:
6256
- [DomPDF](https://github.com/dompdf/dompdf)
6357
- [FPDI](https://github.com/Setasign/FPDI)
6458

65-
Developer
66-
---------
67-
Christoph Massmann
68-
[www.vianetz.com](https://www.vianetz.com)
69-
[@vianetz](https://twitter.com/vianetz)
70-
71-
Licence
72-
-------
59+
## License
7360
[GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.html)
7461
See also LICENSE file.
7562

@@ -78,8 +65,4 @@ repository](https://github.com/dompdf/dompdf).
7865
This extension uses the FPDI library. For license information please visit [the FPDI
7966
repository](https://github.com/Setasign/FPDI/blob/master/LICENSE.txt).
8067

81-
Copyright
82-
---------
83-
(c) since 2008 vianetz
84-
8568
This library uses Semantic Versioning - please find more information at [semver.org](http://semver.org).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
},
2727
"require": {
28-
"php": "^7|^8",
28+
"php": "^7.4|^8",
2929
"dompdf/dompdf": "^2.0",
3030
"setasign/fpdi": "^2.0",
3131
"tecnickcom/tcpdf": "^6.4"

src/Model/Config.php

Lines changed: 31 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
* Vianetz Pdf Options Model
46
*
@@ -24,157 +26,101 @@ final class Config
2426
public const PAPER_ORIENTATION_LANDSCAPE = 'landscape';
2527
public const PAPER_ORIENTATION_PORTRAIT = 'portrait';
2628

27-
/**
28-
* @var string
29-
* @see \Dompdf\Adapter\CPDF::$PAPER_SIZES
30-
*/
31-
private $pdfSize = 'a4';
32-
33-
/** @var string */
34-
private $pdfOrientation = self::PAPER_ORIENTATION_PORTRAIT;
35-
36-
/** @var string */
37-
private $pdfAuthor = '';
38-
39-
/** @var string */
40-
private $pdfTitle = '';
41-
42-
/** @var bool */
43-
private $isDebugMode = false;
44-
45-
/** @var string */
46-
private $tempDir;
47-
48-
/** @var string */
49-
private $chrootDir = '/';
29+
/** @see \Dompdf\Adapter\CPDF::$PAPER_SIZES */
30+
private string $pdfSize = 'a4';
31+
private string $pdfOrientation = self::PAPER_ORIENTATION_PORTRAIT;
32+
private string $pdfAuthor = '';
33+
private string $pdfTitle = '';
34+
private bool $isDebugMode = false;
35+
private string $tempDir;
36+
private string $chrootDir = '/';
5037

5138
public function __construct()
5239
{
5340
$this->tempDir = sys_get_temp_dir();
5441
}
5542

56-
/** @return string */
57-
public function getPdfSize()
43+
public function getPdfSize(): string
5844
{
5945
return $this->pdfSize;
6046
}
6147

62-
/**
63-
* @return string
64-
*/
65-
public function getPdfOrientation()
48+
public function getPdfOrientation(): string
6649
{
6750
return $this->pdfOrientation;
6851
}
6952

70-
/**
71-
* @return string
72-
*/
73-
public function getPdfAuthor()
53+
public function getPdfAuthor(): string
7454
{
7555
return $this->pdfAuthor;
7656
}
7757

78-
/**
79-
* @return string
80-
*/
81-
public function getPdfTitle()
58+
public function getPdfTitle(): string
8259
{
8360
return $this->pdfTitle;
8461
}
8562

86-
/**
87-
* @return boolean
88-
*/
89-
public function isDebugMode()
63+
public function isDebugMode(): bool
9064
{
9165
return $this->isDebugMode;
9266
}
9367

94-
/**
95-
* @return string
96-
*/
97-
public function getTempDir()
68+
public function getTempDir(): string
9869
{
9970
return $this->tempDir;
10071
}
10172

102-
/**
103-
* @return string
104-
*/
105-
public function getChrootDir()
73+
public function getChrootDir(): string
10674
{
10775
return $this->chrootDir;
10876
}
10977

110-
/**
111-
* @param string $pdfSize
112-
* @return Config
113-
*/
114-
public function setPdfSize($pdfSize)
78+
public function setPdfSize(string $pdfSize): self
11579
{
11680
$this->pdfSize = $pdfSize;
81+
11782
return $this;
11883
}
11984

120-
/**
121-
* @param string $pdfOrientation
122-
* @return Config
123-
*/
124-
public function setPdfOrientation($pdfOrientation)
85+
public function setPdfOrientation(string $pdfOrientation): self
12586
{
12687
$this->pdfOrientation = $pdfOrientation;
88+
12789
return $this;
12890
}
12991

130-
/**
131-
* @param string $pdfAuthor
132-
* @return Config
133-
*/
134-
public function setPdfAuthor($pdfAuthor)
92+
public function setPdfAuthor(string $pdfAuthor): self
13593
{
13694
$this->pdfAuthor = $pdfAuthor;
95+
13796
return $this;
13897
}
13998

140-
/**
141-
* @param string $pdfTitle
142-
* @return Config
143-
*/
144-
public function setPdfTitle($pdfTitle)
99+
public function setPdfTitle(string $pdfTitle): self
145100
{
146101
$this->pdfTitle = $pdfTitle;
102+
147103
return $this;
148104
}
149105

150-
/**
151-
* @param boolean $isDebugMode
152-
* @return Config
153-
*/
154-
public function setIsDebugMode($isDebugMode)
106+
public function setIsDebugMode(bool $isDebugMode): self
155107
{
156108
$this->isDebugMode = $isDebugMode;
109+
157110
return $this;
158111
}
159112

160-
/**
161-
* @param string $tempDir
162-
* @return Config
163-
*/
164-
public function setTempDir($tempDir)
113+
public function setTempDir(string $tempDir): self
165114
{
166115
$this->tempDir = $tempDir;
116+
167117
return $this;
168118
}
169119

170-
/**
171-
* @param string $chrootDir
172-
*
173-
* @return $this
174-
*/
175-
public function setChrootDir($chrootDir)
120+
public function setChrootDir(string $chrootDir): self
176121
{
177122
$this->chrootDir = $chrootDir;
123+
178124
return $this;
179125
}
180126
}

src/Model/Generator/AbstractGenerator.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,14 @@ protected function replaceSpecialChars($htmlContents)
8282
return $htmlContents;
8383
}
8484

85-
/**
86-
* Write the given string to debug file.
87-
*
88-
* @param string $fileContents
89-
*
90-
* @return boolean
91-
*/
92-
protected function writeDebugFile($fileContents)
85+
protected function writeDebugFile(string $fileContents): bool
9386
{
9487
if ($this->config->isDebugMode() === false) {
9588
return false;
9689
}
9790

9891
$debugFilename = $this->config->getTempDir() . DIRECTORY_SEPARATOR . self::DEBUG_FILE_NAME;
99-
return @file_put_contents($debugFilename, $fileContents) !== false;
92+
93+
return @file_put_contents($debugFilename, $fileContents, FILE_APPEND) !== false;
10094
}
10195
}

0 commit comments

Comments
 (0)