Tutorials

PHP 7 FPDF Example: Create PDF Document From Text File Using Javascript

index.php

<?php
require('fpdf/fpdf.php');

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)
    $this->SetLineWidth(1);
    
    // Prints a cell Title
    $this->Cell($w, 9, $title, 1, 1, 'C', 1);
    
    // Line break
    $this->Ln(10);
  }
  
  // Function to set the document footer
  function Footer() {

    // Set Y Position from bottom
    $this->SetY(-20);
    
    // Sets font to Arial italic 10
    $this->SetFont('Arial', 'I', 10);
    
    // Sets the Text color in gray
    $this->SetTextColor(128);
    
    // 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
    $this->Ln(4);
  }
  
  // Function to set the content from a external file
  function tutorialContent($file) {

    // Read text file
    $f = fopen($file, 'r');
    $txt = fread($f, filesize($file));
    fclose($f);
    
    // 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
    $this->Ln();
    
    // 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->AddPage();
    $this->tutorialTitle($num, $title);
    $this->tutorialContent($file);
  }
}

// Initiate a PDF object
$pdf = new PDF();
$title = 'C Programming Language';

// Sets the document title
$pdf->SetTitle($title);

// Sets the document author name
$pdf->SetAuthor('gfg author name');

$pdf->showTutorial(
  1,
  'C Language Introduction',
  'cIntro.txt'
);

$pdf->showTutorial(
  2,
  'C Programming Language Standard',
  'cStandard.txt'
);

$pdf->showTutorial(
  3,
  'Importance of function prototype in C',
  'cPrototype.txt'
);

$pdf->Output();
?>
Furqan

Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.

Recent Posts

Obsidian vs Notion (2026): I tested both for 6 months

If you have been searching for the right note-taking or knowledge management app, you have…

May 31, 2026

AnyType Alternatives: 10 Best Tools for Knowledge Management in 2026

Looking for AnyType alternatives? You're not alone. AnyType has gained popularity as a privacy-focused, local-first…

May 31, 2026

Notion Alternatives – Best Note-taking & Wiki Tools

Notion is a popular all-in-one workspace, but many users seek alternatives for different needs (free…

May 31, 2026

Best Logseq Alternatives in 2026: Find Your Perfect Knowledge Management Tool

Logseq is a beloved tool in the personal knowledge management (PKM) community. It's free, open-source,…

May 30, 2026

Webshare Alternatives: 8 Best Proxy Providers to Use in 2026

Looking for a Webshare alternative? You're not alone. Webshare is a popular proxy service with…

May 30, 2026

Docker Alternatives in 2026: The Complete Guide to Container Tools

Docker changed software development forever. It made containers accessible, gave developers a simple workflow, and…

May 30, 2026