From e7595c4325a7f59d130a497cc61b39713a68d349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Pe=C5=A1ek?= Date: Tue, 20 Jul 2021 07:40:25 +0200 Subject: [PATCH 1/2] #131 Added possibility to change FONT for PDF outputs --- .../Output/FpdfOutput/FpdfOutput.php | 35 +++++++++----- .../Output/TcPdfOutput/TcPdfOutput.php | 47 ++++++++++++------- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/src/PaymentPart/Output/FpdfOutput/FpdfOutput.php b/src/PaymentPart/Output/FpdfOutput/FpdfOutput.php index 38368561..7b288d21 100644 --- a/src/PaymentPart/Output/FpdfOutput/FpdfOutput.php +++ b/src/PaymentPart/Output/FpdfOutput/FpdfOutput.php @@ -22,7 +22,6 @@ final class FpdfOutput extends AbstractOutput implements OutputInterface private const ALIGN_LEFT = 'L'; private const ALIGN_RIGHT = 'R'; private const ALIGN_CENTER = 'C'; - private const FONT = 'Helvetica'; // Location private const CURRENCY_AMOUNT_Y = 259.3; @@ -49,18 +48,22 @@ final class FpdfOutput extends AbstractOutput implements OutputInterface private Fpdf $fpdf; private float $offsetX; private float $offsetY; + private string $font; public function __construct( QrBill $qrBill, string $language, Fpdf $fpdf, float $offsetX = 0, - float $offsetY = 0 - ) { + float $offsetY = 0, + string $font = 'Helvetica' + ) + { parent::__construct($qrBill, $language); $this->fpdf = $fpdf; $this->offsetX = $offsetX; $this->offsetY = $offsetY; + $this->font = $font; $this->setQrCodeImageFormat(QrCode::FILE_FORMAT_PNG); } @@ -108,7 +111,7 @@ private function addSwissQrCodeImage(): void private function addInformationContentReceipt(): void { // Title - $this->fpdf->SetFont(self::FONT, 'B', self::FONT_SIZE_MAIN_TITLE); + $this->fpdf->SetFont($this->font, 'B', self::FONT_SIZE_MAIN_TITLE); $this->SetXY(self::LEFT_PART_X, self::TITLE_Y); $this->fpdf->MultiCell(0, 7, utf8_decode(Translation::get('receipt', $this->language))); @@ -120,7 +123,7 @@ private function addInformationContentReceipt(): void } // Acceptance section - $this->fpdf->SetFont(self::FONT, 'B', self::FONT_SIZE_TITLE_RECEIPT); + $this->fpdf->SetFont($this->font, 'B', self::FONT_SIZE_TITLE_RECEIPT); $this->SetXY(self::LEFT_PART_X, 274.3); $this->fpdf->Cell(54, 0, utf8_decode(Translation::get('acceptancePoint', $this->language)), self::BORDER, '', self::ALIGN_RIGHT); } @@ -128,7 +131,7 @@ private function addInformationContentReceipt(): void private function addInformationContent(): void { // Title - $this->fpdf->SetFont(self::FONT, 'B', self::FONT_SIZE_MAIN_TITLE); + $this->fpdf->SetFont($this->font, 'B', self::FONT_SIZE_MAIN_TITLE); $this->SetXY(self::RIGHT_PART_X, 195.2); $this->fpdf->MultiCell(48, 7, utf8_decode(Translation::get('paymentPart', $this->language))); @@ -187,7 +190,7 @@ private function addAmountContent(): void private function addFurtherInformationContent(): void { $this->SetXY(self::RIGHT_PART_X, 286); - $this->fpdf->SetFont(self::FONT, '', self::FONT_SIZE_FURTHER_INFORMATION); + $this->fpdf->SetFont($this->font, '', self::FONT_SIZE_FURTHER_INFORMATION); foreach ($this->getFurtherInformationElements() as $furtherInformationElement) { $this->SetX(self::RIGHT_PART_X); @@ -195,13 +198,13 @@ private function addFurtherInformationContent(): void } } - private function addSeparatorContentIfNotPrintable() + private function addSeparatorContentIfNotPrintable(): void { if (!$this->isPrintable()) { $this->fpdf->SetLineWidth(0.1); $this->fpdf->Line(2 + $this->offsetX, 193 + $this->offsetY, 208 + $this->offsetX, 193 + $this->offsetY); $this->fpdf->Line(62 + $this->offsetX, 193 + $this->offsetY, 62 + $this->offsetX, 296 + $this->offsetY); - $this->fpdf->SetFont(self::FONT, '', self::FONT_SIZE_FURTHER_INFORMATION); + $this->fpdf->SetFont($this->font, '', self::FONT_SIZE_FURTHER_INFORMATION); $this->SetY(189.6); $this->fpdf->MultiCell(0, 0, utf8_decode(Translation::get('separate', $this->language)), self::BORDER, self::ALIGN_CENTER); } @@ -224,7 +227,7 @@ private function setContentElement(OutputElementInterface $element, bool $isRece private function setTitleElement(Title $element, bool $isReceiptPart): void { - $this->fpdf->SetFont(self::FONT, 'B', $isReceiptPart ? self::FONT_SIZE_TITLE_RECEIPT : self::FONT_SIZE_TITLE_PAYMENT_PART); + $this->fpdf->SetFont($this->font, 'B', $isReceiptPart ? self::FONT_SIZE_TITLE_RECEIPT : self::FONT_SIZE_TITLE_PAYMENT_PART); $this->fpdf->MultiCell(0, 2.8, utf8_decode( Translation::get(str_replace("text.", "", $element->getTitle()), $this->language) )); @@ -233,7 +236,7 @@ private function setTitleElement(Title $element, bool $isReceiptPart): void private function setTextElement(Text $element, bool $isReceiptPart): void { - $this->fpdf->SetFont(self::FONT, '', $isReceiptPart ? self::FONT_SIZE_RECEIPT : self::FONT_SIZE_PAYMENT_PART); + $this->fpdf->SetFont($this->font, '', $isReceiptPart ? self::FONT_SIZE_RECEIPT : self::FONT_SIZE_PAYMENT_PART); $this->fpdf->MultiCell( $isReceiptPart ? 54 : 0, $isReceiptPart ? 3.3 : 4, @@ -287,4 +290,14 @@ private function SetXY(float $x, float $y): void { $this->fpdf->SetXY($x + $this->offsetX, $y + $this->offsetY); } + + public function getFont(): string + { + return $this->font; + } + + public function setFont(string $font): void + { + $this->font = $font; + } } diff --git a/src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php b/src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php index 3d6646a1..1bec4d1d 100644 --- a/src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php +++ b/src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php @@ -21,7 +21,6 @@ final class TcPdfOutput extends AbstractOutput implements OutputInterface private const ALIGN_LEFT = 'L'; private const ALIGN_RIGHT = 'R'; private const ALIGN_CENTER = 'C'; - private const FONT = 'Helvetica'; // Ratio private const LEFT_CELL_HEIGHT_RATIO_COMMON = 1.2; @@ -51,18 +50,22 @@ final class TcPdfOutput extends AbstractOutput implements OutputInterface private TCPDF $tcPdf; private float $offsetX; private float $offsetY; + private string $font; public function __construct( QrBill $qrBill, string $language, TCPDF $tcPdf, float $offsetX = 0, - float $offsetY = 0 - ) { + float $offsetY = 0, + string $font = 'Helvetica' + ) + { parent::__construct($qrBill, $language); $this->tcPdf = $tcPdf; $this->offsetX = $offsetX; $this->offsetY = $offsetY; + $this->font = $font; $this->setQrCodeImageFormat(QrCode::FILE_FORMAT_SVG); } @@ -107,7 +110,7 @@ private function addSwissQrCodeImage(): void $qrCode->setWriterByExtension($format); $img = base64_decode(preg_replace('#^data:image/[^;]+;base64,#', '', $qrCode->writeDataUri())); - $this->tcPdf->$method("@".$img, $xPosQrCode, $yPosQrCode, 46, 46); + $this->tcPdf->$method("@" . $img, $xPosQrCode, $yPosQrCode, 46, 46); } private function addInformationContentReceipt(): void @@ -116,7 +119,7 @@ private function addInformationContentReceipt(): void $this->tcPdf->setCellHeightRatio(self::LEFT_CELL_HEIGHT_RATIO_COMMON); // Title - $this->tcPdf->SetFont(self::FONT, 'B', self::FONT_SIZE_MAIN_TITLE); + $this->tcPdf->SetFont($this->font, 'B', self::FONT_SIZE_MAIN_TITLE); $this->SetY(self::TITLE_Y); $this->SetX($x); $this->printCell(Translation::get('receipt', $this->language), 0, 7); @@ -129,7 +132,7 @@ private function addInformationContentReceipt(): void } // Acceptance section - $this->tcPdf->SetFont(self::FONT, 'B', 6); + $this->tcPdf->SetFont($this->font, 'B', 6); $this->SetY(273); $this->SetX($x); $this->printCell(Translation::get('acceptancePoint', $this->language), 54, 0, self::ALIGN_BELOW, self::ALIGN_RIGHT); @@ -141,7 +144,7 @@ private function addInformationContent(): void $this->tcPdf->setCellHeightRatio(self::RIGHT_CELL_HEIGHT_RATIO_COMMON); // Title - $this->tcPdf->SetFont(self::FONT, 'B', self::FONT_SIZE_MAIN_TITLE); + $this->tcPdf->SetFont($this->font, 'B', self::FONT_SIZE_MAIN_TITLE); $this->SetY(self::TITLE_Y); $this->SetX(self::RIGHT_PART_X); $this->printCell(Translation::get('paymentPart', $this->language), 48, 7); @@ -207,7 +210,7 @@ private function addFurtherInformationContent(): void $x = self::RIGHT_PART_X; $this->tcPdf->setCellHeightRatio(self::RIGHT_CELL_HEIGHT_RATIO_COMMON); $this->SetY(286); - $this->tcPdf->SetFont(self::FONT, '', self::FONT_SIZE_FURTHER_INFORMATION); + $this->tcPdf->SetFont($this->font, '', self::FONT_SIZE_FURTHER_INFORMATION); foreach ($this->getFurtherInformationElements() as $furtherInformationElement) { $this->SetX($x); @@ -221,7 +224,7 @@ private function addSeparatorContentIfNotPrintable(): void $this->tcPdf->SetLineStyle(['width' => 0.1, 'color' => [0, 0, 0]]); $this->printLine(2, 193, 208, 193); $this->printLine(62, 193, 62, 296); - $this->tcPdf->SetFont(self::FONT, '', self::FONT_SIZE_FURTHER_INFORMATION); + $this->tcPdf->SetFont($this->font, '', self::FONT_SIZE_FURTHER_INFORMATION); $this->SetY(188); $this->SetX(5); $this->printCell(Translation::get('separate', $this->language), 200, 0, 0, self::ALIGN_CENTER); @@ -246,7 +249,7 @@ private function setContentElement(OutputElementInterface $element, bool $isRece private function setTitleElement(Title $element, bool $isReceiptPart): void { $this->tcPdf->SetFont( - self::FONT, + $this->font, 'B', $isReceiptPart ? self::FONT_SIZE_TITLE_RECEIPT : self::FONT_SIZE_TITLE_PAYMENT_PART ); @@ -261,7 +264,7 @@ private function setTitleElement(Title $element, bool $isReceiptPart): void private function setTextElement(Text $element, bool $isReceiptPart): void { $this->tcPdf->SetFont( - self::FONT, + $this->font, '', $isReceiptPart ? self::FONT_SIZE_RECEIPT : self::FONT_SIZE_PAYMENT_PART ); @@ -307,12 +310,12 @@ private function setPlaceholderElement(Placeholder $element): void private function setX(int $x): void { - $this->tcPdf->SetX($x+$this->offsetX); + $this->tcPdf->SetX($x + $this->offsetX); } private function setY(int $y): void { - $this->tcPdf->SetY($y+$this->offsetY); + $this->tcPdf->SetY($y + $this->offsetY); } private function printCell( @@ -321,7 +324,8 @@ private function printCell( int $h = 0, int $nextLineAlign = 0, string $textAlign = self::ALIGN_LEFT - ): void { + ): void + { $this->tcPdf->Cell($w, $h, $text, self::BORDER, $nextLineAlign, $textAlign); } @@ -331,12 +335,23 @@ private function printMultiCell( int $h = 0, int $nextLineAlign = 0, string $textAlign = self::ALIGN_LEFT - ): void { + ): void + { $this->tcPdf->MultiCell($w, $h, $text, self::BORDER, $textAlign, false, $nextLineAlign); } private function printLine(int $x1, int $y1, int $x2, int $y2): void { - $this->tcPdf->Line($x1+$this->offsetX, $y1+$this->offsetY, $x2+$this->offsetX, $y2+$this->offsetY); + $this->tcPdf->Line($x1 + $this->offsetX, $y1 + $this->offsetY, $x2 + $this->offsetX, $y2 + $this->offsetY); + } + + public function getFont(): string + { + return $this->font; + } + + public function setFont(string $font): void + { + $this->font = $font; } } From 747ed329a0a3bff36c8b58aa58725aaf42ed4e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Pe=C5=A1ek?= Date: Tue, 20 Jul 2021 10:47:00 +0200 Subject: [PATCH 2/2] #131 Meet coding standards --- src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php b/src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php index 1bec4d1d..4af76c72 100644 --- a/src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php +++ b/src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php @@ -59,8 +59,7 @@ public function __construct( float $offsetX = 0, float $offsetY = 0, string $font = 'Helvetica' - ) - { + ) { parent::__construct($qrBill, $language); $this->tcPdf = $tcPdf; $this->offsetX = $offsetX; @@ -324,8 +323,7 @@ private function printCell( int $h = 0, int $nextLineAlign = 0, string $textAlign = self::ALIGN_LEFT - ): void - { + ): void { $this->tcPdf->Cell($w, $h, $text, self::BORDER, $nextLineAlign, $textAlign); } @@ -335,8 +333,7 @@ private function printMultiCell( int $h = 0, int $nextLineAlign = 0, string $textAlign = self::ALIGN_LEFT - ): void - { + ): void { $this->tcPdf->MultiCell($w, $h, $text, self::BORDER, $textAlign, false, $nextLineAlign); }