Skip to content

Commit

Permalink
Added options to output qr-code to an existing document.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Aug 20, 2021
1 parent 53bfce7 commit 7e659e6
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/Writer/PdfWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
final class PdfWriter implements WriterInterface
{
public const WRITER_OPTION_UNIT = 'unit';
public const WRITER_OPTION_PDF = 'fpdf';
public const WRITER_OPTION_X = 'x';
public const WRITER_OPTION_Y = 'y';

public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface
{
Expand Down Expand Up @@ -48,20 +51,36 @@ public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, Label
throw new \Exception('PDF Writer does not support alpha channels');
}

// @todo Check how to add label height later
$fpdf = new \FPDF('P', $unit, [$matrix->getOuterSize(), $matrix->getOuterSize() + $labelSpace]);
$fpdf->AddPage();
if (isset($options[self::WRITER_OPTION_PDF])) {
$fpdf = $options[self::WRITER_OPTION_PDF];
if (!$fpdf instanceof \FPDF) {
throw new \Exception('pdf option must be an instance of FPDF');
}
} else {
// @todo Check how to add label height later
$fpdf = new \FPDF('P', $unit, [$matrix->getOuterSize(), $matrix->getOuterSize() + $labelSpace]);
$fpdf->AddPage();
}

$x = 0;
if (isset($options[self::WRITER_OPTION_X])) {
$x = $options[self::WRITER_OPTION_X];
}
$y = 0;
if (isset($options[self::WRITER_OPTION_Y])) {
$y = $options[self::WRITER_OPTION_Y];
}

$fpdf->SetFillColor($backgroundColor->getRed(), $backgroundColor->getGreen(), $backgroundColor->getBlue());
$fpdf->Rect(0, 0, $matrix->getOuterSize(), $matrix->getOuterSize(), 'F');
$fpdf->Rect($x, $y, $matrix->getOuterSize(), $matrix->getOuterSize(), 'F');
$fpdf->SetFillColor($foregroundColor->getRed(), $foregroundColor->getGreen(), $foregroundColor->getBlue());

for ($rowIndex = 0; $rowIndex < $matrix->getBlockCount(); ++$rowIndex) {
for ($columnIndex = 0; $columnIndex < $matrix->getBlockCount(); ++$columnIndex) {
if (1 === $matrix->getBlockValue($rowIndex, $columnIndex)) {
$fpdf->Rect(
$matrix->getMarginLeft() + ($columnIndex * $matrix->getBlockSize()),
$matrix->getMarginLeft() + ($rowIndex * $matrix->getBlockSize()),
$x + $matrix->getMarginLeft() + ($columnIndex * $matrix->getBlockSize()),
$y + $matrix->getMarginLeft() + ($rowIndex * $matrix->getBlockSize()),
$matrix->getBlockSize(),
$matrix->getBlockSize(),
'F'
Expand All @@ -70,25 +89,21 @@ public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, Label
}
}

$result = new PdfResult($fpdf);

if ($logo instanceof LogoInterface) {
$result = $this->addLogo($logo, $result);
$this->addLogo($logo, $fpdf, $x, $y, $matrix->getOuterSize());
}

if ($label instanceof LabelInterface) {
$fpdf->setY($fpdf->GetPageHeight() - 25);
$fpdf->SetXY($x, $y + $matrix->getOuterSize() + $labelSpace - 25);
$fpdf->SetFont('Helvetica', null, $label->getFont()->getSize());
$fpdf->Cell(0, 0, $label->getText(), 0, 0, 'C');
$fpdf->Cell($matrix->getOuterSize(), 0, $label->getText(), 0, 0, 'C');
}

return $result;
return new PdfResult($fpdf);
}

private function addLogo(LogoInterface $logo, PdfResult $result): PdfResult
private function addLogo(LogoInterface $logo, \FPDF $fpdf, float $x, float $y, float $size): void
{
$fpdf = $result->getPdf();

$logoPath = $logo->getPath();
$logoHeight = $logo->getResizeToHeight();
$logoWidth = $logo->getResizeToWidth();
Expand All @@ -106,11 +121,9 @@ private function addLogo(LogoInterface $logo, PdfResult $result): PdfResult
}
}

$logoX = $fpdf->GetPageWidth() / 2 - (int) $logoWidth / 2;
$logoY = $fpdf->GetPageHeight() / 2 - (int) $logoHeight / 2;
$logoX = $x + $size / 2 - (int) $logoWidth / 2;
$logoY = $y + $size / 2 - (int) $logoHeight / 2;

$fpdf->Image($logoPath, $logoX, $logoY, $logoWidth, $logoHeight);

return $result;
}
}

0 comments on commit 7e659e6

Please sign in to comment.