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

MathML to Docx #2559

Closed
KaioSpa opened this issue Jan 27, 2024 · 3 comments · Fixed by #2645
Closed

MathML to Docx #2559

KaioSpa opened this issue Jan 27, 2024 · 3 comments · Fixed by #2645

Comments

@KaioSpa
Copy link

KaioSpa commented Jan 27, 2024

Hey, I'm lost here and really need help. I'm sorry if it's obvious and I'm just blind...

I'm trying to convert MathML formulas to docx. I have seen that a merge was made, but I was not able to implement it.

When I try to convert directly to ODText, the formula does not appear, and it just shows the numbers without the formula (10^20 appears as 1020).

$phpWord = new PhpWord();
    $section = $phpWord->addSection();
    Html::addHtml($section, $html, false, false);

    // Save the resulting document to a temporary file
    $tempDoc = tempnam(sys_get_temp_dir(), 'PHPWord');
    $phpWord->save($tempDoc, 'Word2007');

And when trying to use the Formula class, it just returns empty:

$math = new Formula(new Math($mathML));

An example of my MathML element (I extracted the element to try to use the Formula class):
$mathML=<math xmlns="https://www.w3.org/1998/Math/MathML"><mn>4</mn><mi>x</mi><mo>-</mo><mfrac><mrow><mn>3</mn><mo>-</mo><mi>x</mi></mrow><mn>2</mn></mfrac><mo>=</mo><mn>21</mn><mo>&#160;</mo><mo>&#160;</mo><mo>&#160;</mo><mi>e</mi><mo>&#160;</mo><mo>&#160;</mo><mo>&#160;</mo><mfrac><mrow><mi>y</mi><mo>-</mo><mn>3</mn></mrow><mn>2</mn></mfrac><mo>=</mo><mfrac><mn>1</mn><mrow><mi>y</mi><mo>+</mo><mn>2</mn></mrow></mfrac><mo>+</mo><mfrac><mn>3</mn><mn>2</mn></mfrac></math>

Can someone help?

@Opussci
Copy link

Opussci commented Jul 30, 2024

Hi @KaioSpa ,
have you managed to solve this issue?

I am also very much interested in writing MathML to docx files, can anybody help with this?

@Progi1984
Copy link
Member

Progi1984 commented Aug 12, 2024

@KaioSpa & @Opussci

It's fixed in develop branch with #2645.

Code :

// Read contents
$name = basename(__FILE__, '.php');
$source = __DIR__ . "/{$name}.docx";

$mathML='<math xmlns="https://www.w3.org/1998/Math/MathML">
<mn>4</mn><mi>x</mi><mo>-</mo>
<mfrac><mrow><mn>3</mn><mo>-</mo><mi>x</mi></mrow><mn>2</mn></mfrac><mo>=</mo><mn>21</mn><mo>&#160;</mo><mo>&#160;</mo><mo>&#160;</mo><mi>e</mi><mo>&#160;</mo><mo>&#160;</mo><mo>&#160;</mo><mfrac><mrow><mi>y</mi><mo>-</mo><mn>3</mn></mrow><mn>2</mn></mfrac><mo>=</mo><mfrac><mn>1</mn><mrow><mi>y</mi><mo>+</mo><mn>2</mn></mrow></mfrac><mo>+</mo><mfrac><mn>3</mn><mn>2</mn></mfrac></math>';

$reader = new \PhpOffice\Math\Reader\MathML();
$math = $reader->read($mathML);

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addFormula($math);

$phpWord->save($source, 'Word2007');

image

@Progi1984
Copy link
Member

@Opussci & @KaioSpa This issue has been fixed by a maintainer. You can help him by sponsoring him through Github sponsors.

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

Successfully merging a pull request may close this issue.

3 participants