Skip to content

Commit

Permalink
sprain#131 revert changes, fix text conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
KamilPesek committed Aug 20, 2021
1 parent 747ed32 commit 4064568
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 90 deletions.
65 changes: 33 additions & 32 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
{
"name": "sprain/swiss-qr-bill",
"description": "A PHP library to create Swiss QR bills",
"type": "library",
"license": "MIT",
"require": {
"php": "^7.4|^8.0",
"symfony/validator": "^3.4.47|^4.4|^5.0",
"symfony/intl": "^3.4.47|^4.4|^5.0",
"khanamiryan/qrcode-detector-decoder": "^1.0.3",
"kmukku/php-iso11649": "^1.5",
"endroid/qr-code": "^3.9.7"
},
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0",
"dg/bypass-finals": "^1.3",
"dms/phpunit-arraysubset-asserts": "^0.1|^0.2",
"fpdf/fpdf": "^1.82",
"friendsofphp/php-cs-fixer": "^2.19",
"phpstan/phpstan": "^0.12.53",
"symfony/css-selector": "^4.2",
"symfony/var-dumper": "^5.1",
"tecnickcom/tcpdf": "^6.3.2"
},
"suggest": {
"tecnickcom/tcpdf": "Needed to create pdfs with TcPdfOutput",
"fpdf/fpdf": "Needed to create pdfs with FpdfOutput"
},
"autoload": {
"psr-4": {
"Sprain\\SwissQrBill\\": "src",
"Sprain\\Tests\\SwissQrBill\\": "tests"
}
"name": "sprain/swiss-qr-bill",
"description": "A PHP library to create Swiss QR bills",
"type": "library",
"license": "MIT",
"require": {
"php": "^7.4|^8.0",
"symfony/validator": "^3.4.47|^4.4|^5.0",
"symfony/intl": "^3.4.47|^4.4|^5.0",
"khanamiryan/qrcode-detector-decoder": "^1.0.3",
"kmukku/php-iso11649": "^1.5",
"endroid/qr-code": "^3.9.7",
"ext-iconv": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0",
"dg/bypass-finals": "^1.3",
"dms/phpunit-arraysubset-asserts": "^0.1|^0.2",
"fpdf/fpdf": "^1.82",
"friendsofphp/php-cs-fixer": "^2.19",
"phpstan/phpstan": "^0.12.53",
"symfony/css-selector": "^4.2",
"symfony/var-dumper": "^5.1",
"tecnickcom/tcpdf": "^6.3.2"
},
"suggest": {
"tecnickcom/tcpdf": "Needed to create pdfs with TcPdfOutput",
"fpdf/fpdf": "Needed to create pdfs with FpdfOutput"
},
"autoload": {
"psr-4": {
"Sprain\\SwissQrBill\\": "src",
"Sprain\\Tests\\SwissQrBill\\": "tests"
}
}
}
51 changes: 22 additions & 29 deletions src/PaymentPart/Output/FpdfOutput/FpdfOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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 @@ -48,22 +49,18 @@ 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,
string $font = 'Helvetica'
)
{
Fpdf $fpdf,
float $offsetX = 0,
float $offsetY = 0
) {
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 @@ -111,9 +108,9 @@ private function addSwissQrCodeImage(): void
private function addInformationContentReceipt(): void
{
// Title
$this->fpdf->SetFont($this->font, 'B', self::FONT_SIZE_MAIN_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, utf8_decode(Translation::get('receipt', $this->language)));
$this->fpdf->MultiCell(0, 7, $this->convertText(Translation::get('receipt', $this->language)));

// Elements
$this->SetY(204);
Expand All @@ -123,17 +120,17 @@ private function addInformationContentReceipt(): void
}

// Acceptance section
$this->fpdf->SetFont($this->font, 'B', self::FONT_SIZE_TITLE_RECEIPT);
$this->fpdf->SetFont(self::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);
$this->fpdf->Cell(54, 0, $this->convertText(Translation::get('acceptancePoint', $this->language)), self::BORDER, '', self::ALIGN_RIGHT);
}

private function addInformationContent(): void
{
// Title
$this->fpdf->SetFont($this->font, 'B', self::FONT_SIZE_MAIN_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, utf8_decode(Translation::get('paymentPart', $this->language)));
$this->fpdf->MultiCell(48, 7, $this->convertText(Translation::get('paymentPart', $this->language)));

// Elements
$this->SetY(197.3);
Expand Down Expand Up @@ -190,23 +187,23 @@ private function addAmountContent(): void
private function addFurtherInformationContent(): void
{
$this->SetXY(self::RIGHT_PART_X, 286);
$this->fpdf->SetFont($this->font, '', self::FONT_SIZE_FURTHER_INFORMATION);
$this->fpdf->SetFont(self::FONT, '', self::FONT_SIZE_FURTHER_INFORMATION);

foreach ($this->getFurtherInformationElements() as $furtherInformationElement) {
$this->SetX(self::RIGHT_PART_X);
$this->setContentElement($furtherInformationElement, true);
}
}

private function addSeparatorContentIfNotPrintable(): void
private function addSeparatorContentIfNotPrintable()
{
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($this->font, '', self::FONT_SIZE_FURTHER_INFORMATION);
$this->fpdf->SetFont(self::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);
$this->fpdf->MultiCell(0, 0, $this->convertText(Translation::get('separate', $this->language)), self::BORDER, self::ALIGN_CENTER);
}
}

