Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to insert formatted html chunks to template. #110

Open
egig opened this issue Mar 12, 2014 · 12 comments
Open

Ability to insert formatted html chunks to template. #110

egig opened this issue Mar 12, 2014 · 12 comments

Comments

@egig
Copy link

egig commented Mar 12, 2014

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! We accept bounties via Bountysource.

@ivanlanin ivanlanin mentioned this issue Mar 12, 2014
12 tasks
@Progi1984 Progi1984 added this to the 0.7.3 milestone Mar 12, 2014
@ghost
Copy link

ghost commented Mar 12, 2014

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

@egig
Copy link
Author

egig commented Mar 13, 2014

@RomanSyroeshko I can't, it generates errors.

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

@ivanlanin
Copy link
Contributor

@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
Copy link
Author

egig commented Mar 13, 2014

@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.

@ivanlanin ivanlanin added this to the 1.0.0 milestone Apr 13, 2014
@calita78
Copy link

@GarryOne
Copy link

GarryOne commented Jul 27, 2016

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
Copy link

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

@kindrosker
Copy link

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
Copy link

Warafux commented Mar 7, 2021

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
Copy link

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
Copy link

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
Copy link

YummiK commented Sep 21, 2022

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests