Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 45

Generate PDF in PHP (FPDF Invoice)

In this lesson: Create PDF invoices and reports in PHP using FPDF — tables, fonts, logo and download — complete working example.

Making PDFs in PHP

PDFs are perfect for invoices, receipts, admit cards and reports — documents that must look the same everywhere and be easy to print. PHP does not create PDFs on its own, so we use a small, free library. Two popular choices are FPDF and Dompdf.

💡 FPDF = place text by position (precise). Dompdf = write HTML/CSS and convert it (familiar).

Why you need a library

There is no built-in PDF function in PHP, and building the PDF format by hand is very complex. A library handles all that for you. You install one with Composer:

composer require setasign/fpdf
// or
composer require dompdf/dompdf

Then you include Composer's autoloader and use the library's simple methods.

Creating a PDF with FPDF

FPDF lets you add text and shapes at exact positions.

<?php
  require "vendor/autoload.php";
  $pdf = new FPDF();
  $pdf->AddPage();
  $pdf->SetFont("Arial", "B", 16);
  $pdf->Cell(40, 10, "Invoice #1001");
  $pdf->Ln();
  $pdf->SetFont("Arial", "", 12);
  $pdf->Cell(40, 10, "Total: Rs 2500");
  $pdf->Output();   // show in browser
?>

AddPage starts a page, SetFont sets the font, Cell prints a box of text, and Output sends the PDF. It is ideal for precise, structured layouts.

HTML to PDF with Dompdf

If you already have HTML, Dompdf turns it straight into a PDF.

<?php
  require "vendor/autoload.php";
  use Dompdf\Dompdf;

  $dompdf = new Dompdf();
  $dompdf->loadHtml("<h1>Invoice #1001</h1><p>Total: Rs 2500</p>");
  $dompdf->render();
  $dompdf->stream("invoice.pdf");
?>

You write normal HTML and CSS, and Dompdf renders it as a PDF. This is great when you already have an HTML template for the document.

Common uses

  • Invoices and receipts for orders and payments.
  • Admit cards and certificates for schools and exams.
  • Reports like sales summaries or attendance.
  • Tickets and passes that users download and print.

Common mistakes

  • Output before the PDF. A stray space or echo corrupts the file — output only the PDF.
  • Expecting PHP to do it alone. You need a library like FPDF or Dompdf.
  • Complex CSS in Dompdf. It supports common CSS, but very advanced layouts may not render perfectly.
  • Not installing via Composer. Add the library properly and include the autoloader.

Summary

  • PHP needs a library to create PDFs — FPDF or Dompdf.
  • FPDF places text and shapes by position (precise control).
  • Dompdf converts HTML/CSS into a PDF (familiar approach).
  • Output with Output() (FPDF) or stream() (Dompdf), with no output before.
  • Great for invoices, certificates, reports and tickets.

Next, pull the data from MySQL, or export it another way with CSV export.

Frequently Asked Questions

Can PHP create PDF files on its own?
PHP has no built-in PDF maker, so you use a library. Two popular free ones are FPDF (you place text and boxes by position) and Dompdf (you write HTML/CSS and it converts to PDF). Both are easy to add via Composer.
What is the difference between FPDF and Dompdf?
FPDF gives you precise control by drawing text and lines at exact positions — great for structured layouts like invoices. Dompdf turns existing HTML and CSS into a PDF — great when you already have an HTML template. Choose based on how you prefer to build the layout.
How do I output a PDF to the browser?
With FPDF, call $pdf->Output(); with Dompdf, call $dompdf->stream(). You can show it inline in the browser or force a download, and there must be no other output before it.
Why do I get a blank or broken PDF?
Usually there is stray output (a space, echo, or warning) before the PDF is sent, which corrupts the file. Make sure nothing is printed before the PDF output, just like with download headers.
What are PDFs commonly used for in PHP?
Invoices, receipts, admit cards, certificates, reports and tickets — anything a user needs to download or print with a fixed layout. Generating them on the server keeps the format consistent.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicOTP Verification Your progress: 45 / 50 (90%) · Lesson 46 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free