mPDF Manual – mPDF functions

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.

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 (if AUTOFONT_RTL is set)

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-detection
  • AUTOFONT_CJK = 1 - Any CJK languages (Chinese - Japanese - Korean)
  • AUTOFONT_THAIVIET = 2 - Thai and Vietnamese
  • AUTOFONT_RTL = 4 - RTL languages i.e. Hebrew and Arabic, including Pashto, Urdu etc.
  • AUTOFONT_INDIC = 8 - Indic languages such as Devanagari
  • AUTOFONT_ALL = 15 - All of the above

Default: AUTOFONT_ALL (15)

Changelog

Version Description
4.0

AutoFont marks up the <span> with class="lang_xx" as well as  lang="xx"

2.3 Function was added.
2.3

Internal variable $pregRTLchars was altered for better detection of arabic/hebrew

6.0

Removed in favour of $autoLangToFont variable

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
Fork me on GitHub