Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the html tag string to a word document using PHPWord?

Tags:

html

php

phpword

I have a HTML string and I want to print in the word document in same format and style as in the HTML. I am using the PHPWord

When I give my HTML string say:

$htmlval ="<h6><div style="text-align: center;"><b style=\\"font-size: x-large;\\">OFFER LETTER</b></div><font size=\\"3\\"><div style="text-align: center;"><span style=\\"font-size: medium;\\">(This goes as an email copy)</span></div>";

this is the HTML tags with break and div.

to:

$section->addText($htmlval);

it prints all HTML tag but I want the content with the format specified in the HTML.

like image 940
user2651906 Avatar asked Apr 11 '14 07:04

user2651906


2 Answers

In limited way its possible, but just only for basic html tags.

<p>,<h1>,<h2>,<h3>,<h4>,<h5>,<h6>,<strong>,
<em>,<sup>,<sub>,<table>,<tr>,<td>,<ul>,<ol>,<li>

Its also not work well in more complicated cases (for example rowspan/colspan in tables)

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);

Please check samples on : PHPWord github

like image 151
Tomek Kobyliński Avatar answered Nov 09 '22 05:11

Tomek Kobyliński


I agree with the answer of @Tomek, but you have more html tags possible.

The full list is here :
https://github.com/PHPOffice/PHPWord/blob/4a530d1d97b064b87d9e2a1cc64cab996246569c/src/PhpWord/Shared/Html.php#L116

Don't try to do complicated thinks with table or style. It just doesn't work.

like image 29
Dam Fa Avatar answered Nov 09 '22 05:11

Dam Fa