Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
How to generate an XML file dynamically using PHP?
Next article icon

How to generate PDF file using PHP ?

Last Updated : 26 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to generate PDF files with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line break, image support, colors, links, and many more.

Approach:

  • You need to download the FPDF class from the FPDF website and include it in your PHP script.
require('fpdf/fpdf.php');
  • Instantiate and use the FPDF class according to your need as shown in the following examples.
$pdf=new FPDF();

Example 1: The following example generates a PDF file with the given text in the code. The file can be downloaded or previewed as needed.

PHP
<?php    ob_end_clean(); require('fpdf/fpdf.php');  // Instantiate and use the FPDF class  $pdf = new FPDF();  //Add a new page $pdf->AddPage();  // Set the font for the text $pdf->SetFont('Arial', 'B', 18);  // Prints a cell with given text  $pdf->Cell(60,20,'Hello GeeksforGeeks!');  // return the generated output $pdf->Output();  ?> 

Output:

Example 2: The following example helps in understanding the setting of the page header and footer along with printing many lines on different pages of PDF files.

PHP
<?php  require('fpdf/fpdf.php');  class PDF extends FPDF {      // Page header     function Header() {                  // Add logo to page         $this->Image('gfg1.png',10,8,33);                  // Set font family to Arial bold          $this->SetFont('Arial','B',20);                  // Move to the right         $this->Cell(80);                  // Header         $this->Cell(50,10,'Heading',1,0,'C');                  // Line break         $this->Ln(20);     }      // Page footer     function Footer() {                  // Position at 1.5 cm from bottom         $this->SetY(-15);                  // Arial italic 8         $this->SetFont('Arial','I',8);                  // Page number         $this->Cell(0,10,'Page ' .              $this->PageNo() . '/{nb}',0,0,'C');     } }  // Instantiation of FPDF class $pdf = new PDF();  // Define alias for number of pages $pdf->AliasNbPages(); $pdf->AddPage(); $pdf->SetFont('Times','',14);  for($i = 1; $i <= 30; $i++)     $pdf->Cell(0, 10, 'line number '              . $i, 0, 1); $pdf->Output();  ?> 

Output:


Next Article
How to generate an XML file dynamically using PHP?

G

geetanjali16
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-function
  • PHP-Questions

Similar Reads

  • How to read PDF file using PHP ?
    In this article, we will learn how you can show/read PDF file contents on a browser using PHP. We have to include an external PHP file named "class.pdf2text.php". Include it in the required web page using PHP. Create an HTML form, in which we can choose a PDF file from your computer and also check w
    2 min read
  • How to generate PDF file and add TrueType fonts using PHP ?
    In this article, we will learn how to generate PDF files and add new TrueType fonts with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line
    2 min read
  • How to generate PDF file from external text files using PHP ?
    In this article, we will learn how to generate PDF files from external text files with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, line br
    5 min read
  • How to generate an XML file dynamically using PHP?
    A file can be generated using PHP from the database, and it can be done by the static or dynamic method in PHP. Static methods can be called directly - without creating an instance of a class. Here we are going to discuss how to create an XML file dynamically. The first thing we need to do is fetch
    3 min read
  • Generate PDF File Using jsPDF Library
    The PDF Generation Project in JavaScript aims to create a seamless tool for generating PDFs dynamically. It allows users to easily convert data into well-formatted PDF documents, enhancing productivity and user experience with quick and efficient document creation. What Will You Learn?We’ll build an
    3 min read
  • How to unzip a file using PHP ?
    To unzip a file with PHP, we can use the ZipArchive class. ZipArchive is a simple utility class for zipping and unzipping files. We don't require any extra additional plugins for working with zip files. ZipArchive class provides us a facility to create a zip file or to extract the existing zip file.
    2 min read
  • PHP | FPDF-PDF Generator
    FPDF is a PHP class which allows generating PDF files with PHP code. It is free to use and it does not require any API keys. FPDF stands for Free PDF. It means that any kind of modification can be done in PDF files. The main features of this class are: Allows to setup page format and margins. Allows
    2 min read
  • How to Embed PDF file using HTML ?
    We will learn how to embed PDF files in HTML documents and explore their implementation through examples. Sometimes, you may want to insert a PDF file into an HTML document to make the content more interactive. Since HTML and PDF formats are different, embedding PDFs can be a bit challenging. Here a
    3 min read
  • How to design runtime generated PDF via HTML ?
    Runtime-generated PDFs via HTML refer to creating PDF documents on-the-fly using HTML content. This process involves rendering HTML elements into a PDF format during application execution, allowing dynamic data and layouts to be included, which is useful for generating reports, invoices, or other do
    5 min read
  • Image To PDF Converter using React
    Image To PDF Converter using React is a web application that allows users to upload images and convert them into PDF documents. It uses React's state management and file handling capabilities to provide a user-friendly interface for image upload, display, deletion, and PDF generation. This applicati
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences