Paragraph style

ParagraphStyle is used to change some properties of a paragraph, like line-height, text alignment, etc.

We create a paragraph style by calling the constructor with a name for the style, then we pass the style created to the constructor of Paragraph.

$pStyle = new ParagraphStyle('myPStyle');
$p = new Paragraph($pStyle);

Line height

Using the method setLineHeight($lineHeight), we can specify a fixed line height either as a length or a percentage that relates to the highest character in a line. The value StyleConstants::NORMAL activates the default line height calculation.

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setLineHeight('1cm');

$p = new Paragraph($pStyle);
$p->addText('Suspendisse eros enim, auctor nec pretium at, porttitor et odio. Sed quis nisl rhoncus, convallis lectus eu, tincidunt arcu. Etiam sed lectus nisi. Fusce consequat pulvinar massa, ac ornare augue tincidunt sit amet. Donec at tincidunt dolor. Duis nec tellus sodales, euismod lorem ac, interdum nibh. Curabitur felis nisl, tincidunt at faucibus in, tincidunt eget eros.');

Line distance

Using the method setLineDistance($distance), we can specify a fixed distance between two lines.

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setLineDistance('1cm');

$p = new Paragraph($pStyle);
$p->addText('Suspendisse eros enim, auctor nec pretium at, porttitor et odio. Sed quis nisl rhoncus, convallis lectus eu, tincidunt arcu. Etiam sed lectus nisi. Fusce consequat pulvinar massa, ac ornare augue tincidunt sit amet. Donec at tincidunt dolor. Duis nec tellus sodales, euismod lorem ac, interdum nibh. Curabitur felis nisl, tincidunt at faucibus in, tincidunt eget eros.');

Text alignment

We set the horizontal alignment of the text using setTextAlign($textAlign). The possible values of the $textAlign argument are: StyleConstants::(START, END, LEFT, RIGHT, CENTER, JUSTIFY)

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setTextAlign(StyleConstants::CENTER);

$p = new Paragraph($pStyle);
$p->addText('Suspendisse eros enim, auctor nec pretium at, porttitor et odio. Sed quis nisl rhoncus, convallis lectus eu, tincidunt arcu. Etiam sed lectus nisi. Fusce consequat pulvinar massa, ac ornare augue tincidunt sit amet. Donec at tincidunt dolor. Duis nec tellus sodales, euismod lorem ac, interdum nibh. Curabitur felis nisl, tincidunt at faucibus in, tincidunt eget eros.');

Keep together

Using the method setKeepTogether(), we can keep a whole paragraph together on the same page or column, if it is for example on the end of a page.

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setKeepTogether();

$p = new Paragraph($pStyle);
$p->addText('Ut fermentum, nulla et aliquam lobortis, metus risus euismod mauris, eu porttitor quam arcu in nisl. In hac habitasse platea dictumst. In et sollicitudin ante, eget mollis lacus. Vivamus condimentum blandit diam, vel interdum dui facilisis sed. Fusce lobortis ac lacus sit amet adipiscing. Sed feugiat justo nisi, eget sollicitudin urna congue ac. Vestibulum eu lacus in libero suscipit porta. Suspendisse non mauris ut est aliquam sodales eu eu diam. Aenean vestibulum bibendum purus at sagittis. Aliquam sit amet mi venenatis, porta dolor non, vulputate leo. Etiam id risus sapien. Nam sit amet porttitor magna, tempor consectetur justo. Integer dapibus erat est, non tristique metus ornare a. Nullam sed lacus at sem scelerisque porta tempus id erat. Donec nunc orci, sodales eget condimentum ac, vehicula in risus. Nullam ultrices iaculis tellus et congue.');

Paragraph widows

The method setWidows($widows) specifies the minimum number of lines allowed at the top of a page to avoid paragraph widows. This means that if there is a page break in a paragraph, there will be $widows lines in the next page.

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setWidows(4);

Paragraph orphans

The method setOrphans($orphans) specifies the minimum number of required at the bottom of a page to avoid paragraph orphans. This means that if there is a page break in a paragraph, there must be at least $orphans lines in the current page, or the paragraph will be changed to meet this condition.

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setOrphans(4);

Horizontal margins

We can set the left & right margins for a paragraph using setHorizontalMargins($leftMargin, $rightMargin).

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setHorizontalMargins('1cm', '2cm');

Vertical margins

We can set the top & bottom margins for a paragraph using setVerticalMargin($topMargin, $bottomMargin).

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setVerticalMargin('1cm', '2cm');

Break before

To insert a page or column break before a paragraph, we use setBreakBefore($breakBefore). To insert a page break, we use StyleConstants::PAGE, or a column break with StyleConstants::COLUMN.

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setBreakBefore(StyleConstants::PAGE);

Break after

To insert a page or column break after a paragraph, we use setBreakAfter($breakAfter). To insert a page break, we use StyleConstants::PAGE, or a column break with StyleConstants::COLUMN.

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setBreakAfter(StyleConstants::PAGE);

Background color

We can set the background color of a paragraph using setBackgroundColor($color).

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setBackgroundColor('#ff22cc');

Background image

We can set an image as the background of a paragraph using setBackgroundImage($image, $repeat, $position).

  • $image: path of the image.
  • $repeat (optional): whether the image should be repeated or not, or stretched. default value is repeated. This can be set using one of these values StyleConstants::(REPEAT, NO_REPEAT, STRETCH).
  • $position (optional): position of the image StyleConstants::(LEFT, RIGHT, CENTER, TOP, BOTTOM).
$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setBackgroundImage('myImage.jpg', StyleConstants::STRETCH);

Text indent

We can specify a positive or negative indent for the first line of a paragraph using setTextIndent($textIndent).

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setTextIndent('1cm');

Border

We can set a border to a paragraph using setBorder($borderColor, $borderStyle, $borderWidth).

  • $borderColor (optional): The border color. It is black by default.
  • $borderStyle (optional): Is it a single or double line StyleConstants::(SOLID|DOUBLE).
  • $borderWidth (optional): Can be a length, or one of these values: StyleConstants::(THIN|THICK|MEDIUM).
$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setBorder();
$pStyle->setBorder('#ff0000', StyleConstants::DOUBLE, '4mm');

We can also apply the border to only one side of the paragraph, using one of these methods:

  • setTopBorder($borderColor, $borderStyle, $borderWidth)
  • setBottomBorder($borderColor, $borderStyle, $borderWidth)
  • setLeftBorder($borderColor, $borderStyle, $borderWidth)
  • setRightBorder($borderColor, $borderStyle, $borderWidth)

Padding

We can specify the padding (spacing around a paragraph) using setPadding($padding).

Note that this property won't apply unless there is a border at the same side of the padding.

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setBorder('#ffffff');
$pStyle->setPadding('1cm');

Like borders, you can specify a padding to a side of the paragraph only, using one of these methods:

  • setTopPadding($padding)
  • setBottomPadding($padding)
  • setLeftPadding($padding)
  • setRightPadding($padding)

Keep with next

We can keep the current paragraph and the next paragraph together on a page or in a column after a break is inserted between them, using setKeepWithNext().

$pStyle = new ParagraphStyle('myPStyle');
$pStyle->setKeepWithNext();