How To Generate PDF From HTML Page in PHP


Share:
Published By: Admin, Published On: May 04, 2021, Category: PHP

Hi Dev,

In this blog, I will learn you PHP script to create PDF document using FPDF.

FPDF is a PHP class which allows engendering PDF files with PHP code. It is in liberty to utilize and it does not require any API keys. FPDF stands for Free PDF. It signifies 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 to setup page header and footer.
  • It provides automatic page break and line break.
  • It supports images in various formats (JPEG, PNG and GIF).
  • It allows to setup Colors and Links.
  • It also support encoding.
  • Along with Page compression feature it provides many other functions.

Note:Download the latest version of this Class from Link Here

Example
<?php
  
require('fpdf.php');
  
// New object created and constructor invoked
$pdf = new FPDF();
  
// Add new pages. By default no pages available.
$pdf->AddPage();
  
// Set font format and font-size
$pdf->SetFont('Arial', 'B', 20);
  
// Framed rectangular area
$pdf->Cell(176, 5, 'Welcome to MyWebtuts.com!', 0, 0, 'C');
  
// Set it new line
$pdf->Ln();
  
// Set font format and font-size
$pdf->SetFont('Times', 'B', 12);
  
// Framed rectangular area
$pdf->Cell(176, 10, 'Thanks For Visit!!', 0, 0, 'C');
  
// Close document and sent to the browser
$pdf->Output();
  
?>
Output

Example
<?php

    require('fpdf.php');
   
    class PDF extends FPDF
    {
        // Page header
        function Header()
        {
            // GFG logo image
            $this->Image('MyWebtuts.com.png', 94, 6, 42);
              
            // GFG logo image
            // $this->Image('MyWebtuts.com.png', 160, 8, 20);
              
            // Set font-family and font-size
            $this->SetFont('Times','B',20);
              
            // Move to the right
            $this->Cell(80);
              
            // Set the title of pages.
            $this->Cell(48, 35, 'Welcome to MyWebtuts.com', 0, 5, 'C');
              
            // Break line with given space
            $this->Ln(5);
        }
           
        // Page footer
        function Footer()
        {
            // Position at 1.5 cm from bottom
            $this->SetY(-15);
              
            // Set font-family and font-size of footer.
            $this->SetFont('Arial', 'I', 8);
              
            // set page number
            $this->Cell(0, 10, 'Page ' . $this->PageNo() .
                  '/{nb}', 0, 0, 'C');
        }
    }
       
    // Create new object.
    $pdf = new PDF();
    $pdf->AliasNbPages();
      
    // Add new pages
    $pdf->AddPage();
      
    // Set font-family and font-size.
    $pdf->SetFont('Times','',12);
      
    // Loop to display line number content
    for($i = 0; $i < 30; $i++)
        $pdf->Cell(30, 10, 'Line Number ' . $i, 0, 2, 'L');
          
    $pdf->Output();

?>
Output


I Hope It will help you..

Share: