Skip to content

Commit 3762623

Browse files
authored
Merge pull request #260 from MaximilianKresse/feature-fpdf
Added fpdf writer
2 parents 1616778 + 2df9482 commit 3762623

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
"myclabs/php-enum": "^1.5"
2222
},
2323
"require-dev": {
24-
"endroid/quality": "dev-master"
24+
"endroid/quality": "dev-master",
25+
"setasign/fpdf": "^1.8"
2526
},
2627
"suggest": {
2728
"roave/security-advisories": "Avoids installation of package versions with vulnerabilities",
28-
"symfony/security-checker": "Checks your composer.lock for vulnerabilities"
29+
"symfony/security-checker": "Checks your composer.lock for vulnerabilities",
30+
"setasign/fpdf": "Required to use the FPDF writer."
2931
},
3032
"autoload": {
3133
"psr-4": {

src/Writer/FpdfWriter.php

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* (c) Jeroen van den Enden <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Endroid\QrCode\Writer;
13+
14+
use Endroid\QrCode\Exception\GenerateImageException;
15+
use Endroid\QrCode\Exception\ValidationException;
16+
use Endroid\QrCode\QrCodeInterface;
17+
18+
class FpdfWriter extends AbstractWriter
19+
{
20+
/**
21+
* Defines as which unit the size is handled. Default is: "mm"
22+
*
23+
* Allowed values: 'mm', 'pt', 'cm', 'in'
24+
*/
25+
public const WRITER_OPTION_MEASURE_UNIT = 'fpdf_measure_unit';
26+
27+
public function __construct()
28+
{
29+
if(!\class_exists(\FPDF::class)){
30+
throw new \BadMethodCallException(
31+
'The Fpdf writer requires FPDF as dependency but the class "\\FPDF" couldn\'t be found.'
32+
);
33+
}
34+
}
35+
36+
public function writeString(QrCodeInterface $qrCode): string
37+
{
38+
if ($qrCode->getValidateResult()) {
39+
throw new ValidationException(
40+
'Built-in validation reader can not check fpdf qr codes: please disable via setValidateResult(false)'
41+
);
42+
}
43+
$foregroundColor = $qrCode->getForegroundColor();
44+
if ($foregroundColor['a'] !== 0) {
45+
throw new \InvalidArgumentException(
46+
'The foreground color has an alpha channel, but the fpdf qr writer doesn\'t support alpha channels.'
47+
);
48+
}
49+
$backgroundColor = $qrCode->getBackgroundColor();
50+
if ($backgroundColor['a'] !== 0) {
51+
throw new \InvalidArgumentException(
52+
'The foreground color has an alpha channel, but the fpdf qr writer doesn\'t support alpha channels.'
53+
);
54+
}
55+
56+
$label = $qrCode->getLabel();
57+
if (null !== $label) {
58+
throw new \InvalidArgumentException('The fpdf qr writer doesn\'t support a label.');
59+
}
60+
61+
$data = $qrCode->getData();
62+
$options = $qrCode->getWriterOptions();
63+
64+
$fpdf = new \FPDF(
65+
'P',
66+
$options[self::WRITER_OPTION_MEASURE_UNIT] ?? 'mm',
67+
[$data['outer_width'], $data['outer_height']]
68+
);
69+
$fpdf->AddPage();
70+
71+
$fpdf->SetFillColor($backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b']);
72+
$fpdf->Rect(0, 0, $data['outer_width'], $data['outer_height'], 'F');
73+
74+
$fpdf->SetFillColor($foregroundColor['r'], $foregroundColor['g'], $foregroundColor['b']);
75+
foreach ($data['matrix'] as $row => $values) {
76+
foreach ($values as $column => $value) {
77+
if (1 === $value) {
78+
$fpdf->Rect(
79+
$data['margin_left'] + ($column * $data['block_size']),
80+
$data['margin_left'] + ($row * $data['block_size']),
81+
$data['block_size'],
82+
$data['block_size'],
83+
'F'
84+
);
85+
}
86+
}
87+
}
88+
89+
$logoPath = $qrCode->getLogoPath();
90+
if (null !== $logoPath) {
91+
$this->addLogo(
92+
$fpdf,
93+
$logoPath,
94+
$qrCode->getLogoWidth(),
95+
$qrCode->getLogoHeight(),
96+
$data['outer_width'],
97+
$data['outer_height']
98+
);
99+
}
100+
101+
return $fpdf->Output('S');
102+
}
103+
104+
protected function addLogo(
105+
\FPDF $fpdf,
106+
string $logoPath,
107+
?int $logoWidth,
108+
?int $logoHeight,
109+
int $imageWidth,
110+
int $imageHeight
111+
) {
112+
if (null === $logoHeight || null === $logoWidth) {
113+
[$logoSourceWidth, $logoSourceHeight] = \getimagesize($logoPath);
114+
115+
if (null === $logoWidth) {
116+
$logoWidth = (int) $logoSourceWidth;
117+
}
118+
119+
if (null === $logoHeight) {
120+
$aspectRatio = $logoWidth / $logoSourceWidth;
121+
$logoHeight = (int) ($logoSourceHeight * $aspectRatio);
122+
}
123+
}
124+
125+
$logoX = $imageWidth / 2 - (int) $logoWidth / 2;
126+
$logoY = $imageHeight / 2 - (int) $logoHeight / 2;
127+
128+
$fpdf->Image($logoPath, $logoX, $logoY, $logoWidth, $logoHeight);
129+
}
130+
131+
public static function getContentType(): string
132+
{
133+
return 'application/pdf';
134+
}
135+
136+
public static function getSupportedExtensions(): array
137+
{
138+
return ['pdf'];
139+
}
140+
141+
public function getName(): string
142+
{
143+
return 'fpdf';
144+
}
145+
}

src/WriterRegistry.php

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Endroid\QrCode\Writer\BinaryWriter;
1616
use Endroid\QrCode\Writer\DebugWriter;
1717
use Endroid\QrCode\Writer\EpsWriter;
18+
use Endroid\QrCode\Writer\FpdfWriter;
1819
use Endroid\QrCode\Writer\PngWriter;
1920
use Endroid\QrCode\Writer\SvgWriter;
2021
use Endroid\QrCode\Writer\WriterInterface;
@@ -39,6 +40,7 @@ public function loadDefaultWriters(): void
3940
new EpsWriter(),
4041
new PngWriter(),
4142
new SvgWriter(),
43+
new FpdfWriter()
4244
]);
4345

4446
$this->setDefaultWriter('png');

0 commit comments

Comments
 (0)