vendor/endroid/qr-code/src/Writer/PdfWriter.php line 22

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