Skip to content
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

Closed
xueliangliang opened this issue Aug 12, 2016 · 32 comments · Fixed by #2477
Closed

Is there any plan to support OMML or MathML? #860

xueliangliang opened this issue Aug 12, 2016 · 32 comments · Fixed by #2477
Assignees
Milestone

Comments

@xueliangliang
Copy link

xueliangliang commented Aug 12, 2016

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.

@xueliangliang
Copy link
Author

xueliangliang commented Aug 12, 2016

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.

@lucus-lee
Copy link

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

@r3sist
Copy link

r3sist commented Dec 13, 2018

+1, it would be nice

@xueliangliang
Copy link
Author

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.

@r3sist
Copy link

r3sist commented Dec 19, 2018

@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!

@xueliangliang
Copy link
Author

xueliangliang commented Dec 21, 2018

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.

@eduardobreno
Copy link

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 ?

@xueliangliang
Copy link
Author

xueliangliang commented Feb 12, 2019

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');

@eduardobreno
Copy link

@xueliangliang , it worked! Thanks for your help.

@lucasres
Copy link

I make this code but my docx is generate empty

@hungdang6676
Copy link

hungdang6676 commented Jun 5, 2020

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');

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?"

@bhavink21
Copy link

bhavink21 commented Sep 23, 2020

@xueliangliang @hungdang6676

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

@xueliangliang
Copy link
Author

@xueliangliang @hungdang6676

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

install xsl extension.

@bhavink21
Copy link

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

@hungdang6676
Copy link

@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

@bhavink21
Copy link

bhavink21 commented Oct 12, 2020

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.

@xueliangliang
Copy link
Author

xueliangliang commented Oct 14, 2020

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

@xueliangliang
Copy link
Author

@hungdang6676

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

You can embed the formula with your normal text.

@bhavink21
Copy link

bhavink21 commented Nov 30, 2020

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 @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:https://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:https://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mml="http:https://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.

@jjtorres2002
Copy link

@xueliangliang @hungdang6676

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).

empty place holder

Any idea why would this be happening? I use Word 2007, and I have MathType installed?

Thanks in advance for any guidance or pointers

@xueliangliang
Copy link
Author

xueliangliang commented Mar 13, 2021

<?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.

helloWorld.docx

@nandaks
Copy link

nandaks commented Dec 31, 2021

@xueliangliang How can be read Mathtype from docx file using PhpWord? When i tried to convert in pdf it shows me
<Object: word/embeddings/oleObject1.bin>

@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.
qq.docx

@yangweijie
Copy link

@Yolver
Copy link

Yolver commented Oct 9, 2022

How to mix html with mathml?
I used HTML::addHtml() method and addText() method, but there is no expect effect.
please help me.

@frozenade
Copy link

How to do the opposite? Extract Word Equation to MathML?

@nguyenvanlongBG
Copy link

nguyenvanlongBG commented Mar 18, 2023

@nandaks I am also having the same problem as you. Have you been able to solve this problem?

@redrum0x
Copy link

Has anyone managed to extract it?

@Progi1984
Copy link
Member

@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 ?

@redrum0x
Copy link

@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 ?

image

I am upload this test file to https://easyupload.io/4fmcye

@Progi1984
Copy link
Member

File from @redrum0x  test+php.docx

@Progi1984
Copy link
Member

FYI : A Pull Request is in Progress : #2477

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.