Convert HTML code to doc using PHP and PHPWord
Asked Answered
B

4

6

I am using PHPWord to load a docx template and replace tags like {test}. This is working perfectly fine.

But I want to replace a value with html code. Directly replacing it into the template is not possible. There is now way to do this using PHPWord, as far as I know.

I looked at htmltodocx. But it seams it will not work either, is it posible to transform a peace of code like <p>Test<b>test</b><br>test</p> to a working doc markup? I only need the basic code, no styleing. but Linebreaks have to work.

Barrelchested answered 6/5, 2015 at 12:35 Comment(0)
C
7

Here is the link to the github. It is working fine Html-Docx-js.

And it is the demo also available here.

Other option is this Link.

 $toOpenXML = HTMLtoOpenXML::getInstance()->fromHTML("<p>te<b>s</b>t</p>");
    $templateProcessor->setValue('test', $toOpenXML);
Catania answered 6/5, 2015 at 12:51 Comment(11)
Thanks for your answer, it works well, however, I only want the code of my tiny mc value.Barrelchested
What do you mean by that ? If you are using any code with free licence you just have to follow the T&C of that licence.Catania
lets say I have the string "<b>test</b>". i want this to be transformed to a doc element, not a document. I will then place this element inside my docx using PHPWord. Placing existing doc elements works, but converting the html to them doesn't.Barrelchested
not clear what where you asking but have tried the link that I gave above ?? here is another link am adding where you can test a working demo of html to doc conversion with same source code.Catania
Yes, I tried it, but I do not want to have a downloadable file but code like bla<w:BR>blaBarrelchested
you just have to explore the code and then you can get that code tooCatania
I thought the tool generates a full document, I have not found a way of getting only the elements in it I need.Barrelchested
I think i should go the altchunks wayBarrelchested
docx is similar to zip file if you rename the extension then you will find a document.xml in it you want the contents of that xml ?Catania
Yes, I know, I was looking for something like this #27312821 I will try HTMLtoOpenXMLBarrelchested
getting error - Fatal error: Uncaught Error: Class "HTMLtoOpenXML" not foundPhysostomous
G
3

The other answers propose H2OXML which only supports

  • Bold, italic and underlined text

  • Bulled lists

As described in their docs and their last update was in 2012.

I did some research and found a pretty nice solution:

$var = 'Some text';
$xml = "<w:p><w:r><w:rPr><w:strike/></w:rPr><w:t>". $var."</w:t></w:r></w:p>";

$templateProcessor->setValue('param_1', $xml);

The above example, shows how would be a striked text. Instead of "w:strike" you can use "w:i" for italic or "w:b" bold, and so on. Not sure if it works on all tags or not.

Girlhood answered 27/7, 2016 at 16:21 Comment(1)
your shared link is broken, kindly update it. thanksPhysostomous
B
2

Thanks for your answer, Varun.

The simple PHP library H2OXML works for me https://h2openxml.codeplex.com/

$toOpenXML = HTMLtoOpenXML::getInstance()->fromHTML("<p>te<b>s</b>t</p>");
$templateProcessor->setValue('test', $toOpenXML);

I can now convert html code to insert it using PHPWord.

Barrelchested answered 7/5, 2015 at 8:27 Comment(1)
your shared link is broken, kindly update it. thanksPhysostomous
N
1

$content = '<p>Test<b>test</b><br>test</p>';

use it before IOFactory::createWriter();

\PhpOffice\PhpWord\Shared\Html::addHtml($section, $content);
Notarize answered 3/7, 2018 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.