Forum


Replies: 3   Views: 3205
How do i deal with html in a word fragment please
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by maxelcat  · 16-10-2017 - 10:47

Hi

I am trying to produce a report which is based on three pieces of data, 1 of which has html in it. Altogether the same 3 pieces of data crop up about 20 times and so i am creating an array of arrays like this:

Array ( [SUBJECT] => Relationships and Living in the Wider World [DESCRIPTION] =>

<p>Pupils respond.....
[GRADE] => P6c ) 
Array ( [SUBJECT] => Independence and Personal Wellbeing [DESCRIPTION] =>

<p>Pupils carry ....
[GRADE] => P6b ) 
Array ( [SUBJECT] => Attention Motivation and Self Reflection [DESCRIPTION] => <p>Pupils...

etc

I then create on big wordfragment like this:

foreach ($tabledata as $value){
                                
                        
                                $titlewf->addtext($value['SUBJECT'].' '.$value['GRADE'],
                                                                 
                                                                 array('bold'=>'true',
                                                                          'color'=>'2e74b5',
                                                                           'font'=>'Arial',
                                                                           'fontSize'=>'12'
                                                                          )
                                                                 
                                                                 );
                                
                                
                                $titlewf->addtext($value['DESCRIPTION'],
                                                                          array('bold'=>0,
                                                                                   'color'=>'0ff000',
                                                                                   'font'=>'Arial',
                                                                                   'fontSize'=>'10'
                                                                                   )
                                                                          );
                                
                        
                        } //foreach

this all works fine - the data is printed out to the worddoc. The trouble is that the html code prints out rather than adds the styles. It is only very simple html like <p>, <strong> and <ul>

I don't really know how to handle the fact that $value['DESCRITPION'] was created via an editor and therefore contains HTML.

I am sure this is easy - but I can't see how to do it

This is a link to a sample of the worddoc produced so you can see the issue.

Many thanks in advance - though this is a 'demo' version, I have bought a licence.

Posted by admin  · 16-10-2017 - 11:42

Hello,

The addText method adds text strings into documents, but if you need to transform HTML to Word you need to use the embedHTML method (this method works standalone and as WordFragment). Please check that PHP Tidy is installed and enabled on your server to import HTML and CSS.

Regards.

Posted by maxelcat  · 16-10-2017 - 15:07

Thank you for your reply. I have used check.php - tidy is showing green. I have made some progress but there is still something not right (I think)

Here is some of my code showing how I produce the document

foreach ($tabledata as $value){
                                $titlewf->addtext($value['SUBJECT'].' '.$value['GRADE'],
                                                                 
                                                                 array('bold'=>'true',
                                                                          'color'=>'2e74b5',
                                                                           'font'=>'Arial',
                                                                           'fontSize'=>'12'
                                                                          )
                                                                 
                                                                 );
                                $DESCRIPTION = $value['DESCRIPTION'];
                                $titlewf->embedHTML('<div>'.$DESCRIPTION.'</div>');      
                        } //foreach
                        
                        
                        $docx->replaceVariableByWordFragment(array(
                                'SUBJECT' => $titlewf, 
                                'GRADE'=>$titlewf
                        
                        ), array('type' => 'block'));
                        
                        
                        
                        $docx->createDocx('../wordreports/fullreport/'.$filename);
                

I have managed to get the html code to embed - so that's great.

However, I noticed that I have to wrap the $DESCRITPION in <div> tags (as you can see - <p> tags work too). If I don't, then no word documents are produced, though no error code shows.

I checked in the database where the descriptions are located - and the are wrapped in well formed html.

Somethign doesn't 'feel' right to me.

Am I doing something wrong?

Thanks in advance

Posted by admin  · 17-10-2017 - 06:58

Hello,

The code seems correct, but maybe the HTML tags are escaped in the database or any other issue. We recommend you to run the included sample in the examples/Core/embedHTML folder and the documentation available on

https://www.phpdocx.com/documentation/introduction/html-to-word-PHP

to find the source of the issue. Although there's no problem if you wrap your HTML using a root tag such as div or p, there's no need to do it when transforming HTML to DOCX.

Regards.