Forum


Replies: 4   Views: 4490
Embedhtml(): how to style bold text when strictwordstyles = true
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 shroom  · 16-12-2015 - 22:50

We are building Word docx's by pulling text from a database.  That text has HTML in it.  A sample might be:

"The quick <strong>brown fox</strong> jumped over the lazy <strong>sleeping dog</strong>."

embedHTML() works fine, and displays the text as expected.

But what I want to do is make the bolded text a little smaller.  In HTML, this is easy: I just style <strong> tags with "font-size:90%".  But since I am using embedHTML with strictWordStyles = true, it discards all inline styles.  I understand this, and have tried several ways to get phpdocx to style bolded text like I need.

I have a custom style in the doxc I am using as a template that works fine.  I can select the words I want to apply the style to, apply the style,and they are bolded and made a little smaller.  With embedHTML, I have tried assigning my custom style to "<strong>" tags in the wordstyles() array, it does not work.  I have tried changing <strong>sleeping dog</strong> to <span class="myboldclass">sleeping dog</span> and assigning myboldclass to the custom style in the wordstyles() array, it does not work.

How can I get phpdocx to apply a custom style to <strong> tags?  Note that I have no problem applying custom styles to <p>, <h2>, etc tags, but I cannot figure out how to apply a custom style to <strong> tags.  Also note that if I class a <p> with "myboldclass", it works, the entire paragraph is bolded and the font made a little smaller.  But I do not want to class an entire paragraph, I want the class to apply to words within a paragraph.  According to the documentation, applying a custom style to a classed <span> should work, but it does not.

I hope that makes sense.

Thanks

Posted by admin  · 17-12-2015 - 08:04

Hello,

We recommend you to read in detail the documentation available on this page: http://www.phpdocx.com/documentation/introduction/html-to-word-PHP

It explains all supported tag and styles. If the style or tag is not explicity detailed on this page, then it's not officially supported.

For example about font-size, the units may be pixels, points or ems...

And a paragraph custom style can't be applied to a chunk of text, only to the whole paragraph.

We recommend you to use strictWordStyles as false with your own template that is a more flexible approach; or use more than once embedHtml to add each HTML content. To set some rPr attributes (font-size, color, position), this is styles for a run of text, you need to set the strictWordStyles as false. There's no need, but some special cases, to set strictWordStyles always as true.

Regards.

Posted by shroom  · 18-12-2015 - 15:35

>>We recommend you to read in detail the documentation available on this page: http://www.phpdocx.com/documentation/introduction/html-to-word-PHP.

Believe me, I have read that page many times.

>>And a paragraph custom style can't be applied to a chunk of text

I am not trying to apply a paragraph style, I am trying to apply a character style to a <span>.  This guy had the same problem.  Was it ever solved?

http://www.phpdocx.com/en/forum/default/topic/536

The documentation states: "One may associate different Word styles to HTML classes, ids or tags."  I have tried associating an imported style to a classed <span>, it does not work.  I have tried associating an imported style to a tag, it does not work except for certain tags (<h1>, <h2>, etc).  I know the imported style is valid because when I apply it to the text after the docx is created, it works.  

I tried setting strictWordStyles = false, and while inline styles are then processed, some of my custom styles are not fully applied.  

It just seems that with strictWordStyles=true, there should be SOME way to apply a custom style to a classed <span>.  According to the documentation it should work, but I have not had any success.  Is it possible for you to create a simple example demonstrating that it does work?

Thanks.

Posted by admin  · 21-12-2015 - 08:07

Hello,

UPDATE: phpdocx 9.5 added support  for custom character styles when importing HTML.

No, there isn't support of Word character styles when importing HTML; you need to add them setting strictWordStyles as false and through HTML styles; or don't use HTML content but native methods: addList, addText, WordFragments...

It's explained on the documentation pages:

'One may use native Word styles for paragraphs and tables.'

To set span styles you need to set strictWordStyles as false and add the styles inline or through CSS. If some styles aren't applied then they're not supported.

Regards.

Posted by shroom  · 21-12-2015 - 15:01

I figured out how to accomplish what I needed by modifying HTML2WordML.inc:

case 'sub':
case 'sup':
case 'span':
   if ($nodo['nodeName'] == 'sub') {self::$openScript = 'subscript';}
   if ($nodo['nodeName'] == 'sup') {self::$openScript = 'superscript';}
   self::$WordML .= $this->closePreviousTags($depth, $nodo['nodeName']);
   if(!empty($nodo['attributes']['class'][0])){
      self::$WordML .= '<w:rPr><w:rStyle w:val="'.$nodo['attributes']['class'][0].'"/></w:rPr>';
   }
   self::$openTags[$depth] = $nodo['nodeName'];
   break;
case '#text':

This allows me to assign character classes to <sup>s, <sub>s, and <span>s even  when strictWordStyles = true.  It works well except that the class is not applied if the <span> or <sup> is the first element in a <p>.  It's probably something missing in the WordML I am inserting...  My workaround is to insert a zero-length-space (&#8203;) preceding <span>s and <sup>s.  Then it works fine.

I have one more issue I need to handle concerning how to embed footnotes in strings containing HTML.  I'll post about it later.

Thanks.