Create PDF in PHP using FPDF Cell() and Output()

FPDF is a PHP library for creating PDF documents directly from PHP. We can add pages, select fonts, position text inside cells, add borders and colors, create links and control whether the final PDF is displayed, downloaded, saved on the server or returned as a string.

PHP script
    |
    v
FPDF()
    |
    +-- AddPage()
    +-- SetFont()
    +-- Cell()
    |
    v
Output()
    |
    v
PDF document

Install FPDF in a PHP Project Top ↑

FPDF PHP PDF generator

Download the current FPDF package from the official FPDF website:

FPDF Website

Keep fpdf.php and the required FPDF font files in a location accessible to your PHP script.

FPDF PHP file and font directory structure

Important Notice for PHP 8.x Users

Use a current FPDF release when running PHP 8.x. Older FPDF versions can contain code that is incompatible with newer PHP releases.

Create Your First PDF using PHP FPDF Top ↑

The basic sequence is:

  1. Load fpdf.php.
  2. Create an FPDF object.
  3. Add a page.
  4. Select a font.
  5. Add some content.
  6. Send the PDF to the browser.
<?php
require 'fpdf.php';

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$pdf->Cell(80,10,'Hello World!');

$pdf->Output('I','my_file.pdf');
View Hello World PDF Output

AddPage() creates a PDF page, SetFont() selects the font and Cell() places the text inside the document.

FPDF Cell() Parameters Top ↑

FPDF Cell parameters for creating PDF documents

The commonly used Cell syntax is:

$pdf->Cell(width,height,text,border,line,align,fill,link);
width
Width of the cell.
height
Height of the cell.
text
Text displayed inside the cell.
border
Controls the cell border. Use 0, 1 or selected sides such as 'LRT'.
line
Controls the position after the Cell. A value of 0 keeps the next Cell on the same line, while 1 moves to the next line.
align
Use 'L', 'C' or 'R' for left, center or right alignment.
fill
Set to true to use the current fill color.
link
Optional URL or internal PDF link.

The next example adds a border, right-aligns the text and makes the Cell clickable.

<?php
require 'fpdf.php';

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$pdf->Cell(
    80,
    10,
    'Hello World!',
    1,
    0,
    'R',
    false,
    'https://www.plus2net.com'
);

$pdf->Output('I','my_file.pdf');
View Cell with Border, Alignment and Link

Add Background Color using SetFillColor() Top ↑

SetFillColor() defines the RGB background color used by filled cells.

$pdf->SetFillColor(1,255,255);

$pdf->Cell(
    80,
    10,
    'Hello World!',
    1,
    0,
    'R',
    true,
    'https://www.plus2net.com'
);

The Cell fill parameter must be true for the background color to appear.

View Cell with Background Color

Change PDF Text Color using SetTextColor() Top ↑

Use SetTextColor() to set the RGB color used for text.

<?php
require 'fpdf.php';

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$pdf->SetFillColor(1,99,255);
$pdf->SetTextColor(255,254,254);

$pdf->Cell(
    80,
    10,
    'Hello World!',
    1,
    0,
    'C',
    true,
    'https://www.plus2net.com'
);

$pdf->Output('I','my_file.pdf');
View Cell with Background and Text Color

Set Border Color and Width Top ↑

SetDrawColor() controls the border color and SetLineWidth() controls its thickness.

<?php
require 'fpdf.php';

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$pdf->SetFillColor(1,99,255);
$pdf->SetTextColor(255,254,254);
$pdf->SetDrawColor(255,1,1);
$pdf->SetLineWidth(1);

$pdf->Cell(
    80,
    10,
    'Hello World!',
    1,
    0,
    'C',
    true,
    'https://www.plus2net.com'
);

$pdf->Output('I','my_file.pdf');
View Cell with Border Width and Color

For more examples of drawing colored lines and changing line width, see the FPDF Line() tutorial.

Add Borders to Selected Sides of a Cell Top ↑

The border argument can specify individual sides:

'L'   // Left
'T'   // Top
'R'   // Right
'B'   // Bottom
'LRT' // Left, right and top

For example:

$pdf->Cell(80,10,'Selected borders','LRT');
View Different Border Options

Display, Download, Save or Return a PDF using Output() Top ↑

FPDF Output() controls what happens to the completed PDF document.

$pdf->Output('I','my_file.pdf');
// Display the PDF inline in the browser

$pdf->Output('D','my_file.pdf');
// Send the PDF as a download

$pdf->Output('F','my_file.pdf');
// Save the PDF file on the server filesystem

$pdf_data=$pdf->Output('S');
// Return the PDF document as a string

Output destination values Top ↑

I
Display the PDF inline in the browser.
D
Send the PDF to the browser as a download.
F
Save the generated file on the server.
S
Return the generated PDF as a PHP string.
The destination comes first and the filename comes second. For example: Output('I','my_file.pdf').

FPDF Cell() vs MultiCell() Top ↑

Cell() works well when the text fits on one line. If the text is longer than the available width, it can extend beyond the Cell boundary.

MultiCell() is designed for text that needs to wrap automatically onto additional lines.

Cell()      - single-line text
MultiCell() - wrapped multi-line text
Learn FPDF MultiCell()

FPDF Cell() and Output() Video Tutorials Top ↑

Installation and generating PDF document in PHP using FPDF class and showing Hello World

FPDF Cell() and Output() options for borders, alignment, colors and PDF delivery

Common FPDF Problems Top ↑

FPDF class is not found Top ↑

Check the path used by require 'fpdf.php'; and confirm that the FPDF file is available to the PHP script.

PDF cannot be displayed because output already started Top ↑

Do not send HTML, debugging messages or other browser output before calling FPDF Output().

Background color does not appear Top ↑

Call SetFillColor() and make sure the Cell fill argument is set to true.

Text goes outside the Cell Top ↑

A normal Cell does not wrap long text. Use MultiCell() when the text must continue onto additional lines.

PDF is downloaded instead of displayed Top ↑

Use Output('I','filename.pdf') to request inline browser display. Use D when the PDF should be sent as a download.

Some characters are missing from the PDF Top ↑

Classic FPDF core fonts have limited character-set support. A suitable font and encoding setup is required when the document contains characters outside those supported by the selected font.

Continue from the basic Cell example to practical PDF layouts and reports:

MultiCell() Draw Lines Add Images Create PDF Tables

Create PDF with Pie Chart Database Records to PDF Table

Generate PDF from Database Values Multi-page Database PDF

Frequently Asked Questions Top ↑

Q1: How do I create a PDF file in PHP using FPDF?

Load fpdf.php, create an FPDF object, call AddPage(), select a font with SetFont(), add content with Cell() and finish by calling Output().

Q2: How do I add text to an FPDF document?

Use Cell() for single-line text. Supply the cell width, height and text, followed by optional border, position, alignment, fill and link settings.

Q3: How do I add a background color to an FPDF Cell?

Set the RGB color with SetFillColor() and pass true as the fill argument of Cell().

Q4: How do I change text and border colors in FPDF?

Use SetTextColor() for text and SetDrawColor() for borders and drawing operations. Both can accept RGB values.

Q5: How do I display a generated PDF in the browser?

Use Output('I','filename.pdf'). The I destination requests inline browser display.

Q6: How do I force a generated PDF to download?

Use Output('D','filename.pdf'). The D destination sends the generated PDF as a download.

Q7: What is the difference between Cell() and MultiCell() in FPDF?

Cell() is suitable for single-line content. MultiCell() automatically wraps longer text onto additional lines within the specified width.


MultiCell() Drawing Line in PDF Adding Image to PDF Table in PDF


Subscribe to our YouTube Channel here



plus2net.com







20-11-2020

Excellent post. Nice work keep it up. Thanks for sharing the knowledge.

26-11-2021

Excellent post. Nice work keep it up. Thanks for sharing the knowledge.

19-01-2023

fpdf was run complatly in local host , browser but cant use in live server why?




PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer