Forum


Replies: 2   Views: 3428
Embedded html : how to inherit the font size and family of a template document?
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 Bouillou  · 01-11-2017 - 09:06

Hi,

I am using PHPDocx Corporate license. 

Is it possible to embed HTML content with the method replaceVariableByHTML and inherit the font-size and font-family used in the Word template?

For example, I start by creating a new object with CreateDocxFromTemplate. This template has a special font and font-size defined for the whole document. Let's take "Font-size 55" and "Font-family Algerian" for this example.

I would like that the embedded HTML inherits of these font-size and font-family. How can I do that?

I tried to add a wrapping DIV with "font-family: inherit; font-size: inherit" but unfortunately it is not working.

Best regards,

Sébastien

Posted by admin  · 01-11-2017 - 13:22

phpdocx 10 Premium licenses include the new option stylesReplacementType in HTML Extended, to keep existing placehoder pPr and rPr styles when being replaced by HTML.

 

Hello,

There's no method do it automatically. The easiest solution would be extracting the default information from the word/styles.xml file (w:docDefaults tag) and apply it to the HTML as a body (or another root tag) CSS.

The beta version of the Indexer class allows extracting that information (default font size, font family, lang and spacing); if you upgrade to the latest version of phpdocx we'll send you this new file and a script to illustrate how to that.

Regards.

Posted by Bouillou  · 01-11-2017 - 20:01

Thank you for your reply.

By removing font-style and font-family, then wrapping a div with font-size:initial, the embeded HTML is formatted as the "Normal" sytle is defined in the template. It is enough for me, I can customize each template as I want with the "Normal" style and a generic PHP code. 

In case of more than one style is needed, check replaceVariablebyHTML methods strictWordStyles and wordStyles.

//Convert accents in html entities
$html = htmlspecialchars_decode(htmlentities($html, ENT_NOQUOTES, 'UTF-8'), ENT_NOQUOTES);

// Remove font-size CSS statements
$html = preg_replace("/font-size: ?(\d*[.])?\d+(px|%|pt);/", '', $html);

// Remove font-family CSS statements
$html = preg_replace("/(font-family:)(.+?)(;)/", '', $html);

//Set the font size
$html = "<div style='font-size:initial;'>".$html."</div>";

Best regards