Expand All @@ -227,20 +224,20 @@ private function setContentElement(OutputElementInterface $element, bool $isRece

private function setTitleElement(Title $element, bool $isReceiptPart): void
{
$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(
$this->fpdf->SetFont(self::FONT, 'B', $isReceiptPart ? self::FONT_SIZE_TITLE_RECEIPT : self::FONT_SIZE_TITLE_PAYMENT_PART);
$this->fpdf->MultiCell(0, 2.8, $this->convertText(
Translation::get(str_replace("text.", "", $element->getTitle()), $this->language)
));
$this->fpdf->Ln($this->amountLS);
}

private function setTextElement(Text $element, bool $isReceiptPart): void
{
$this->fpdf->SetFont($this->font, '', $isReceiptPart ? self::FONT_SIZE_RECEIPT : self::FONT_SIZE_PAYMENT_PART);
$this->fpdf->SetFont(self::FONT, '', $isReceiptPart ? self::FONT_SIZE_RECEIPT : self::FONT_SIZE_PAYMENT_PART);
$this->fpdf->MultiCell(
$isReceiptPart ? 54 : 0,
$isReceiptPart ? 3.3 : 4,
str_replace("text.", "", utf8_decode($element->getText())),
str_replace("text.", "", $this->convertText($element->getText())),
self::BORDER,
self::ALIGN_LEFT
);
Expand Down Expand Up @@ -291,13 +288,9 @@ 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
private function convertText(string $text)
{
$this->font = $font;
$text = stripslashes($text);
return iconv('UTF-8', 'CP1250//TRANSLIT', $text);
}
}
46 changes: 17 additions & 29 deletions src/PaymentPart/Output/TcPdfOutput/TcPdfOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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 @@ -50,21 +51,18 @@ 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,
string $font = 'Helvetica'
TCPDF $tcPdf,
float $offsetX = 0,
float $offsetY = 0
) {
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 @@ -118,7 +116,7 @@ private function addInformationContentReceipt(): void
$this->tcPdf->setCellHeightRatio(self::LEFT_CELL_HEIGHT_RATIO_COMMON);

// Title
$this->tcPdf->SetFont($this->font, 'B', self::FONT_SIZE_MAIN_TITLE);
$this->tcPdf->SetFont(self::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 @@ -131,7 +129,7 @@ private function addInformationContentReceipt(): void
}

// Acceptance section
$this->tcPdf->SetFont($this->font, 'B', 6);
$this->tcPdf->SetFont(self::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 @@ -143,7 +141,7 @@ private function addInformationContent(): void
$this->tcPdf->setCellHeightRatio(self::RIGHT_CELL_HEIGHT_RATIO_COMMON);

// Title
$this->tcPdf->SetFont($this->font, 'B', self::FONT_SIZE_MAIN_TITLE);
$this->tcPdf->SetFont(self::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 @@ -209,7 +207,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($this->font, '', self::FONT_SIZE_FURTHER_INFORMATION);
$this->tcPdf->SetFont(self::FONT, '', self::FONT_SIZE_FURTHER_INFORMATION);

foreach ($this->getFurtherInformationElements() as $furtherInformationElement) {
$this->SetX($x);
Expand All @@ -223,7 +221,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($this->font, '', self::FONT_SIZE_FURTHER_INFORMATION);
$this->tcPdf->SetFont(self::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 @@ -248,7 +246,7 @@ private function setContentElement(OutputElementInterface $element, bool $isRece
private function setTitleElement(Title $element, bool $isReceiptPart): void
{
$this->tcPdf->SetFont(
$this->font,
self::FONT,
'B',
$isReceiptPart ? self::FONT_SIZE_TITLE_RECEIPT : self::FONT_SIZE_TITLE_PAYMENT_PART
);
Expand All @@ -263,7 +261,7 @@ private function setTitleElement(Title $element, bool $isReceiptPart): void
private function setTextElement(Text $element, bool $isReceiptPart): void
{
$this->tcPdf->SetFont(
$this->font,
self::FONT,
'',
$isReceiptPart ? self::FONT_SIZE_RECEIPT : self::FONT_SIZE_PAYMENT_PART
);
Expand Down Expand Up @@ -319,19 +317,19 @@ private function setY(int $y): void

private function printCell(
string $text,
int $w = 0,
int $h = 0,
int $nextLineAlign = 0,
int $w = 0,
int $h = 0,
int $nextLineAlign = 0,
string $textAlign = self::ALIGN_LEFT
): void {
$this->tcPdf->Cell($w, $h, $text, self::BORDER, $nextLineAlign, $textAlign);
}

private function printMultiCell(
string $text,
int $w = 0,
int $h = 0,
int $nextLineAlign = 0,
int $w = 0,
int $h = 0,
int $nextLineAlign = 0,
string $textAlign = self::ALIGN_LEFT
): void {
$this->tcPdf->MultiCell($w, $h, $text, self::BORDER, $textAlign, false, $nextLineAlign);
Expand All @@ -341,14 +339,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);
}

public function getFont(): string
{
return $this->font;
}

public function setFont(string $font): void
{
$this->font = $font;
}
}

0 comments on commit 4064568

Please sign in to comment.