Forum


Replies: 8   Views: 4207
Add background image for first page
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 wwu_ivv_wiwi  · 02-02-2015 - 16:17

When I use addbackgroundimage($img), the image is added to each page, regardless of sections. How can I add the background image only to my first page? Also, how can I scale the Image resp. add width and height? Right now the image is larger than A4, so only the top-left part of the image is shown. I would like to have it fit to page. Thanks in advance! Edit: Using 4.5 Corporate with Namespaces

Posted by admin  · 03-02-2015 - 08:11

Hello, Please try to create the desired content with Word and use it as the base template. Word shares the same background in all pages of the document; you could use the watermarkInsertImage method of the DocxUtilities class to set a custom height and width. Regards.

Posted by wwu_ivv_wiwi  · 03-02-2015 - 09:38

Creating my front page in Word and using it as a template works for the main page, but the next pages afterwards are now missing, although I did not change their contents, no Table of Contents, etc.. This is not the way I want to go. Now I try to add the frontpage-background via the addImage() method, using textWrap = 5. Unfortunately, as soon as I set textWrap = 5, Word cannot read the document anymore (Word 2011, Mac). Code Example: $options = array( 'src' => $backimg, 'scaling' => 100, 'spacingTop' => 0, 'spacingBottom' => 0, 'spacingLeft' => 0, 'spacingRight' => 0, 'textWrap' => 5, ); $docx->addImage($options); Do you know the reason for that or have any advice? Regards

Posted by admin  · 03-02-2015 - 10:28

Hello, We're checking the issue with textWrap. Please try this approach: 1. Generate the first page using Word or phpdocx as a standalone document. 2. Generate a second document with the other pages. 3. Merge them using the MultiMerge class. Regards.

Posted by wwu_ivv_wiwi  · 03-02-2015 - 10:35

That is a good idea and I am able to create the contents in two separate files, which both look fine for themselves. For the merging I call: $frontfile = tempnam('','wiwi_f_'); $front->createDocx($frontfile); $frontfile .= '.docx'; chmod($frontfile, 0644); $mainfile = tempnam('', 'wiwi_m_'); $docx->createDocx($mainfile); $mainfile .= '.docx'; chmod($mainfile, 0644); $merge = new \Phpdocx\BatchProcessing\MultiMerge(); $merge->mergeDocx($frontfile, array($mainfile), $destination, array('preserveStyleDefaults' => false)); Both front and main files are created and stored in the temporary file system (I checked that and DLed the files). Unfortunately the merging does not work out and ends in the following error: Fatal error: Call to a member function appendChild() on a non-object in [...]/libraries/phpdocx-45/Classes/Phpdocx/BatchProcessing/MultiMerge.inc on line 2059 I can send you the two files to merge if you want to try it for yourself.

Posted by admin  · 03-02-2015 - 10:54

Hello, Please send both documents to contact@phpdocx.com. Regards.

Posted by wwu_ivv_wiwi  · 03-02-2015 - 11:06

I'm digging deeper into the problem and I found out that line 762 $this->_firstDocx->open($finalDocument) silently returns error 11 (can't open file). ==> you should add a visible warning here, so users directly know that something with the file paths is wrong. This is actually a problem with Drupal 7, where I use phpdocx with: 'private://file-15-01-09-54d0a7a69d710' is not a path the PHP Zip implementation can resolve. Too bad. using drupal_realpath($destination) in the function call resolves this. Now it works to 95%. The table of contents is not updated, I had to do this manually in word. The ToC is in the second file to merge.

Posted by admin  · 03-02-2015 - 12:19

Hello, The problem with the TOC is that you can't force Word to update it content. The option autoUpdate of the method addTableContents advises (request) Word to update it. But there's no other choice to force it than using a macro. This is due to how Word works. Regards.

Posted by wwu_ivv_wiwi  · 03-02-2015 - 13:11

Thanks for the explanation and the support. I think I'm fine now :)