Jonne Mikkanen - 2014-12-11

I'd like TCPDF to automatically insert line breaks at hyphens (-) if needed, like in the word 15-year-old. It works in the "year-old" part, but "15-year" never breaks, even when necessary. It seems the system won't let lines break if there are numbers on the left side of the hyphen. (Actually, I'm not sure if it's grammatically proper to break lines this way in English, but it is in Finnish and I'd need it for that)

Example image

Here, "15-year-old" is not broken at the first hyphen, but "ab-year-old" is. How can I make words with numbers behave the same way as "ab-year-old"? Before you say it, I have tried the hyphenation functions which are excellent for what they do (add soft hyphens). That doesn't help here (it won't add a soft hyphen after a hard one, and if you do it manually, you get two lines if the line breaks such as "12--
year-old").

I also can't replace all "-"-letters with ­-soft hyphens, because then words like 15-year-old become "15yearold" if they don't happen to be at the end of a line.

Thanks in advance!

Example code:

require_once('../tcpdf/tcpdf.php');
class MYPDF extends TCPDF {
    public function Header() {
        // Coloring the margins for clarity:
        $this->setColor("fill", 0, 0, 0, 50);
        $this->Rect(0, 0, 148, 20, 'F');
        $this->Rect(0, 185, 148, 25, 'F');
        $this->Rect(128, 0, 21, 210, 'F');
        $this->Rect(0, 0, 17, 210, 'F');
    }
    public function Footer() {  }
}
$pdf = new MYPDF('P', 'mm', 'A5', 1, 'UTF-8');
$pdf->setMargins(17, 21, 20);
$pdf->setColor("fill", 100, 0, 0, 0);

$pdf->AddPage('P', 'A5');
$pdf->SetAutoPageBreak(1, 25);

// Title:
$pdf->SetFont('Courier', 'B', 12, '', false);
$pdf->writeHTML("Line break test", 1, 0, 0, 0,"L");
$pdf->ln(5);

// Reserved area:
$pdf->Rect(65, 31, 63, 90);
$regions = array(array('page' => '', 'xt' => 65, 'yt' => 31, 'xb' => 65, 'yb' => 90, 'side' => 'R'));
$pdf->setPageRegions($regions);

// Page text:
$pdf->SetFont('Courier', '', 12, '', false);
$text = "   <p>Line break te1: 12-year-old</p>
            <p>Line break te2: ab-year-old</p>";
$pdf->writeHTML($text, '', 1);

$pdf->Output(); ?>
 

Last edit: Jonne Mikkanen 2014-12-11