Headings are just a particular kind of paragraph to which we associate an outline level, so one should expect that the API of the corresponding heading method is almost identical to the one of the paragraph method but for the appearance of an extra mandatory level option.

Headings in Docxpresso are internally bookmarked so they can be recognized by a Table of Contents (see Table of Contents), if you choose to include one.

Its public API is given by:

Signature

public heading ($options)

Parameters

  • $options (type: array). This array has the following available keys and values:
    • level (type: integer). A positive integer smaller or equal to 6 specifying the required outline level. This parameter is mandatory.
    • text (type: string). An optional text (it can also be included via the text method).
    • style (type: string). A string of styles in CSS format.

The relevant CSS style type styles are exctly the same as the ones for the paragraph method so we refer the reader to that for further details (see Paragraphs).

In this case a single example would be enough to understand the inner workings of this method:

<?php
/**
 * This sample script inserts a series of headings
 */
require_once 'pathToDocxpresso/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//insert a level 1 heading and some text
$title_1 = 'This is the first title';
$text_1 = 'Some plain text to fill out the space.';
$doc->heading(array('level' => 1, 'text' => $title_1));
$doc->paragraph(array('text' => $text_1));
//insert a level 2 heading and some text
$title_2 = 'Subtitle of level 2';
$text_2 = 'Some more text with no special meaning at all.';
$doc->heading(array('level' => 2, 'text' => $title_2));
$doc->paragraph(array('text' => $text_2));
//insert a new level 1 heading but in a new page
$title_3 = 'This is the second title';
$text_3 = 'This is the last paragraph.';
$doc->heading(array('level' => 1, 'text' => $title_3))->style('page-break-before: always;');
$doc->paragraph(array('text' => $text_3));
//include in the render method the path where you want your document to be saved
$doc->render('headings' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'headings' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

By default headings of level one are not shown in a new page so you must explicitely request via the page-break-before style if that is your preferred layout option.
This example use the default style options, of course, you may also use your own styles at your will.