Forum


Replies: 6   Views: 4369
Add embed html image to header not working
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 ytalexx888  · 09-10-2015 - 20:49

Hi

 

I am attempting to add an image to a header formatted in a table with some text.  The image is to be on the right side, right justified and the text is to be on the left side, left justified.

Everything works except when I add an image.

I have tried to add an embeddedHTML table and I have tried to the addTable method.  Both methods throw an error, by either not displaying the image or claiming that the documents contents are corrupted.

Adding the same image to the body of the document is no problem.

Here are both codes:

EmbedHTML:

$header = new WordFragment($docx, 'defaultHeader');

$hTableValues = "<table><tr><td width=500><span style='font-size: 14pt; font-weight: bold'>".$CASp_number."</span><br /><span style='font-size: 10pt'>".$meta['Name']."<br />".$meta['Description']."</span></td><td align='right' width=500>".$logo."</td></tr></table>";

$header->embedHTML($hTableValues, array('spacingBottom' => 400));

$docx->addHeader(array('default' => $header));

 

The above method yields "Microsoft Office cannot open this file because some parts are missing or invalid" but no errors are logged.

 

addTable:

$header = new WordFragment($docx, 'defaultHeader');

$leftFragment = new WordFragment($docx);

$headerLeft = array();

$headerLeft[] = array('text' => $CASp_number, 'jc' => 'left', 'sz' => 14, 'b' => 'on', 'lineBreak' => 'after', 'lineSpacing' => 120);

$headerLeft[] = array('text' => $meta['Name'], 'jc' => 'left', 'sz' => 10, 'b' => 'on', 'lineBreak' => 'after', 'lineSpacing' => 120);

$headerLeft[] = array('text' => $meta['Description'], 'jc' => 'left', 'sz' => 10, 'b' => 'off');

$leftFragment->addText($headerLeft);
    

$rightFragment = new WordFragment($docx);

$headerImage = array('src' => 'logos/logo-'.$accountDBNUM[0].'.png');

$rightFragment->addImage($headerImage);

    

$tArray = array(array($leftFragment, $rightFragment));

$header->addTable($tArray, array(), array());

$docx->addHeader(array('default' => $header));

 

Using this second method generates the following error: PHP Fatal error:  Call to a member function addFile() on a non-object in /home/adaverac/public_html/phpdocx4/classes/CreateDocx.inc on line 2082

 

I can't seem to figure this problem out, as adding the image to the document yields no problems.  The header generates properly with either method when the image is left out.

 

I don't care which method I use, as long as I am able to get the necessary results.  Any suggestions or thoughts you have would be much appreciated.  Thank you.

Posted by 243127387@qq.com  · 12-10-2015 - 03:21

I also have same problem!

Posted by admin  · 12-10-2015 - 06:38

Hello,

The problem is that you're not setting the correct scope when you instanciate the WordFragment class to add the image. For example:

$headerImage = new WordFragment($docx, 'defaultHeader');

In your code $header has the scope, but for example $headerLeft (among others) doesn't have it.

Please check the included example LayoutAndGeneral/addHeader/sample_2.php. This example example adds an image, a text and a pager to the header and it works perfectly.

Regards.

Posted by ytalexx888  · 12-10-2015 - 22:52

Thank you for your response.  I have added 'defaultHeader' to both $leftFragment and $rightFragment but unfortunately I still get the same error:

PHP Fatal error: Call to a member function addFile() on a non-object in [...] /CreateDocx.inc on line 2082.

Any other ideas?

Posted by admin  · 13-10-2015 - 06:14

Hello,

Please run the example LayoutAndGeneral/addHeader/sample_2.php and check if you get any error and the DOCX output.

If you still have issues please send to contact[at]phpdocx.com the most simple script that illustrates the error. As we need to run it, please don't do external connections (such as databases or webservices).

Regards.

Posted by 243127387@qq.com  · 13-10-2015 - 07:22

Deleted by admin · 13-10-2015 - 07:25

Posted by ytalexx888  · 15-10-2015 - 19:19

Thank you.

The example works fine.  I have copied its form, but I still get an error.  I am sending the script.  Any remarks would be appreciated.