Phpword: Is there any plan to support OMML or MathML?

Created on 12 Aug 2016  路  20Comments  路  Source: PHPOffice/PHPWord

Understand that OMML and MathML specs are quite complex, but if we use third party tool to author MathML, the PHPWord library just open an API to load the MathML and convert the MathML using Office's own mathml2ooml.xls, during exporting phase, it directly insert into the final document.xml. In this case, the implementation effort should be much less. Anyone can verify whether the idea is achievable? And which files I should look into?


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Most helpful comment

For the latest version , it doesn't require any hacking, the following code is working as I expected.

<?php
require_once 'bootstrap.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
$section = $phpWord->addSection();

$xml = new DOMDocument;
$xml->loadXML('<math xmlns="http://www.w3.org/1998/Math/MathML"><mroot><mi>x</mi><mn>4</mn></mroot></math>');
$xsl = new DOMDocument;
$xsl->load('./mathml2omml.xsl'); // This could be found in MSOffice installation
$processor = new XSLTProcessor;
$processor->importStyleSheet($xsl);
$omml = $processor->transformToXML($xml);
$t_omml = new DOMDocument;
$t_omml->loadXML($omml);
$omml = $t_omml->saveXML($t_omml->documentElement);    //Remove XML Version tag
echo $omml;
$section->addText($omml);


// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

All 20 comments

I did a intrusive hacking approach via patch

PhpWord/Writer/Word2007/Element/Text.php
PhpWord/Element/Text.php

For MathML, it will just use XSLT to transform to OOML and directly call

$xmlWriter->writeRaw($this->getText($element->getText()));

It has achieved my original intention, I will see how to make an more elegant patch from here.

Hi Xueliangliang, could you please share more details on the hacking approach? Thanks

+1, it would be nice

What kind of details do you need? mathml2ooml.xls can be found in Office folder, from there you can create MathML to OOML. and use $xmlWriter->writeRaw to insert the OOML into word directly.

@xueliangliang MathML->OMML conversion is not a problem. For those who need:

$xml = new DOMDocument;
$xml->loadXML($mathmlString);
$xsl = new DOMDocument;
$xsl->load('mml2omml.xsl'); // This could be found in MSOffice installation
$processor = new XSLTProcessor;
$processor->importStyleSheet($xsl);
$ommlString = $processor->transformToXML($xml);

I just couldn't find $xmlWriter object or couldn't use $xmlWriter->writeRaw method. I have active word and section objects and I would like to write that section texts and math expressions.

$word = new \PhpOffice\PhpWord\PhpWord();
$wordSection = $word->addSection();

// For simple text I use:
$wordSection->addText('text');

Thank you for your comment!

You need look into

PhpWord/Writer/Word2007/Element/Text.php,
PhpWord/Element/Text.php

to create a customised element yourself to inject the raw omml. You can not use the existing exposed API to achieve it.

And unzip a word with Math Equations, and check the actual output of the OMML, it is not a trivial task.

You need look into

PhpWord/Writer/Word2007/Element/Text.php,
PhpWord/Element/Text.php

to create a customised element yourself to inject the raw omml. You can not use the existing exposed API to achieve it.

And unzip a word with Math Equations, and check the actual output of the OMML, it is not a trivial task.

Hi ,@xueliangliang , can you provide a example of how you inject raw omml ?

For the latest version , it doesn't require any hacking, the following code is working as I expected.

<?php
require_once 'bootstrap.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
$section = $phpWord->addSection();

$xml = new DOMDocument;
$xml->loadXML('<math xmlns="http://www.w3.org/1998/Math/MathML"><mroot><mi>x</mi><mn>4</mn></mroot></math>');
$xsl = new DOMDocument;
$xsl->load('./mathml2omml.xsl'); // This could be found in MSOffice installation
$processor = new XSLTProcessor;
$processor->importStyleSheet($xsl);
$omml = $processor->transformToXML($xml);
$t_omml = new DOMDocument;
$t_omml->loadXML($omml);
$omml = $t_omml->saveXML($t_omml->documentElement);    //Remove XML Version tag
echo $omml;
$section->addText($omml);


// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

@xueliangliang , it worked! Thanks for your help.

I make this code but my docx is generate empty

For the latest version , it doesn't require any hacking, the following code is working as I expected.

<?php
require_once 'bootstrap.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
$section = $phpWord->addSection();

$xml = new DOMDocument;
$xml->loadXML('<math xmlns="http://www.w3.org/1998/Math/MathML"><mroot><mi>x</mi><mn>4</mn></mroot></math>');
$xsl = new DOMDocument;
$xsl->load('./mathml2omml.xsl'); // This could be found in MSOffice installation
$processor = new XSLTProcessor;
$processor->importStyleSheet($xsl);
$omml = $processor->transformToXML($xml);
$t_omml = new DOMDocument;
$t_omml->loadXML($omml);
$omml = $t_omml->saveXML($t_omml->documentElement);    //Remove XML Version tag
echo $omml;
$section->addText($omml);


// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

i can transform them to math type in word, but how can i concat some string together the formular? For example: "Give a fomular formular goes here please choose correct answer?"

@xueliangliang @hungdang6676

hello,
in my case i m getting an error of "XSLTProcessorNot Found".
can anyone help me with it?

@xueliangliang @hungdang6676

hello,
in my case i m getting an error of "XSLTProcessorNot Found".
can anyone help me with it?

install xsl extension.

@xueliangliang
Not being able to do so.
could you guide me how to do so, or share a link of how to install it?

@xueliangliang @hungdang6676

hello,
in my case i m getting an error of "XSLTProcessorNot Found".
can anyone help me with it?

Hi there,
i copied XSTLTProcessor from new version of PHPWord

Hi there,
i copied XSTLTProcessor from new version of PHPWord

I have recently installed PHPWORD in my code but not being able to find XSLTProcessor.

Hi there,
i copied XSTLTProcessor from new version of PHPWord

I have recently installed PHPWORD in my code but not being able to find XSLTProcessor.

It is a PHP extension: https://www.php.net/manual/en/class.xsltprocessor.php

@hungdang6676

$textrun = $section->addTextRun();
$textrun->addText("HELLO:");
$textrun->addText($omml);
$textrun->addText("WORLD!");

You can embed the formula with your normal text.

For the latest version , it doesn't require any hacking, the following code is working as I expected.

<?php
require_once 'bootstrap.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
$section = $phpWord->addSection();

$xml = new DOMDocument;
$xml->loadXML('<math xmlns="http://www.w3.org/1998/Math/MathML"><mroot><mi>x</mi><mn>4</mn></mroot></math>');
$xsl = new DOMDocument;
$xsl->load('./mathml2omml.xsl'); // This could be found in MSOffice installation
$processor = new XSLTProcessor;
$processor->importStyleSheet($xsl);
$omml = $processor->transformToXML($xml);
$t_omml = new DOMDocument;
$t_omml->loadXML($omml);
$omml = $t_omml->saveXML($t_omml->documentElement);    //Remove XML Version tag
echo $omml;
$section->addText($omml);


// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

@xueliangliang @hungdang6676
hey there I have used the same code as yours but I am unable to print mixtures of formulas and text.

Example:-

<p><math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac bevelled="true"><mi>a</mi><mi>b</mi></mfrac></math> Some Normal Text Goes Here </p>

this was the math type after conversion the string is as given below.

<p><m:oMath xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mml="http://www.w3.org/1998/Math/MathML"><m:f><m:fPr><m:type m:val="skw"/></m:fPr><m:num><m:r><m:t>a</m:t></m:r></m:num><m:den><m:r><m:t>b</m:t></m:r></m:den></m:f></m:oMath> Some Normal Text Goes Here </p>

Now I need to insert this text inside a word file using PHPWORD but the file gets corrupted.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

igorsantos07 picture igorsantos07  路  3Comments

agang235 picture agang235  路  3Comments

fishwolf picture fishwolf  路  4Comments

cedrictailly picture cedrictailly  路  5Comments

totpero picture totpero  路  6Comments