From 209a2e6803318a6739c2df4f6c1dbb8b39e6338c Mon Sep 17 00:00:00 2001 From: rmm Date: Wed, 24 Jul 2024 19:31:18 +0200 Subject: [PATCH] fix text justification for Unicode fonts --- src/Html2Pdf.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Html2Pdf.php b/src/Html2Pdf.php index fb3c4b3f..94a9976a 100755 --- a/src/Html2Pdf.php +++ b/src/Html2Pdf.php @@ -578,7 +578,7 @@ public function output($name = 'document.pdf', $dest = 'I') // call the output of TCPDF $output = $this->pdf->Output($name, $dest); - + // close the pdf and clean up $this->clean(); @@ -3767,6 +3767,9 @@ protected function _tag_open_WRITE($param) $w = $this->parsingCss->value['width']; $align = 'R'; } + else if ($this->parsingCss->value['text-align'] === 'justify') { + $align = 'J'; + } // calculate the width of each words, and of all the sentence $w = 0; @@ -3833,7 +3836,7 @@ protected function _tag_open_WRITE($param) } if (strlen($str[0])) { - $this->pdf->SetXY($this->pdf->GetX(), $y+$dh+$dy); + $this->pdf->SetXY($this->pdf->GetX(), $y+$dh+$dy); $this->pdf->Cell($wc, $h, $str[0], 0, 0, $align, $fill, $this->_isInLink); $this->pdf->SetXY($this->pdf->GetX(), $y); } @@ -3907,6 +3910,7 @@ protected function _tag_open_WRITE($param) } // if we have words after automatic cut, it is because they fit on the line => we write the text + // if text should be justified, it will be left-aligned to avoid long spaces between words for shorter texts if (count($words)) { $txt = ''; foreach ($words as $k => $word) { @@ -3914,7 +3918,7 @@ protected function _tag_open_WRITE($param) } $w+= $this->pdf->getWordSpacing()*(count($words)); $this->pdf->SetXY($this->pdf->GetX(), $y+$dh+$dy); - $this->pdf->Cell(($align === 'L' ? $w : $this->parsingCss->value['width']), $h, $txt, 0, 0, $align, $fill, $this->_isInLink); + $this->pdf->Cell(($align === 'L' ? $w : $this->parsingCss->value['width']), $h, $txt, 0, 0, $align === 'J' ? 'L' : $align, $fill, $this->_isInLink); $this->pdf->SetXY($this->pdf->GetX(), $y); $this->_maxH = max($this->_maxH, $lh); $this->_maxE+= count($words);