| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Endroid\QrCode\Writer; |
| 6 |
|
| 7 |
use Endroid\QrCode\Bacon\MatrixFactory; |
| 8 |
use Endroid\QrCode\Label\LabelInterface; |
| 9 |
use Endroid\QrCode\Logo\LogoInterface; |
| 10 |
use Endroid\QrCode\QrCodeInterface; |
| 11 |
use Endroid\QrCode\Writer\Result\PdfResult; |
| 12 |
use Endroid\QrCode\Writer\Result\ResultInterface; |
| 13 |
|
| 14 |
final class PdfWriter implements WriterInterface |
| 15 |
{ |
| 16 |
public const WRITER_OPTION_UNIT = 'unit'; |
| 17 |
public const WRITER_OPTION_PDF = 'fpdf'; |
| 18 |
public const WRITER_OPTION_X = 'x'; |
| 19 |
public const WRITER_OPTION_Y = 'y'; |
| 20 |
public const WRITER_OPTION_LINK = 'link'; |
| 21 |
|
| 22 |
public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface |
| 23 |
{ |
| 24 |
$matrixFactory = new MatrixFactory(); |
| 25 |
$matrix = $matrixFactory->create($qrCode); |
| 26 |
|
| 27 |
$unit = 'mm'; |
| 28 |
if (isset($options[self::WRITER_OPTION_UNIT])) { |
| 29 |
$unit = $options[self::WRITER_OPTION_UNIT]; |
| 30 |
} |
| 31 |
|
| 32 |
$allowedUnits = ['mm', 'pt', 'cm', 'in']; |
| 33 |
if (!in_array($unit, $allowedUnits)) { |
| 34 |
throw new \Exception(sprintf('PDF Measure unit should be one of [%s]', implode(', ', $allowedUnits))); |
| 35 |
} |
| 36 |
|
| 37 |
$labelSpace = 0; |
| 38 |
if ($label instanceof LabelInterface) { |
| 39 |
$labelSpace = 30; |
| 40 |
} |
| 41 |
|
| 42 |
if (!class_exists(\FPDF::class)) { |
| 43 |
throw new \Exception('Unable to find FPDF: check your installation'); |
| 44 |
} |
| 45 |
|
| 46 |
$foregroundColor = $qrCode->getForegroundColor(); |
| 47 |
if ($foregroundColor->getAlpha() > 0) { |
| 48 |
throw new \Exception('PDF Writer does not support alpha channels'); |
| 49 |
} |
| 50 |
$backgroundColor = $qrCode->getBackgroundColor(); |
| 51 |
if ($backgroundColor->getAlpha() > 0) { |
| 52 |
throw new \Exception('PDF Writer does not support alpha channels'); |
| 53 |
} |
| 54 |
|
| 55 |
if (isset($options[self::WRITER_OPTION_PDF])) { |
| 56 |
$fpdf = $options[self::WRITER_OPTION_PDF]; |
| 57 |
if (!$fpdf instanceof \FPDF) { |
| 58 |
throw new \Exception('pdf option must be an instance of FPDF'); |
| 59 |
} |
| 60 |
} else { |
| 61 |
// @todo Check how to add label height later |
| 62 |
$fpdf = new \FPDF('P', $unit, [$matrix->getOuterSize(), $matrix->getOuterSize() + $labelSpace]); |
| 63 |
$fpdf->AddPage(); |
| 64 |
} |
| 65 |
|
| 66 |
$x = 0; |
| 67 |
if (isset($options[self::WRITER_OPTION_X])) { |
| 68 |
$x = $options[self::WRITER_OPTION_X]; |
| 69 |
} |
| 70 |
$y = 0; |
| 71 |
if (isset($options[self::WRITER_OPTION_Y])) { |
| 72 |
$y = $options[self::WRITER_OPTION_Y]; |
| 73 |
} |
| 74 |
|
| 75 |
$fpdf->SetFillColor($backgroundColor->getRed(), $backgroundColor->getGreen(), $backgroundColor->getBlue()); |
| 76 |
$fpdf->Rect($x, $y, $matrix->getOuterSize(), $matrix->getOuterSize(), 'F'); |
| 77 |
$fpdf->SetFillColor($foregroundColor->getRed(), $foregroundColor->getGreen(), $foregroundColor->getBlue()); |
| 78 |
|
| 79 |
for ($rowIndex = 0; $rowIndex < $matrix->getBlockCount(); ++$rowIndex) { |
| 80 |
for ($columnIndex = 0; $columnIndex < $matrix->getBlockCount(); ++$columnIndex) { |
| 81 |
if (1 === $matrix->getBlockValue($rowIndex, $columnIndex)) { |
| 82 |
$fpdf->Rect( |
| 83 |
$x + $matrix->getMarginLeft() + ($columnIndex * $matrix->getBlockSize()), |
| 84 |
$y + $matrix->getMarginLeft() + ($rowIndex * $matrix->getBlockSize()), |
| 85 |
$matrix->getBlockSize(), |
| 86 |
$matrix->getBlockSize(), |
| 87 |
'F' |
| 88 |
); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if ($logo instanceof LogoInterface) { |
| 94 |
$this->addLogo($logo, $fpdf, $x, $y, $matrix->getOuterSize()); |
| 95 |
} |
| 96 |
|
| 97 |
if ($label instanceof LabelInterface) { |
| 98 |
$fpdf->SetXY($x, $y + $matrix->getOuterSize() + $labelSpace - 25); |
| 99 |
$fpdf->SetFont('Helvetica', '', $label->getFont()->getSize()); |
| 100 |
$fpdf->Cell($matrix->getOuterSize(), 0, $label->getText(), 0, 0, 'C'); |
| 101 |
} |
| 102 |
|
| 103 |
if (isset($options[self::WRITER_OPTION_LINK])) { |
| 104 |
$link = $options[self::WRITER_OPTION_LINK]; |
| 105 |
$fpdf->Link($x, $y, $x + $matrix->getOuterSize(), $y + $matrix->getOuterSize(), $link); |
| 106 |
} |
| 107 |
|
| 108 |
return new PdfResult($matrix, $fpdf); |
| 109 |
} |
| 110 |
|
| 111 |
private function addLogo(LogoInterface $logo, \FPDF $fpdf, float $x, float $y, float $size): void |
| 112 |
{ |
| 113 |
$logoPath = $logo->getPath(); |
| 114 |
$logoHeight = $logo->getResizeToHeight(); |
| 115 |
$logoWidth = $logo->getResizeToWidth(); |
| 116 |
|
| 117 |
if (null === $logoHeight || null === $logoWidth) { |
| 118 |
$imageSize = \getimagesize($logoPath); |
| 119 |
if (!$imageSize) { |
| 120 |
throw new \Exception(sprintf('Unable to read image size for logo "%s"', $logoPath)); |
| 121 |
} |
| 122 |
[$logoSourceWidth, $logoSourceHeight] = $imageSize; |
| 123 |
|
| 124 |
if (null === $logoWidth) { |
| 125 |
$logoWidth = (int) $logoSourceWidth; |
| 126 |
} |
| 127 |
|
| 128 |
if (null === $logoHeight) { |
| 129 |
$aspectRatio = $logoWidth / $logoSourceWidth; |
| 130 |
$logoHeight = (int) ($logoSourceHeight * $aspectRatio); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
$logoX = $x + $size / 2 - $logoWidth / 2; |
| 135 |
$logoY = $y + $size / 2 - $logoHeight / 2; |
| 136 |
|
| 137 |
$fpdf->Image($logoPath, $logoX, $logoY, $logoWidth, $logoHeight); |
| 138 |
} |
| 139 |
} |