Hyphenation
(mPDF ≥ 2.5)
Hyphenation was changed in mPDF 5.7 to support the CSS property hyphens (cf.)
The CSS property hyphens
is supported on block elements (e.g. <div>
),
inline elements (e.g. <span>
) and <td>
/<th>
.
hyphens: none|manual|auto
Default: manual
The default can be changed by altering defaultCSS
configuration variable;
Values:
none
- Words are not broken at line breaks, even if characters inside the word suggest line break points.manual
- Words are only broken at line breaks where there are characters inside the word that suggest line break opportunities. Characters can be explicit or conditional.auto
- Words can be broken at appropriate hyphenation points, as determined by characters inside the word, resources. SHY (i.e. soft-hyphen) inside the word take priority over hyphenation points determined by other resources.
Soft hyphens
The soft-hyphen character (U+00AD or ­
) and the <wbr>
tag (from mPDF 5.7) are supported in
WriteHTML().
Automatic hyphenation
Automatic hyphenation is set using CSS:
hyphens: auto;
Automatic hyphenation is based on the commonly used TeX algorithm and requires pattern files for each language. The following languages are supplied with mPDF 2.5:
Language | $SHYlang variable |
---|---|
English | 'en' (Default) |
German | 'de' |
Spanish | 'es' |
Finnish | 'fi' |
French | 'fr' |
Italian | 'it' |
Dutch | 'nl' |
Portuguese | 'pl' |
Russian | 'ru' |
Swedish | 'sv' |
A pattern file for each language is found in the directory [mpdf root]/data/patterns/
and the
configuration variable
$SHYlanguages needs to be updated if any additions are made.
The $SHYlang variable specifies which language to use for the automatic hyphenation from
the $SHYlanguages array.
Pattern checking can be fine-tuned by four variables if required:
mPDF variable | Default value | Description |
---|---|---|
$SHYleftmin | 2 |
Minimum number of characters allowed to the left of a hyphen. |
$SHYrightmin | 2 |
Minimum number of characters allowed to the right of a hyphen. |
$SHYcharmin | 2 |
Minimum number of characters of words to be checked. |
$SHYcharmax | 10 |
Maximum number of characters in a pattern used for pattern checking (10 usually is more than enough). This is NOT the maximum length of words to be checked. |
Examples
<?php
$mpdf = new \Mpdf\Mpdf();
$mpdf->SHYlang = 'fr';
$mpdf->SHYleftmin = 3;
$mpdf->WriteHTML('Le long texte....');
$mpdf->Output();
Hyphenation Dictionary
If automatic hyphenation does not recognise a particular word, you can add words to a dictionary file with your own
hyphenation. Edit the file [path to mpdf]/patterns/dictionary.txt
and add a new line for each word, marking the
possible hyphenations with a forward slash. You can mark more than one place for each word e.g.
dis/es/tab/lish/men/tar/i/an/ism
See Also
- SHYlang - Specify the language to use for automatic hyphenation