Skip to content

Commit

Permalink
Merge pull request #260 from kohlerdominik/pdf-unsupported-characters
Browse files Browse the repository at this point in the history
Modernize encoding conversion for FPDF
  • Loading branch information
sprain authored Sep 5, 2024
2 parents 5490e91 + 1c3f909 commit 3805597
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"php": "~8.0.0|~8.1.0|~8.2.0|~8.3.0",
"ext-dom": "*",
"ext-bcmath": "*",
"ext-iconv": "*",
"symfony/validator": "^4.4|^5.0|^6.0|^7.0",
"symfony/intl": "^4.4|^5.0|^6.0|^7.0",
"kmukku/php-iso11649": "^1.5",
"endroid/qr-code": "^4.4.4|^5.0",
"symfony/polyfill-intl-icu": "^1.23"
"symfony/polyfill-intl-icu": "^1.23",
"symfony/polyfill-mbstring": "^1.30"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down
21 changes: 10 additions & 11 deletions src/PaymentPart/Output/FpdfOutput/FpdfOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private function addInformationContentReceipt(): void
// Title
$this->fpdf->SetFont(self::FONT, 'B', self::FONT_SIZE_MAIN_TITLE);
$this->SetXY(self::LEFT_PART_X, self::TITLE_Y);
$this->fpdf->MultiCell(0, 7, $this->toUtf8(Translation::get('receipt', $this->language)));
$this->fpdf->MultiCell(0, 7, $this->convertEncoding(Translation::get('receipt', $this->language)));

// Elements
$this->setY(204);
Expand All @@ -149,15 +149,15 @@ private function addInformationContentReceipt(): void
// Acceptance section
$this->fpdf->SetFont(self::FONT, 'B', self::FONT_SIZE_TITLE_RECEIPT);
$this->SetXY(self::LEFT_PART_X, 274.3);
$this->fpdf->Cell(54, 0, $this->toUtf8(Translation::get('acceptancePoint', $this->language)), self::BORDER, '', self::ALIGN_RIGHT);
$this->fpdf->Cell(54, 0, $this->convertEncoding(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->SetXY(self::RIGHT_PART_X, 195.2);
$this->fpdf->MultiCell(48, 7, $this->toUtf8(Translation::get('paymentPart', $this->language)));
$this->fpdf->MultiCell(48, 7, $this->convertEncoding(Translation::get('paymentPart', $this->language)));

// Elements
$this->setY(197.3);
Expand Down Expand Up @@ -229,7 +229,7 @@ private function addSeparatorContentIfNotPrintable(): void
$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->setY(189.6);
$this->fpdf->MultiCell(0, 0, $this->toUtf8(Translation::get('separate', $this->language)), self::BORDER, self::ALIGN_CENTER);
$this->fpdf->MultiCell(0, 0, $this->convertEncoding(Translation::get('separate', $this->language)), self::BORDER, self::ALIGN_CENTER);
}
}

Expand Down Expand Up @@ -258,9 +258,7 @@ private function setTitleElement(Title $element, bool $isReceiptPart): void
$this->fpdf->MultiCell(
0,
2.8,
iconv(
'UTF-8',
'windows-1252',
$this->convertEncoding(
Translation::get(str_replace('text.', '', $element->getTitle()), $this->language)
)
);
Expand All @@ -273,7 +271,7 @@ private function setTextElement(Text $element, bool $isReceiptPart): void
$this->fpdf->MultiCell(
$isReceiptPart ? 54 : 0,
$isReceiptPart ? 3.3 : 4,
str_replace('text.', '', $this->toUtf8($element->getText())),
str_replace('text.', '', $this->convertEncoding($element->getText())),
self::BORDER,
self::ALIGN_LEFT
);
Expand All @@ -286,7 +284,7 @@ private function setFurtherInformationElement(FurtherInformation $element): void
$this->fpdf->MultiCell(
0,
4,
$this->toUtf8($element->getText()),
$this->convertEncoding($element->getText()),
self::BORDER,
self::ALIGN_LEFT
);
Expand Down Expand Up @@ -336,8 +334,9 @@ private function SetXY(float $x, float $y): void
$this->fpdf->SetXY($x + $this->offsetX, $y + $this->offsetY);
}

private function toUtf8(string $text): string
private function convertEncoding(string $text): string
{
return iconv('UTF-8', 'windows-1252', $text) ?: '';
// FPDF does not support unicode.
return mb_convert_encoding($text, 'CP1252', 'UTF-8');
}
}
7 changes: 1 addition & 6 deletions src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private function setFurtherInformationElement(FurtherInformation $element): void
{
$this->tcPdf->SetFont(self::FONT, '', self::FONT_SIZE_FURTHER_INFORMATION);
$this->printMultiCell(
$this->toUtf8($element->getText()),
$element->getText(),
0,
0,
self::BORDER
Expand Down Expand Up @@ -352,9 +352,4 @@ 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);
}

private function toUtf8(string $text): string
{
return iconv('UTF-8', 'windows-1252', $text) ?: '';
}
}

0 comments on commit 3805597

Please sign in to comment.