PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.22k stars 2.69k forks source link

Ability to insert formatted html chunks to template. #110

Open egig opened 10 years ago

egig commented 10 years ago

A request: It'll be great if we can insert html chunk to doc template, which is urgently I need to do now. e.g:

$doc = $PHPWord->loadTemplate('Template.docx');

$doc->setValueFromHtml('SomePlaceholder', '<h1>some value</h1>');

Is there anyone know how to do this, just for now ? by hack or something ? Thanks.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/7455450-ability-to-insert-formatted-html-chunks-to-template?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github).
ghost commented 10 years ago

Hi, @egig, how about using PHPWord_Template.setValue(...)?

egig commented 10 years ago

@RomanSyroeshko I can't, it generates errors.

For those who probably want to help, I create the question: http://stackoverflow.com/questions/22292195/insert-html-to-docx

ivanlanin commented 10 years ago

@egig I think what you need is a parser that can interpret HTML tags into suitable PHPWord element, e.g. <h1> to Title and <b> to bold property of Font. We don't have it yet, though I think this is good to have. PHPExcel already has an HTML Reader. Perhaps you can tweak it for your own needs. Sorry I can't help much right now.

egig commented 10 years ago

@ivanlanin yes, that is it, actually. Never mind, I'll take a look into the excel reader. it could be a solution. Thanks for the help.

calita78 commented 9 years ago

Check this solution: http://stackoverflow.com/questions/27312321/solved-phpoffice-phpword-loadtemplate-not-rendering-word-as-it-should .... i've tested with success, using https://h2openxml.codeplex.com/

GarryOne commented 8 years ago

The other answers, propose H2OXML which support just

Bold, italic and underlined text

Bulled lists

from their docs and last update was in 2012, which wasn't a good option for me.

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.

pascaldls commented 6 years ago

has this gone any where really need this function and think this library need it as well

kindrosker commented 3 years ago

You can add html to table cell, see example:

$value = '<b>TEST</b>';
$wordTable = new \PhpOffice\PhpWord\Element\Table();
$wordTable->addRow();
$cell = $wordTable->addCell();                                
 \PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value);

$templateProcessor->setComplexBlock($block_id, $wordTable);
Warafux commented 3 years ago

You can add html to table cell, see example:

$value = '<b>TEST</b>';
$wordTable = new \PhpOffice\PhpWord\Element\Table();
$wordTable->addRow();
$cell = $wordTable->addCell();                                
 \PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value);

$templateProcessor->setComplexBlock($block_id, $wordTable);

Omg thank you very much. Never though of using a table... I've used everything but a table! Thank you very much sir.

adamwiguna commented 2 years ago

You can add html to table cell, see example:

$value = '<b>TEST</b>';
$wordTable = new \PhpOffice\PhpWord\Element\Table();
$wordTable->addRow();
$cell = $wordTable->addCell();                                
 \PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value);

$templateProcessor->setComplexBlock($block_id, $wordTable);

Hi, i have issue if use Numbering Error Call to a member function addNumberingStyle() on null

Please help sir, Thanks

albertolarah commented 2 years ago

You can add html to table cell, see example:

$value = '<b>TEST</b>';
$wordTable = new \PhpOffice\PhpWord\Element\Table();
$wordTable->addRow();
$cell = $wordTable->addCell();                                
 \PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value);

$templateProcessor->setComplexBlock($block_id, $wordTable);

Hi, i have issue if use Numbering Error Call to a member function addNumberingStyle() on null

Please help sir, Thanks

I have the same problem ¿do you found a solution?

YummiK commented 1 year ago

You can add html to table cell, see example:

$value = '<b>TEST</b>';
$wordTable = new \PhpOffice\PhpWord\Element\Table();
$wordTable->addRow();
$cell = $wordTable->addCell();                                
 \PhpOffice\PhpWord\Shared\Html::addHtml($cell,$value);

$templateProcessor->setComplexBlock($block_id, $wordTable);

Hi, i have issue if use Numbering Error Call to a member function addNumberingStyle() on null Please help sir, Thanks

I have the same problem ¿do you found a solution?

You must create a table from the Section object:

$phpWord = new PhpWord();
$section = $phpWord->addSection();
$wordTable = $section->addTable();
$wordTable->addRow();
$cell = $table->addCell();
$value = str_replace('&', '&amp;', $value); // Fixed file error with symbol "&"
Html::addHtml($cell, $value);

$templateProcessor->setComplexBlock($block_id, $wordTable);