Forum


Replies: 3   Views: 2013
Replacevariablebywordfragment inline removes the font size of the rest of the text
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by droh10  · 06-12-2019 - 10:06

Hi,

I have a problem with replaceVariableByWordFragment (type: inline), when i replace the variables in my template document the text font size of the rest of the text is no longer the same.

Inside my template document will have something like this:

line text 1
line text 2 |mathml| and a text after.
line text 3

then i create a WordFragment and create a mathml throught addMathEquation().
when i run the replaceVariableByWordFragment and then create the file,

the  text, line  text 2 "replaced by mathml equation" and a text after, the "and a text after" portion is no longer the same font size with "line  text 2" portion

my code is like this

$pStylev2 = array(
    'font' => 'Times New Roman',
    'fontSize' => 15,
    'contextualSpacing' => true,
    'spacingBottom' => 0,
    'spacingTop' => 0,
    'lineSpacing' => 240,
);
$docx = new CreateDocx();
$docx->addText("line text 1", $pStylev2);
$docx->addText("line text 2 |mathml| and a text after.", $pStylev2);
$docx->addText("line text 3", $pStylev2);
$tmpfilename = "sampledoc";
$docx->createDocx($tmpfilename);
$docxtemplate = new CreateDocxFromTemplate($tmpfilename. ".docx");
$docxtemplate->setTemplateSymbol('|');
$keyfragmentarray = array();
$mathml[] = '<math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi></math>';
foreach ($mathml as $key => $value) {
    $fragment = new WordFragment($docxtemplate);
    $fragment->addMathEquation($value, 'mathml', array('align' => 'left'));
    $keyfragmentarray["mathml"] = $fragment;
}
$docxtemplate->replaceVariableByWordFragment($keyfragmentarray, array('type' => 'inline'));
$docxtemplate->createDocxAndDownload("replacesample.docx");

basically, "line text 2" will be font size 15 but the text after the mathml, "and a text after", will be turned into font size 11 (i think it is the default);

any help will be greatly appreciated. thanks

Posted by admin  · 06-12-2019 - 10:30

Hello,

On https://www.phpdocx.com/api-documentation/word-content/add-math-OMML-Word-document-with-PHP you can read the supported styles when inserting MathML contents:

options

The possible keys and values are (required options are shown with yellow background):

Key Type Description

align string left, center, right

As the font size style is not directly supported by the method, you need to use a custom paragraph style (https://www.phpdocx.com/api-documentation/layout-and-general/create-Word-custom-paragraph-style-with-PHP) or DOCXCustomizer (https://www.phpdocx.com/documentation/introduction/docxcustomizer). Or set it in the imported MathML tags.

The current beta of phpdocx includes new options for addMathEquation (such as font size, underscore, bold and others) to be applied to math equations). If you upgrade your license to the latest release, the dev team will send you the new classes and new samples to illustrate how to use them. You can upgrade your license on MY PHPDOCX page after login.

Regards.

Posted by droh10  · 06-12-2019 - 10:52

Oh okay,ill have to talk with my manager for the upgrade.

You are saying that the mathml is making the text font size different after replacing? it will affect the text after the mathml insert? am i correct?

and one more thing, some of the text are not inserted by addText() function, some are by embedHTML. im working  on a webpage and im converting a math question with some mathml together to docx document,  thats why some done by embedHTML();

Posted by admin  · 06-12-2019 - 10:59

Hello,

Yes, the replacement is adding another font size to the equation. Equations in MS Word work using particular tags, so they can't be used as regular text/contents.

After you have done the upgrade please send to contact[at]phpdocx.com and e-mail attaching a MahtML sample and a template, and if you are using the classic or namespaces package, and the dev team will send the classes and a sample script.

Regards.