Forum


Replies: 16   Views: 4547
Header and footer not showing
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 Simco uwkm  · 19-12-2017 - 14:35

$headerImageOptions = array(
        'src' => __DIR__ . '/../../Resources/Public/Images/simco_header.png', 
        'dpi' => 300,  
);
$footerImageOptions = array(
        'src' => __DIR__ . '/../../Resources/Public/Images/simco_footer.png', 
        'dpi' => 300,  
);
$headerImage = new \Phpdocx\Elements\WordFragment($docx, 'defaultHeader');
$headerImage->addImage($headerImageOptions);

$footerImage = new \Phpdocx\Elements\WordFragment($docx, 'defaultFooter');
$footerImage->addImage($footerImageOptions);

$docx->addHeader(array('default' => $headerImage));
$docx->addFooter(array('default' => $footerImage));

it's not showing  there as image, but as red cross with message that probably it has not enough memory to display it there or the image was damaged.

how can it be fixed ?

Posted by admin  · 19-12-2017 - 15:00

Hello,

Please check and run the included examples of adding headers (https://www.phpdocx.com/api-documentation/layout-and-general/insert-headers-Word-document-with-PHP) and footers (https://www.phpdocx.com/api-documentation/layout-and-general/insert-footers-Word-document-with-PHP). Both methods are working perfectly.

You can find the same samples in the package. Also please check that the images have the correct extension for their mime-types (for example you are not adding a JPEG using a PNG extension file).

Please note that addHeader and addFooter methods can be used when creating a DOCX from scratch. If you are using a template you need to set placeholders in the template or import the headers/footers from an existing DOCX (https://www.phpdocx.com/api-documentation/layout-and-general/import-headers-and-footers-Word-document-with-PHP). If you need to mix both (templates and created from scratch) please use the mergeDocx method (https://www.phpdocx.com/api-documentation/docxutilities/merge-Word-documents-with-PHP, https://www.phpdocx.com/documentation/cookbook/headers-and-footers-for-sections).

Regards.

Posted by Simco uwkm  · 19-12-2017 - 15:43

It was showing yesterday but only on last page, after modifying template and replacing placeholders with sections and  embedhtml i get this issue.
Checked  images - mime type is png. Path to image is also correct one.

you can see document here : http://simco-ion.nl/typo3conf/ext/uwkm_offer/Classes/Controller/document.docx
and for template it's: http://simco-ion.nl/typo3conf/ext/uwkm_offer/Resources/Public/Templates/Quotation_front_nl.docx
path to image : http://simco-ion.nl/typo3conf/ext/uwkm_offer/Resources/Public/Images/simco_footer.png

so not really sure what could be else go wrong here.

Posted by admin  · 19-12-2017 - 16:20

Hello,

If you are using a template and then you need to add new content with headers and footers created from scratch, we recommend you to use the generate each DOCX standalone and the use the mergeDocx method:

1. Replace the contents of the template (this is the cover).

2. Create a new DOCX with the new contents, headers, footers.

3. Merge both DOCX using the mergeDOCX method (https://www.phpdocx.com/api-documentation/docxutilities/merge-Word-documents-with-PHP).

There's also an article about this approach (https://www.phpdocx.com/documentation/cookbook/headers-and-footers-for-sections). The addHeader and addFooter methods should only be used for DOCX created from scratch.

Or you could create a single template with the cover and a new page with a placeholder image for the headers and footers.

Regards.

Posted by Simco uwkm  · 20-12-2017 - 08:08

Tested with creating single document and merge them together.
in merged and in single i do have same issue headers and footers showing  same no image.

double checked path and mime type they are correct ones.

Posted by Simco uwkm  · 20-12-2017 - 08:17

if i set image to 72dpi it show me only one on single page, rest of the pages continue to display no image.

my guess here that it runs indeed out of memory although in php.ini i have set it to 2Gb

Posted by admin  · 20-12-2017 - 08:55

Hello,

If PHP throws an out of memory message, it is registered in the log of the server. Do you see that out of memory message in your server logs? Please also test your script using PHP CLI mode.

We have done some tests (doing the merging and working with a single template) with your DOCX and images and everything is working correctly.

Also please run the included samples in the package (LayoutAndGeneral folder: addHeader and addFooter) and check if the images are added correctly.

Regards.

Posted by Simco uwkm  · 20-12-2017 - 09:21

No absolutely nothing in logs,  now made html part for header and tried to insert that as header. 
still image gives me cross.
although if i say not to download images while embedding html word document says that the document is broken.

http://simco-ion.nl/typo3conf/ext/uwkm_offer/Classes/Controller/broken.docx

we use php 7.0 if that makes any difference.

Posted by admin  · 20-12-2017 - 09:23

Hello,

Please run the included samples in the package (LayoutAndGeneral folder: addHeader and addFooter) and check if the images are added and they appear correctly.

The URL you have posted with your DOCX is broken.

About PHP version, phpdocx is fully tested with PHP 5.2 to PHP 7.1 (including all the versions from PHP 5.2: PHP 5.3, PHP 5.4...PHP 7.0 and PHP 7.2 beta).

Regards.

 

Posted by Simco uwkm  · 20-12-2017 - 09:26

modified url to file.. donno from where  but got strange characters there.
http://www.simco-ion.nl/typo3conf/ext/uwkm_offer/Classes/Controller/broken.docx
tested package examples everything works just fine.
http://www.simco-ion.nl/typo3conf/ext/uwkm_offer/Lib/examples/LayoutAndGeneral/addHeader/example_addHeader_2.docx

is it possible that document bugs from the image path ?
http://www.simco-ion.nl/typo3temp/_processed_/e/9/csm_simco_header2_c1a8e5a805.png

Posted by admin  · 20-12-2017 - 09:31

Hello,

Now please run the same scripts included in the package but adding your images. Please change only the path to the image, not how it's being added (just copy the new image and change the path in the script). Please test it using the most simple script: examples/addHeader/sample_1.php

Regards.

Posted by Simco uwkm  · 20-12-2017 - 10:25

now i'm totaly lost, just have this code in my extension:

<?php
namespace UWKM\UwkmOffer\Controller;

class OfferController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{

        public function submitAction()
        {

                require_once(__DIR__ . '/../../Lib/Classes/Phpdocx/AutoLoader.inc');
                \Phpdocx\AutoLoader::load();

                $docx = new \Phpdocx\Create\CreateDocx();
                $footerImageOptions = array(
                        'src' => __DIR__ . '/../../Resources/Public/Images/simco_footer.png', 
                        'dpi'=>300,
                );
                
                $footerImage = new \Phpdocx\Elements\WordFragment($docx, 'defaultFooter');
                $footerImage->addImage($footerImageOptions);
                
                $headerElements = new \Phpdocx\Elements\WordFragment($docx, 'headers');
                
                $headView = $this->offerRepository->getTemplateHtml('Offer', 'header');
                
                $headView->assign('image', __DIR__ .'/../../Resources/Public/Images/simco_header2.png');
                $headView->assign('addressInfo', 'Simco-ION
Aalsvoort 74
7241 MB Lochem
The Netherlands
+31 (0)753 288378
+31 (0)573 288390
cs@simco-ion.nl');

                $headerElements->embedHTML($headView->render(), array('downloadImages'=>true));
                $docx->addHeader(array('first'=>$headerElements, 'default' => $headerElements));
                $docx->addFooter(array('first'=>$footerImage,'default' => $footerImage));
                $docx->createDocxAndDownload(__DIR__.'/document.docx');
        }
}


wel resulting docx is without images here. is it bugging due script using namespace too ?

Posted by admin  · 20-12-2017 - 10:26

Hello,

OK, we have found the problem. The DOCX you are using as cover has 'Different Odd & Even pages' enabled for headers and footers (<w:evenAndOddHeaders/> tag in the settings.xml file), so when you merge both DOCX (cover and the DOCX with contents), odd pages have the headers but not even pages as the settings.xml from the first DOCX is used.

You just need to change your cover inserting an empty header and disable the option 'Different Odd & Even Pages' in the "Header & Footer Tools Design" tab.

If you use this cover changed and then run this script:

<?php

require_once 'Classes/Phpdocx/Create/CreateDocx.inc';

$docx = new Phpdocx\Create\CreateDocx();

$imageOptions = array(
        'src' => 'simco_footer.png',
        'dpi' => 300,
);

$headerImage = new Phpdocx\Elements\WordFragment($docx, 'defaultHeader');
$headerImage->addImage($imageOptions);
$docx->addHeader(array('default' => $headerImage));

$docx->addText('Page.');

$docx->addBreak(array('type' => 'page'));

$docx->addText('Page.');

$docx->createDocx('output');

$merge = new Phpdocx\Utilities\MultiMerge();
$merge->mergeDocx('Quotation_front_nl.docx', array('output.docx'), 'output_merge.docx', array());

The header will appear in all pages but the cover one.

About your code, you are using a wrong target (headers is not a valid value):

$headerElements = new \Phpdocx\Elements\WordFragment($docx, 'headers');

Regards.

Posted by Simco uwkm  · 20-12-2017 - 10:50

made standalon script to test the code it does work just fine.. but when it's placed within extension it gives me those bugs.. 

things are really weird.. 

Posted by admin  · 20-12-2017 - 10:56

Hello,

Sorry but if it's working standalone, we have tested with the change in your DOCX explained on our previous update and the output is correct, then there must be something wrong about how it's being used/integrated with the CMS that we can't guess.

The namespaces package is fully tested with CMS such as WordPress and Drupal and frameworks (Symfony, Laravel, Yii, CakePHP...). Type3 should work fine too, and we are aware that other users are using phpdocx namespaces with Type3 without issues.

We recommend you try using the included samples in your Controller, maybe it can't read the images due to some missing rw access.

Regards

Posted by Simco uwkm  · 20-12-2017 - 11:23

found out what was wrong with it.. it didn't like 

$docx->addHeader(array('first'=>$headerElements,'default' => $headerElements));

now have just :

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

and that works just fine.