The pageLayout method of Docxpresso offers you complete control over your document layout.

You may customize:

  • The paper size. You may use a standard paper size like A4 (default), A3, legal or letter or define explicit width and height properties in cm, mm, inches or points.
  • The paper orientation: portrait (default) or landscape.
  • The number of columns together with their gap and separator rule if any.
  • The background color and/or image.
  • Page borders, margins and paddings.
  • Text direction.

You may set this properties globally for the whole document or for any of its sections.

if you prefer not to bother you may also start with a template with all the required set of properties (see Custom templates).

The public API of the pageLayout method can be summarized as follows:

Signature

public pageLayout ($options)

Parameters

  • $options (type: array). This array has the following available keys and values:

    • orientation (type: string, default: portrait). The possible values are: portrait and landscape.
    • paperSize (type: string, default: A4). The possible values are: A4, A3, letter and legal. These values are overriden, if set, by the width and height
      style options.
    • section (type: integer). If set the layout will be only applied to the selected section otherwise the layout will be applied globally to all the document sections.
    • style (type: string). A string of styles in CSS format.

Document size and orientation

If you want, as it is more probable, that your document has a standard paper size you just need to set the orientation and paperSize options to any of the predefined values enumerated above, for example:

<?php
/**
 * This sample script sets the document layout to be A3 landscape
 */
require_once 'pathToDocxpresso/CreateDocument.inc';
$doc = new DocxpressoCreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//set the required options
$options = array(
    'orientation' => 'landscape',
    'paperSize'   => 'A3',
    );
$doc->pageLayout($options);
$doc->paragraph()
    ->text(array('text' => 'A simple A3-landscape document.'));
//include in the render method the path where you want your document to be saved
$doc->render('simple_A3_landscape' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'simple_A3_landscape' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

You may have, from time to time, the need to generate a document with a non standard size. Fortunately, that is equally straightforward to do so by a judicious use of the width and height CSS properties. Explicitely:

<?php
/**
 * This sample script sets the document size to a squared non-standard format
 */
require_once 'pathToDocxpresso/CreateDocument.inc';
$doc = new DocxpressoCreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//set the required options
$options = array(
    'style' => 'width: 20cm; height: 20cm',
    );
$doc->pageLayout($options);
$doc->paragraph()
    ->text(array('text' => 'A simple "squared" document of 20x20 cm.'));
//include in the render method the path where you want your document to be saved
$doc->render('simple_squared' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'simple_squared' . $format . '">Download document</a>'; 

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

Notice that the paperSize option is overridden whenever we explicitly set a width and heightfor the document.

Multicolumn documents

One may create whole documents or document sections with more than one column and style them via the corresponding CSS properties. Docxpresso supports the following features:

  • column-count: sets the number of columns.
  • column-gap: sets the gap between columns.
  • column-rule: shorthand property for the column-rule-* properties.
  • column-rule-color: sets the color of the rule between columns.
  • column-rule-style: sets the style of the rule between columns.
  • column-rule-width: sets the width of the rule between columns.
  • columns: shorthand property for column-width and column-count.

If we want, for example, to generate a A4 document with two columns and with a vertical line separating them we may use the following code:

<?php
/**
 * This sample script sets the document size to A4 with two columns
 * separated by a solid black line
 */
require_once 'pathToDocxpresso/CreateDocument.inc';
$doc = new DocxpressoCreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//set the required options
$options = array(
    'style' => 'column-count: 2; column-rule: 1pt solid black',
    );
$doc->pageLayout($options);
//include the following paragraph 40 times
for ($j = 0; $j < 40; $j++) {
$doc->paragraph()
    ->text(array('text' => 'This text should show up in a two column layout.'));
}
//include in the render method the path where you want your document to be saved
$doc->render('two_columns' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'two_columns' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

Document background

One may specify the background properties of a document or document section using the following standard CSS properties:

  • background: shorthand for background properties.
  • background-color: sets the document background color.
  • background-image: sets the document background image.
  • background-position: sets the position of the background image.
  • background-repeat: specifies if the background image will repeat or not.

In order to create a document with a PDF logo as background one should only do the following:

<?php
/**
 * This sample script generates a document with a centered background image
 */
require_once 'pathToDocxpresso/CreateDocument.inc';
$doc = new DocxpressoCreateDocument();
$format = '.pdf';//.pdf, .odt, not supported for other document formats
//set the required options
$options = array(
    'style' => 'background-image: url(PDF_logo.jpg); background-position: center center; background-repeat: no-repeat',
    );
$doc->pageLayout($options);
$doc->paragraph()
    ->text(array('text' => 'A document with a centered background image.'));
//include in the render method the path where you want your document to be saved
$doc->render('background_image' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'background_image' . $format . '">Download document</a>';  

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

This feature is not yet supported for Word and RTF documents.

Page margins and borders

By now it should be pretty obvious how to set up document or section margins and borders with the corresponding CSS properties.

Beware that if a standard paperSize is chosen Docxpresso will automatically apply its default margins so there is no need to bother specifying them separately.

The parsed CSS properies include (where ‘*’ in this context stands for top, right, bottom or left):

  • 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.
<?php
/**
 * This sample script generates a document custom margins and borders
 */
require_once 'pathToDocxpresso/CreateDocument.inc';
$doc = new DocxpressoCreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//set the required options
$options = array(
    'style' => 'margin: 1cm 0.5cm; border: 1pt solid #b70000',
    );
$doc->pageLayout($options);
$doc->paragraph()
    ->text(array('text' => 'A document with custom margins and borders.'));
//include in the render method the path where you want your document to be saved
$doc->render('margins_borders' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'margins_borders' . $format . '">Download document</a>';  

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

MS Office Word will ignore page margins bigger than 30pt as one may check in the Word interface. The reasons for that are unknown to us.

Further customizations

Besides all the above one may also set some other additional document wide properties via CSS, namely:

  • padding: specifies all padding widths in on shot.
  • padding-*: specifies one padding at a time.
  • direction or writing-mode: sets the text direction.

For example, if we wish to include some padding in the previous example so the document content is not so close to the page border we may modify the script as follows:

<?php
/**
 * This sample script generates a document custom margins and borders
 */
require_once 'pathToDocxpresso/CreateDocument.inc';
$doc = new DocxpressoCreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//set the required options
$options = array(
    'style' => 'margin: 1cm 0.5cm; padding: 2cm 1cm; border: 1pt solid #b70000',
    );
$doc->pageLayout($options);
$doc->paragraph()
    ->text(array('text' => 'A document with custom margins and borders with an additional padding.'));
//include in the render method the path where you want your document to be saved
$doc->render('margins_borders_paddings' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'margins_borders_paddings' . $format . '">Download document</a>';   

DOWNLOAD: download pdfdownload docdownload docxdownload odtdownload rtf