Headers and footers are, of course, an important part of any professional looking business report or document so Docxpresso pays special attention to them.
With the help of Docxpresso you may basically insert any kind of content you wish into your document headers and footers as you will do within the body of your document: pargraphs with text and images, tables, links, etcetera and, of course, fields that may be of specific practical use in headers and footers like dates and/or page numbers.
You may do so with the help of the <header> and <footer> HTML5 tags or by using directly the header and footer methods that we describe in the following.
You may insert different headers and sections for each section of your document see, for example, the section method documentation.
Headers
The core Docxpresso method responsible for the insertion of headers is the header method.
You may customize:
- The header layout properties.
- The header content.
Of course you may also start with a document that includes all the required headers (see Custom templates).
The public API of the header method can be summarized as follows:
Signature
public header ([$options ])
Parameters
- $options (type: array).
This array has the following available keys and values:- section (type: integer), the section number. If not given it is assumed that the header will be inserted in the current section.
- dynamic-spacing (type: boolean, default: false). If true Docxpresso will try to accomodate as best as possible the header properties to the general document
layout. - left-page (type: boolean, default: false). If true the inserted header will be specific of left pages only.
- style (type: string). A string of styles in CSS format.
The relevant header styling parameters include (where ‘*’ stays in this context for top, right, bottom or left:
- background-color: sets the header background color.
- border: shorthand for border properties.
- border-color: sets the border color.
- border-style: sets the border line style.
- border-width: sets the border width.
- border-*-color: sets only the border color of one side.
- border-*-style: sets only the border line style of one side.
- border-*-width: sets only the border width of one side.
- margin: specifies all margin widths in on shot.
- margin-*: specifies one margin at a time.
- min-height: specifies the header minimum height.
- pading: specifies all padding widths in on shot.
- padding-*: specifies one padding at a time.
- height: specifies the header height.
Let us start by inserting a (very) simple header:
<?php /** * This sample script inserts a very simple header in the document */ require_once 'pathToDocxpresso/CreateDocument.inc'; $doc = new DocxpressocreateDocument(); $format = '.pdf';//.pdf, .odt, the direct styling of headers is not supported by Word and RTF formats (style the contents instead) //insert the header with some basic formatting $style = 'min-height: 2cm;'; $style .= 'border: 1px solid red;'; $style .= 'margin-bottom: 0.5cm;'; $style .= 'padding: 0.5cm;'; $style .= 'background-color: #ffff99;'; $doc->header(array('style' => $style)) ->paragraph() ->text(array('text' => 'This is a header.')); $doc->paragraph() ->text(array('text' => 'This document has a very simple header. ')) ->text(array('text' => 'Don't you agree? ')); //include in the render method the path where you want your document to be saved $doc->render('simple_header' . $format); //echo a link to the generated document echo 'You may download the generated document from the link below:<br/>'; echo '<a href="' . 'simple_header' . $format . '">Download document</a>';
MS Word does not allow for direct styling of the header properties, one should include all styling in the header content.
In order to obtain an identical result for Word and RTF we may use the following script instead:
<?php /** * This sample script inserts a very simple header in the document */ require_once 'pathToDocxpresso/CreateDocument.inc'; $doc = new DocxpressocreateDocument(); $format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf //insert the header with some basic formatting $style = 'min-height: 2cm;'; $style .= 'border: 1px solid red;'; $style .= 'margin-bottom: 1cm;'; $style .= 'padding: 0.5cm;'; $style .= 'background-color: #ffff99;'; $doc->header() ->paragraph(array('style' => $style)) ->text(array('text' => 'This is a header.')); $doc->paragraph() ->text(array('text' => 'This document has a very simple header. ')) ->text(array('text' => 'Don't you agree? ')); //include in the render method the path where you want your document to be saved $doc->render('simple_header_word' . $format); //echo a link to the generated document echo 'You may download the generated document from the link below:<br/>'; echo '<a href="' . 'simple_header_word' . $format . '">Download document</a>';
Lets us now insert a somehow more realistic header that incorporates the standard elements, i.e. an image/logo, some text and page numbering:
<?php /** * This sample script inserts a header in the document with an image and page numbering */ require_once 'pathToDocxpresso/CreateDocument.inc'; $doc = new DocxpressocreateDocument(); $format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf //first reduce a little bit the page margin top so the header stands closer to the top page border $doc->pageLayout(array('style' => 'margin-top: 0.5cm')); //insert the header with images and page numbering $headerStyle = array('style' => 'min-height: 3cm'); $rowStyle = array('style' => 'min-height: 2.5cm');//only required for nice .docx conversion $cellStyle = array('style' => 'margin: 0; padding: 0; vertical-align: middle; border-bottom: 1px solid #444444'); $pStyle = array('style' => 'text-align: right; font-family: Arial; font-size: 12pt; margin-right: 10pt;'); $textStyle = 'font-family: Cambria; font-size: 18pt; color: #b70000'; $doc->header($headerStyle) ->table(array('grid' => array('5cm', '5cm', '7cm'), 'style' => 'margin: 0; padding: 0;')) ->row($rowStyle) ->cell($cellStyle)->paragraph()->image(array('src' => 'docxpresso.png', 'style' => 'margin: 0; padding:0')) ->cell($cellStyle)->paragraph()->text(array('text' => 'Docxpresso', 'style' => $textStyle)) ->cell($cellStyle)->paragraph($pStyle)->text(array('text' => 'page '))->field('page-number'); $doc->paragraph() ->text(array('text' => 'This document has a nice header with an image and page number. ')); //include in the render method the path where you want your document to be saved $doc->render('header' . $format); //echo a link to the generated document echo 'You may download the generated document from the link below:<br/>'; echo '<a href="' . 'header' . $format . '">Download document</a>';
Footers
The core Docxpresso method responsible for the insertion of headers is the footer method.
You may customize:
- The footer layout properties.
- The header content.
The public API of the footer method coincides 100% with the one of the header method:
Signature
public footer ($options)
Parameters
- $options (type: array).
This array has the following available keys and values:- section (type: integer), the section number. If not given it is assumed that the footer will be inserted in the current section.
- dynamic-spacing (type: boolean, default: false). If true Docxpresso will try to accomodate as best as possible the footer properties to the general document layout.
- left-page (type: boolean, default: false). If true the inserted footer will be specific of left pages only.
- style (type: string). A string of styles in CSS format.
The relevant CSS properties are exactly the same as the ones for the header method so we will not repeat them again.
A simple footer with just a centered page numbering will read:
<?php /** * This sample script inserts a simple footer with page numbering */ require_once 'pathToDocxpresso/CreateDocument.inc'; $doc = new DocxpressocreateDocument(); $format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf //insert the footer $pStyle = array('style' => 'text-align: center; font-family: Arial; font-size: 12pt;'); $doc->footer() ->paragraph($pStyle) ->text(array('text' => 'page ')) ->field('page-number') ->text(array('text' => ' of ')) ->field('page-count'); $doc->paragraph() ->text(array('text' => 'This document has a footer with the page number. ')); //include in the render method the path where you want your document to be saved $doc->render('footer' . $format); //echo a link to the generated document echo 'You may download the generated document from the link below:<br/>'; echo '<a href="' . 'footer' . $format . '">Download document</a>';