-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there any plan to support OMML or MathML? #860
Comments
I did a intrusive hacking approach via patch PhpWord/Writer/Word2007/Element/Text.php For MathML, it will just use XSLT to transform to OOML and directly call
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? |
@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
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:https://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 |
i can transform them to math type in word, but how can i concat some string together the formular? For example: "Give a fomular |
hello, |
install xsl extension. |
@xueliangliang |
Hi there, |
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 |
$textrun = $section->addTextRun();
$textrun->addText("HELLO:");
$textrun->addText($omml);
$textrun->addText("WORLD!"); You can embed the formula with your normal text. |
@xueliangliang @hungdang6676 Example:-
this was the math type after conversion the string is as given below.
Now I need to insert this text inside a word file using PHPWORD but the file gets corrupted. |
Thanks for the code snippet! It partially works for me. It shows a math equation in the word document when I open it, with the fourth root symbol well placed, but with an empty placeholder in the argument (a blank square). Any idea why would this be happening? I use Word 2007, and I have MathType installed? Thanks in advance for any guidance or pointers |
<?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();
$textrun = $section->addTextRun();
$textrun->addText("Please find the equivalent expression for ");
$textrun->addText(mathml2omml('<math xmlns="http:https://www.w3.org/1998/Math/MathML"><msup><mi>x</mi><mn>2</mn></msup><mo>-</mo><msup><mi>y</mi><mn>2</mn></msup></math>'));
$textrun->addText(" below:");
$textRunA = $section->addTextRun();
$textRunA->addText("A. ");
$textRunA->addText(mathml2omml('<math xmlns="http:https://www.w3.org/1998/Math/MathML"><mfenced><mrow><mi>x</mi><mo>-</mo><mi>y</mi></mrow></mfenced><mfenced><mrow><mi>x</mi><mo>+</mo><mi>y</mi></mrow></mfenced></math>'));
$textRunB = $section->addTextRun();
$textRunB->addText("B. ");
$textRunB->addText(mathml2omml('<math xmlns="http:https://www.w3.org/1998/Math/MathML"><msup><mfenced><mrow><mi>x</mi><mo>-</mo><mi>y</mi></mrow></mfenced><mn>2</mn></msup></math>'));
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
function mathml2omml($mathml) {
$xml = new DOMDocument;
$xml->loadXML($mathml);
$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
return $omml;
} Mix normal text with mathml. I also attached the generated docx file. |
@xueliangliang How can be read Mathtype from docx file using PhpWord? When i tried to convert in pdf it shows me @xueliangliang I have searched lots of tickets and blogs. But did not get any solution. Please help me I am sharing docx file, please have a look. |
How to mix html with mathml? |
How to do the opposite? Extract Word Equation to MathML? |
@nandaks I am also having the same problem as you. Have you been able to solve this problem? |
Has anyone managed to extract it? |
@yangweijie @xueliangliang @r3sist @eduardobreno @frozenade @lucus-lee @lucasres @redrum0x @hungdang6676 @Yolver Hello there, please, could you give us some files with math equations to analyze it and create a pull request ? |
I am upload this test file to https://easyupload.io/4fmcye |
File from @redrum0x test+php.docx |
FYI : A Pull Request is in Progress : #2477 |
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.
The text was updated successfully, but these errors were encountered: