pdflib

FPDF / TCPDF / tFPDF Advanced Multicell User Manual

Installation

evosys21/pdflib can be used with FPDF, TCPDF or tFPDF. Because of this we haven’t added a fixed dependency in the main composer.json file. You need to add the dependency to the PDF generation library of your choice yourself.

To use evosys21/pdflib with FPDF, install it via Composer:

composer require evosys21/pdflib
composer require setasign/fpdf

If you want to use TCPDF:

composer require evosys21/pdflib
composer require tecnickcom/tcpdf

If you want to use tFPDF:

composer require evosys21/pdflib
composer require setasign/tfpdf

Usage

To generate an “Advanced Multicell” the followings are required:

use EvoSys21\PdfLib\Multicell;
use EvoSys21\PdfLib\Fpdf\Pdf; // Pdf extends FPDF

// create the Pdf Object
$pdf = new Pdf();

// do some pdf initialization settings
// $pdf->SetMargins(20, 20, 20);
...

// create the Multicell Object
$multicell = new Multicell($pdf);

// set the style definitions
// `default` is inherited by all other styles
$multicell->setStyle('default', 11, '', '130,0,30', 'helvetica');
$multicell->setStyle('p', 11, '', '130,0,30', 'helvetica');
$multicell->setStyle('b', null, 'B', null, null);

// create the multicells
$multicell->multiCell(0, 5, 'This is a simple cell');
$multicell->multiCell(0, 5, '<p>This is a <b>BOLD</b> text</p>');

Create the Pdf object

Use the provided Pdf classes to create the PDF object.

For example, if you want to use FPDF, use the EvoSys21\PdfLib\Fpdf\Pdf class.

Why? Read here

# use one of the following classes
use EvoSys21\PdfLib\Fpdf\Pdf; // Pdf extends FPDF
// use EvoSys21\PdfLib\TCPDF\Pdf; // Pdf extends TCPDF
// use EvoSys21\PdfLib\tFpdf\Pdf; // Pdf extends tFPDF

// create the Pdf Object
$pdf = new Pdf();

// initialize your pdf
$pdf->SetMargins(20, 20, 20);

Please refer to the FPDF/TCPDF/tFPDF class manual in order to do other initializations.

Create the Advanced Multicell object

// Create the Advanced Multicell Object and inject the PDF object
use EvoSys21\PdfLib\Multicell;
$multicell = new Multicell($pdf);

Tag styling

A tag style is used specify the text display properties. The following properties can be set:

Examples:


// the `default`(if set) style is inherited by all other styles
$multicell->setStyle('default', 11, '', [0, 0, 77], 'helvetica');

// define a tag style using associative array
$multicell->setStyleAssoc('p', ['size' => 11, 'style' => '', 'color' => '130,0,30', 'family' => 'helvetica']);

// define only a few properties
$multicell->setStyleAssoc('b', ['style' => 'B']);
$multicell->setStyleAssoc('fancy-bi', ['style' => 'BI', 'color' => '130,0,30']);

// Set the styles for the advanced multicell using `setStyle`
$multicell->setStyle('p', 11, '', '130,0,30', 'helvetica');
$multicell->setStyle('b', 11, 'B', '130,0,30', 'helvetica');
$multicell->setStyle('i', 11, 'I', '80,80,260', 'helvetica');

Style Inheritance

Styles can be inherited if specified so.

<?php
// you can specify which style should be inherited
// example: define a base "h" style and h1, h2, h3, h4 will inherit h
$multicell->setStyle('h', null, 'B', '203,0,48');
$multicell->setStyle('h1', 16, null, null, null, 'h');
$multicell->setStyle('h2', 14, null, null, null, 'h');
$multicell->setStyle('h3', 12, null, null, null, 'h');
$multicell->setStyle('h4', 11, 'B', null, null, 'h');

// a more simple approach: use the `style` function
$multicell->setStyleAssoc('h1', ['size' => 16], 'h');
$multicell->setStyleAssoc('h2', ['size' => 14], 'h');
$multicell->setStyleAssoc('h3', ['size' => 12], 'h');
$multicell->setStyleAssoc('h4', ['size' => 11, 'style' => 'B'], 'h');

Text Formatting

No formatting

The default PDF formatting is used in this case(font, font-size and color)

// no formatting - the default

//t pdf font, font-size, colors are used
$s = "This is a simple text";
$multicell->multiCell(0, 5, $s);

Simple formatting

// simple formatting
$s = "<p>This is a paragraph</p>";
$multicell->multiCell(0, 5, $s);

Nested tags

In case of nested tags, the styles are NOT inherited

// nested tags
$s = "<p>This is <b>BOLD</b> text, this is <i>ITALIC</i></p>";
$multicell->multiCell(0, 5, $s);

Subscripts and superscripts

Subscript and superscripts can be adjusted with the y attribute. See example:

$s = "<p>The following is <s y='-1'>Subscript</s> and <s y='1'>$multicell->setStyle('default', 11, '', [0, 0, 77], 'helvetica');</s></p>";



Strikethrough

Text strikethrough can be defined using the strike attribute in any tag:

$s = "<p>The following is <n strike=''>Text Strikethrough</n> and <bi strike='.5'>Text Strikethrough bolder line</bi></p>";
$multicell->multiCell(0, 5, $s);


You can create links by using the href attribute in a tag.

$s = "<p>Created by <h1 href='mailto:office@evosys21.ch'>EvoSys21 Office</h1>
<h1 href='https://www.evosys21.ch'>www.evosys21.ch</h1></p>";
$multicell->multiCell(0, 5, $s);

Paragraphs

Paragraphs can be created by using the size attribute and the ~~~ reserved pattern for paragraphs. See the following example:

$s = "<size size='50' >Paragraph Example:~~~</size><font> - Paragraph 1</font>
<p size='60' > ~~~</p><font> - Paragraph 2</font>
<p size='60' > ~~~</p> - Paragraph 2
<p size='70' >Sample text~~~</p><p> - Paragraph 3</p>
<p size='50' >Sample text~~~</p> - Paragraph 1
<p size='60' > ~~~</p><h4> - Paragraph 2</h4>";
$multicell->multiCell(0, 5, $s);

Tag Attributes

href

The href attribute will provide you a link in the pdf document.

<h1 href="www.evosys21.ch">Visit our website</h1>

width

By default, the width of a tag is calculated. Use width to set a fixed width for your tag. No checks are being done for text-overflow!

align

align is to be used only in combination width width, to specify where the text should be aligned.

Valid values are: left|center|right|L|C|R

Example with width and align

<p width="100" align="left"> Align Left </p>
<p width="100" align="center"> Align Center </p>
<p width="100" align="right"> Align Right </p>

size

size is used in case of paragraphs. This value will become deprecated in future versions and width is to be preferred instead.

nowrap

nowrap is used if you the text in a paragraph to break or be separated on a new line

For example, you want the price to always be written on the same line:

$s = "The price is <b nowrap='1'>USD 5.344,23</b>";
$multicell->multiCell(50, 5, $s);

FEATURES

Height Limitations

Max Lines

Limit the number of lines that will be in the multicell

//set a limit to the maximum number of lines 
$multicell->maxLines(10);
$multicell->multiCell(0, 5, "...Some text...");

Max Height

Limit the height that a multicell can have

//set a limit to the maximum number of lines 
$multicell->maxHeight(50);
$multicell->multiCell(0, 5, "...Some text...");

If height limits are hit, the following text is cut-off from the multicell.

Text shrinking

Text and line height will shrink in order to fit into a specific maxHeight or maxLines If the text doesn’t fit the font-size will be decreased with 1 and the line-height with 0.5 units until the text fits.

Notice

There is a possibility that the shrinking fails if the input and shrinking settings are invalid.

For example: you set maxLines to 5 and your input text has 10 EOLs in it $multicell->maxLines(5)

Limit the number of lines that will be in the multicell

$multicell->maxLines(10)->shrinkToFit();
$multicell->multiCell(0, 5, "...Some text...");

Limit the height that a multicell can have

$multicell->maxHeight(50)->shrinkToFit();
$multicell->multiCell(0, 5, "...Some text...");
// shrink, max-height and all cells 
$multicell->maxHeight(50)->shrinkToFit()->applyAll();
$multicell->multiCell(0, 5, "...Some text...");

...
$multicell->reset();

Change shrinking units

$multicell->shrinkToFit()->shrinkFontStep(2)->shrinkLineHeightStep(0.2)->maxHeight(50);
$multicell->multiCell(0, 5, "...Some text...");

...
$multicell->reset();

Apply features to all cells and reset

Features like: maxHeight, maxLines, shrinkToFit, shrinkLineHeightStep, shrinkFontStep are applied to the next cell only.

In order to apply these features to all following cells, use $multicell->applyAll();
In order to reset these features use $multicell->reset();