mPDF Manual – What Else Can I Do

Math Formulae with MathJax

mPDF does not support MathML or LaTeX math formulae directly. However you can include math in a PDF document making use of MathJax following the steps below.

MathJax is a Javascript program which renders math equations in a browser from either MathML or LaTeX sources. You need to use MathJax first to render the equation, and MathJax needs to be configured to offer SVG as an output option. To demonstrate, start with a sample page such as

1) Open the HTML page in your browser (allowing MathJax to render the equations).

2) Right click over one of the equations and select: Math Settings >> Math Renderer >> SVG (This re-renders all the equations n the page in SVG format),

3) Save the page including the javascript-processed code. In Firefox, select File >> Save Page As… (This doesn’t work in IE which only saves the original HTML code prior to processing).

4) Edit the file to make any necessary changes e.g. CSS stylesheets.

5) Run the following script to produce your PDF document (assumes you saved the file as TeXSample.htm). This adjusts the SVG code produced to allow mPDF to display it:


$mpdf = new mPDF('');

$html = file_get_contents('TeXSample.htm');


$defs = $m[1];

$html = preg_replace('/<svg[^>]*>\s*<defs.*?<\/defs>\s*<\/svg>/','',$html);

$html = preg_replace('/(<svg[^>]*>)/',"\\1".$defs,$html);


for ($i=0;$i<count($m[0]);$i++) {


    preg_match('/width: (.*?);/',$style, $wr);

    $w = $mpdf->ConvertSize($wr[1],0,$mpdf->FontSize) * $mpdf->dpi/25.4;

    preg_match('/height: (.*?);/',$style, $hr);

    $h = $mpdf->ConvertSize($hr[1],0,$mpdf->FontSize) * $mpdf->dpi/25.4;

    $replace = '<svg'.$m[1][$i].' width="'.$w.'" height="'.$h.'" style="'.$m[2][$i].'"';

    $html = str_replace($m[0][$i],$replace,$html);





See an example of output: