SetAutoFont()
(mPDF ≥ 2.3 ≤ 5.7)
SetAutoFont – Automatically detect language in the input HTML text and use appropriate fonts
Description
void SetAutoFont ( int $flag )
Turns on the AutoFont function, which automatically detects language in the input HTML text and uses appropriate fonts.
Note: This function was removed in mPDF 6.0 Use the
$autoLangToFont
variable
for the same results
AutoFont uses <span lang="" class="lang_xx">...</span>
to mark text which is auto-detected.
See lang for further details,
but note that SetAutoFont()
also:
- disables support for
text-align=justify
- sets
$useLang
= true
- sets
$biDirectional
= true
(ifAUTOFONT_RTL
is set)
Note: Using automatic language detection adds considerable processing time when creating a large document.
Note: It is better to mark the HTML text yourself using block tags e.g. <p lang="">
rather than relying on mPDF to set <span lang="">
if possible; when set at block level, it allows appropriate
changes to text alignment, character spacing and text-justification.
Note: Automatic language detection is based on analysis of the characters in the text. It is therefore not precise, and in particular it may be difficult to distinguish between different arabic languages (arabic, farsi, urdu, pashto etc), CJK languages (even between Chinese and Japanese text in same text chunk).
Note: Chinese Traditional and Simplified Chinese share so many characters that mPDF does not
even try to distinguish which is being used. If AutoFont is being used and a chinese languages is identified,
it is marked as <span lang="zh">...</span>
.
The font actually used for default chinese is determined by the settings in function
GetCodepage() in \Mpdf\LangToFont
class.
By default this is Chinese Simplified (GB, GBK).
Note: SetAutoFont is only valid when using UTF-8 as the codepage for the document.
Parameters
- $flag
-
This parameter specifies which languages are auto-detected. Either an integer or one of the defined constants can be used.
SetAutoFont(0)
will turn off any auto-detection.Bitwise operators can be used for combining the defined constants e.g.
SetAutoFont(AUTOFONT_CJK | AUTOFONT_THAIVIET)
will turn on autodetection for CJK languages and Thai and Vietnamese.Values
0
- Turn off any auto-detectionAUTOFONT_CJK
= 1 - Any CJK languages (Chinese - Japanese - Korean)AUTOFONT_THAIVIET
= 2 - Thai and VietnameseAUTOFONT_RTL
= 4 - RTL languages i.e. Hebrew and Arabic, including Pashto, Urdu etc.AUTOFONT_INDIC
= 8 - Indic languages such as DevanagariAUTOFONT_ALL
= 15 - All of the above
Default:
AUTOFONT_ALL
(15)
Changelog
Version | Description |
---|---|
4.0 |
AutoFont marks up the |
2.3 | Function was added. |
2.3 |
Internal variable $pregRTLchars was altered for better detection of arabic/hebrew |
6.0 |
Removed in favour of |
Examples
Example #1
<?php
// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8']);
$html = '
Most of this text is in English, but has occasional words in Chinese:其貢獻在 or
Vietnamese: Một khảo sát mới cho biết, or maybe even Arabic: البرادعی
البرادعی -12- البرادعی
其貢獻在國際間亦備受肯定,2005年
';
$mpdf->SetAutoFont();
$mpdf->WriteHTML($html);
$mpdf->Output();
See Also
- $useLang - Specify whether to recognise and support the HTML attribute lang
- $autoFontGroupSize - Specify the text chunk size to group when autodetecting text language
- disableMultilingualJustify (mPDF < 6.0) - Specify whether to disable text justification in multilingual documents
- lang - Information on mPDF support for the HTML attribute lang