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

Fix text justification for Unicode fonts #802

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions src/Html2Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -3907,14 +3910,15 @@ 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) {
$txt.= ($k ? ' ' : '').$word[0];
}
$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);
Expand Down