Open In App
Related Articles

How to generate PDF file from external text files using PHP ?

Like Article
Save Article
Report issue

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 break, image support, colors, links, and many more.


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

Example: The following code generates a PDF file based on external text files, namely “cIntro.txt”, “cStandard.txt” and “cPrototype.txt”. Refer to the comments in the code for better understanding and customisation as per the application’s needs.


class PDF extends FPDF {
    // Function to set the document header
    function Header() {
        global $title;
        // Sets font to Arial bold 15
        $this->SetFont('Arial', 'B', 15);
        // Calculate string length 
        $w = $this->GetStringWidth($title) + 6;
        $this->SetX((210 - $w) / 2);
        // Set drawing color
        $this->SetDrawColor(0, 80, 180);
        // It defines the grey color for filling
        $this->SetFillColor(105, 105, 105);
        // Sets the text color
        $this->SetTextColor(255, 0, 0);
        // Set the line width to 1 mm)
        // Prints a cell Title
        $this->Cell($w, 9, $title, 1, 1, 'C', 1);
        // Line break
    // Function to set the document footer
    function Footer() {
        // Set Y Position from bottom
        // Sets font to Arial italic 10
        $this->SetFont('Arial', 'I', 10);
        // Sets the Text color in gray
        // Prints a cell with Page number
        $this->Cell(0, 10, 'Page ' 
            . $this->PageNo(), 0, 0, 'C');
    // Function to set the title for a tutorial
    function tutorialTitle($num, $label) {
        // Sets font to Arial 12
        $this->SetFont('Arial', '', 12);
        // Sets to fill Background color with Light grey 
        $this->SetFillColor(211, 211, 211);
        // Prints a cell with Title for tutorial
        $this->Cell(0, 6, "Chapter $num : $label", 0, 1, 'L', 1);
        // Line break
    // Function to set the content from a external file 
    function tutorialContent($file) {
        // Read text file
        $f   = fopen($file, 'r');
        $txt = fread($f, filesize($file));
        // Sets the font to Times 12
        $this->SetFont('Times', '', 12);
        // It prints texts with line breaks
        $this->MultiCell(0, 5, $txt);
        //Puts a Line break
        // Set font in italics
        $this->SetFont('', 'I');
        // Prints a cell
        $this->Cell(0, 5, '(end of content)');
    function showTutorial($num, $title, $file) {
        // Add a new page
        $this->tutorialTitle($num, $title);
// Initiate a PDF object
$pdf   = new PDF();
$title = 'C Programming Language';
// Sets the document title
// Sets the document author name
$pdf->SetAuthor('gfg author name');
    'C Language Introduction'
    'C Programming Language Standard'
    'Importance of function prototype in C'


“cIntro.txt” file:

C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system. The main features of the C language include low-level memory access, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development. Many later languages have borrowed syntax/features directly or indirectly from the C language. Like syntax of Java, PHP, JavaScript, and many other languages are mainly based on the C language. C++ is nearly a superset of C language (Few programs may compile in C, but not in C++).

“cStandard.txt” file:

The latest C standard is ISO/IEC 9899:2011, also known as C11 as the final draft was published in 2011. Before C11, there was C99. The C11 final draft is available here. See this for a complete history of C standards. Can we know the behavior of all programs from C standard? C standard leaves some behavior of many C constructs as undefined and some as unspecified to simplify the specification and allow some flexibility in implementation. For example, in C the use of any automatic variable before it has been initialized yields undefined behavior and the order of evaluations of subexpressions is unspecified. This specifically frees the compiler to do whatever is easiest or most efficient, should such a program be submitted.

“cPrototype.txt” file:

Function prototype tells compiler about number of parameters function takes, data-types of parameters and return type of function. By using this information, compiler cross checks function parameters and their data-type with function definition and function call. If we ignore function prototype, program may compile with warning, and may work properly. But some times, it will give strange output and it is very hard to find such programming mistakes. Above program checks existence of file, provided from command line, if given file is exist, then the program prints “file exist”, otherwise it prints appropriate error message. Let us provide a filename, which does not exist in file system, and check the output of program on x86_64 architecture.


Last Updated : 14 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads