Adding HTML

Apr 22, 2011 at 6:01 PM

Hey:

Is there any way to add HTML to a PHPWord object and have it rendered? I've tried adding HTML via the addText method, but it shows up as HTML source instead of rendered HTML.

Thanks!

Apr 23, 2011 at 5:33 PM

Why put HTML in a Word document?  Post a sample of the HTML you want to use.

Apr 24, 2011 at 5:34 PM
Edited Apr 24, 2011 at 5:35 PM

It was more of a question of "how could I" as opposed to "should I" :)

As far as a sample:

 

<div style="float:left; background-color: white; margin: 2px;">
   <h1>Test HTML</h1>
   <p>test content title</p>
   <table style="font-family: Arial">
      <tr>
          <td>test1</td>
          <td style="color: red">test2</td>
          <td>test3</td>
          <td style="color: red">test4</td>
      </tr>
   </table>
</div>

The actual code I'm using is much, much longer, but the content is similar: a pretty straight forward div containing some header and p tags w/ inline styles throughout. 

Apr 24, 2011 at 5:48 PM

I asked 'why do it' because I was curious. Still am. :-)

Most formatting that one can do with HTML can be done with XML , though PHPWord may not yet have all possible styles developed yet. Lots of room for new features.

In any case, as you've already discovered, PHPWord does not render HTML, nor does Word.

Apr 24, 2011 at 6:20 PM

I have some data that I need to export from PHP to a Word document, and it would look MUCH better (and be a lot easier) if I can just take the generated HTML and dump it into a word document. I did this the quick, easy, and incorrect way already using headers, but I also need images exported as well. PHPWord handles the images great. 

Apr 25, 2011 at 5:04 PM

So basically, the answer I'm seeing is "No, there is no way to pass HTML into the PHPWord object and have it rendered the resulting Word document", yes? If so, are there any plans to implement this feature in future versions? 

Apr 26, 2011 at 2:57 PM

I am not a PHPWord developer, just a user trying to help in the forum. I don't know what, if any, plans there are for future iterations of PHPWord. I do know that several users are working on code to improve & expand PHPWord.

Jul 19, 2011 at 12:59 AM

I am wondering the same things. Does anyone know if this is still being developed? I am trying to do exactly the same thing...

Sep 14, 2011 at 7:19 PM

Hi,

to lavelle568, I don't think it is possible.

But I'd be interested in a way to mimic html style float left, to display a sequence of images side by side, with a description just under each image and a fixed space width between each image.
They might have diferent size, so putting it in a table would not be a good idea.

Nov 8, 2011 at 4:17 PM

I have written some code which is capable of handling simple HTML (the sort that you might find in a blog post say). It recursively steps through the dom tree created by PHP Simple HTML Dom creating elements in PHPWord as it does so. It handles HTML elements including tables, paragraphs, links, images, lists, it could also handle some basic CSS. In using such a converter it is necessary to make decisions about how you want the HTML to be converted into the Word document - for example in my set up paragraphs are converted to a text run followed by a text break, words within PHPWord list elements don't seem to be separately stylable (important for our use), so I create text runs beginning with a number or bullet as required.

Coordinator
Nov 8, 2011 at 9:32 PM

Hi neilt17.

I got a lot of feature requests about adding HTML Code to PHPWord Objects.
If you can share the code i can try to implement it in PHPWord.

Thanks,

Raw

Nov 9, 2011 at 1:26 PM

Hi Raw,

I'm happy to do that, what's the best way to share the code with you?

neilt17

Coordinator
Nov 16, 2011 at 2:51 PM

Hello neilt17.

Unfortunatly I don't know how to share the code with CodePlex.
So, please send your code to matthias.backhaus (at) gmx.de   (replace (at) with @).

Thx alot!

Raw

Nov 20, 2011 at 8:04 AM

Omg! So brilliant to read that someone already did this! Thanks alot Neilt17!

@raw_venture_ad: any ideas when this will be available to the public?

 

Coordinator
Nov 21, 2011 at 7:19 PM

I havent received the code from neilt17. No news so far, sry. :)

Nov 21, 2011 at 7:27 PM

I'm on to it. :-)

Nov 23, 2011 at 10:29 PM
Edited Nov 23, 2011 at 10:30 PM

I thought the best way to share the code was to put it in a project - so I have uploaded it here: http://htmltodocx.codeplex.com/ with a bit of explanation.

Coordinator
Nov 24, 2011 at 3:39 PM

First test looks pretty awesome, dude!! =)

Nov 26, 2011 at 3:02 PM

Thank you :-)

Mar 12, 2012 at 9:15 AM

raw_venture_ad, hello! were the plans to implement htmltodocx to PHPWord realized?

i ask because it's really great script - but i can't use cyrillic symbols(( i'm using utf-8... english, tags, images - great! but i need russian...  i tried to use iconv function - like iconv("utf-8","windows-1251",$data) but it doesn't help...

i would be very pleases if someone has the advice or solution.... 

p.s. PHPWord (clean) works with utf-8 and cyrillic normally

Mar 13, 2012 at 7:26 AM

i've solved the problem almost... utf_decode() helped

Jan 23, 2013 at 2:09 PM

Hello everyone,

I know that this topic is quite old, but is it still under development?

When I study the previous posts ("First test looks pretty awesome, dude!! =)"), it makes me hopeful - but that was long ago ...

(And I can't find any support in the actual beta release) :-(

 

Anyway: GREAT WORK!