Forum


Replies: 3   Views: 318
Remove left and right margins in header / footer
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 InsaneMe  · 17-04-2025 - 14:40

Hello,

I'm having an issue.

I'd like to create a DOCX file but remove the margins of the header and footer.

The goal is to be able to modify them dynamically — for example, 5px on the right, 2px on the left, or no margins at all (no top, bottom, or right margins, etc.).

That way, I could, for example, position an image at the very left and right edges, allowing me to add an image that appears in the corner of the page.

I've gone through the documentation, spent a lot of time on this, and haven't gotten any results.

At the moment, I'm testing with an image since I also need that functionality, but I haven't had any success.

Here's a simple code example:

 

      $docx = new CreateDocx();

        $docx->modifyPageLayout('A4', [
            'headerMargin' => 0,
            'topMargin' => 0,
            'rightMargin' => 0,
            'leftMargin' => 0,
            'bottomMargin' => 0,
        ]);

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

        $emptyFragment = new WordFragment($docx, 'defaultHeader');
        $emptyFragment->addText('', ['fontSize' => 1]);

        $imageFragment = new WordFragment($docx, 'defaultHeader');
        $imageFragment->addImage([
            'src' => 'data:image/jpeg;base64,' . self::BASE64,
            'scaling' => 30,
            'textWrap' => 0,
        ]);

        $header->addTable(
            [[$emptyFragment, $imageFragment]],
            [
                'border' => 'none',
                'columnWidths' => [9900, 100],
                'tableAlign' => 'right',
                'tableWidth' => ['type' => 'pct', 'value' => 100],
                'cellMargin' => 0,
                'cellPadding' => 0,
                'indent' => 0,
                'offset' => 0,
                'cellSpacing' => 0,
                'borderWidth' => 0,
            ]
        );

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

This code is just a hardcoded test, because I've tried so many things, whether using a table in the header or embedHTML,and I still don’t know how to make it work. Or even if it’s actually possible to do.

Thank you in advance for your help! Feel free to ask if you need more information.