PHPWord
PHPWord copied to clipboard
Unable to have different margins for the header/footer and the rest of the page
Hello. I am struggling to use PHPWord to accomplish a layout that I need. I am hoping to have a page with a header and footer that have no margins and are full width. I would like the rest of the page to have typical 1" margins on the left and the right. Here is a representation of what I am trying to accomplish:
I am able to create the header and footer w/ no margins by doing the following:
$section = $this->phpWord->addSection(['marginLeft' => 0, 'marginRight' => 0, 'marginTop' => 0, 'marginBottom' => 0, 'headerHeight' => 0, 'footerHeight' => 0]);
$header = $section->addHeader();
$table = $header->addTable();
$table->addRow();
$header_left = $table->addCell(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(7.66), ['valign' => 'bottom']);
$header_left->addImage($themePath . '/images/proposals/header.png', ['width' => \PhpOffice\PhpWord\Shared\Converter::inchToPoint(7.66)]);
$header_right = $table->addCell(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(0.84), ['bgColor' => 'DDE1EC', 'valign' => 'center']);
$header_right->addPreserveText('{PAGE}', ['bold' => true, 'size' => 11], ['align' => 'center']);
What I'm not able to do is then have regular 1" margins for the rest of the page. I am also able to make the footer work the way I would like using code similar to the above code. Any help you can provide is appreciated.
Hi @eoleary , have you a Word generated document with this layout ?
@Progi1984 I am not able to generate this layout. I am able to generate a page with no margins and I am able to generate a page with 1" margins. What I cannot accomplish is a page where the header and the footer have no margins but the body of the page have 1" margins.
Here is an example of the document I am currently able to create: proposal-example.docx
Here is an example of what I would like to create: proposal layout with margins.docx
I'm really struggling with this issue. I need to know if it is possible to accomplish this. Any insights / help would be deeply appreciated.
Hi @eoleary and @Progi1984, is there any news regarding this issue? I'm struggling with the same.
Here's what I've achieved so far:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection([
'paperSize' => 'A4',
'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(0),
'marginRight' => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(0),
"headerHeight" => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(0),
"footerHeight" => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(0),
]);
// Add header for all other pages
$subsequent = $section->addHeader();
$subsequent->addImage(__DIR__.'/assets/header.jpg', array(
'width' => 595,
'height' => 120,
'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::LEFT
));
// Add footer for all other pages
$subsequent = $section->addFooter();
$subsequent->addImage(__DIR__.'/assets/footer.png', array(
'width' => 595,
'height' => 120,
'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::LEFT
));
\PhpOffice\PhpWord\Shared\Html::addHtml($section, "<p style='font-size: 14pt; font-family: Calibri;'>Test <strong>Content</strong></p>");
// Save the file
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save(__DIR__ . '/tempFile.docx');
And here's the result:
I've tried to create two sections, one for the header and the footer, and the other for my HTML content, but by doing this, it creates a second page.
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection([
'paperSize' => 'A4',
'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(0),
'marginRight' => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(0),
"headerHeight" => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(0),
"footerHeight" => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(0),
]);
// Add header for all other pages
$subsequent = $section->addHeader();
$subsequent->addImage(__DIR__.'/assets/header.jpg', array(
'width' => 595,
'height' => 120,
'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::LEFT
));
// Add footer for all other pages
$subsequent = $section->addFooter();
$subsequent->addImage(__DIR__.'/assets/footer.png', array(
'width' => 595,
'height' => 120,
'alignment' => \PhpOffice\PhpWord\SimpleType\Jc::LEFT
));
$section = $phpWord->addSection([
'marginLeft' => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(5),
'marginRight' => \PhpOffice\PhpWord\Shared\Converter::cmToTwip(5),
]);
\PhpOffice\PhpWord\Shared\Html::addHtml($section, "<p style='font-size: 14pt; font-family: Calibri;'>Test <strong>Content</strong></p>");
// Save the file
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save(__DIR__ . '/tempFile.docx');
Here's what I get:
@hadran9 This is exactly where I am now. The margins are all or nothing in the document.
The issue seems to be that margins can only be applied to sections and you cannot have more than one section on a page. I'm really stuck on this and would really appreciate any help.
Any updates on this issue?
Hi @vatsake, Unfortunately, I had to change the approach as I couldn't find a viable solution, leading me to abandon the initial idea.
@hadran9 Any updates on this issue?
Hi @wpindia007, Unfortunately not, don't think there's a solution for this, and I've abandoned the idea since it wasn't worth wasting that much time on it. But if I recall correctly, it wasn't technically possible on the version I had, don't know if they changed the codebase to support this.