Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to change FONT for PDF outputs #136

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions src/PaymentPart/Output/FpdfOutput/FpdfOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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)));

Expand All @@ -120,15 +123,15 @@ 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);
}

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)));

Expand Down Expand Up @@ -187,21 +190,21 @@ 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);
$this->setContentElement($furtherInformationElement, true);
}
}

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);
}
Expand All @@ -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)
));
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
}
38 changes: 25 additions & 13 deletions src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,18 +50,21 @@ 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);
}

Expand Down Expand Up @@ -107,7 +109,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
Expand All @@ -116,7 +118,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);
Expand All @@ -129,7 +131,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);
Expand All @@ -141,7 +143,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);
Expand Down Expand Up @@ -207,7 +209,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);
Expand All @@ -221,7 +223,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);
Expand All @@ -246,7 +248,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
);
Expand All @@ -261,7 +263,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
);
Expand Down Expand Up @@ -307,12 +309,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(
Expand All @@ -337,6 +339,16 @@ private function printMultiCell(

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;
}
}