how to convert html to pdf in php using fpdf

how to convert html to pdf in php using fpdf To convert a PDF from HTML we use open source fpdf library.

This class allows to convert HTML to PDF. It was designed to export my blog entries to PDFs.
Also Available Main fpdf features are:

  • provides for underlined, bold, italics text
  • provides for headlines <h1>, <h2>, <h3> & <h4>
  • provides for images
  • simple provides for tables
  • full provides for lists <li>
  • full provides for <blockquote>
  • provides for pseudo-tags <red> & <blue>
  • <pre> will be shown using Courier font, other text with Times
  • provides for <br>, <br /> & <p>
  • provides for <hr> & <hr />
  • document header is created with publishing date, article title, URL, company
  • document footer is created with current & total pagination numbers
  • built-in provides for iconv (character conversions)

Step 1: how to convert html to pdf in php using fpdf?

First of all you can free download FPDF Library from official main website

Read Also:  Send Emails Using Laravel 6 Mailable Class?

and then this downloaded unzip file & copy as well as paste FPDF library in your main PHP project library folder.

Call fpdf.php main class where you want apply the PDF download feature, If you want to make a PDF directly with HTML then display as below official great basic and simple example.

$pdf = new FPDF();
$pdf->Cell(40,10,'Welcome To Pakainfo!');

Also second example to generate table format in pdf data content example for “how to convert html to pdf in php using fpdf”

class PDF extends FPDF
	// Simple table
	function BasicTable($header, $data)
		// Header
		foreach($header as $col)
		// Data
		foreach($data as $row)
			foreach($row as $col)

Now You can call BasicTable() methods where you want to generate dynamic product table in PDF format.

$pdf = new PDF();
$header = array('ID', 'Product Name', 'Price (Rs)');
$data = array(
	          array('1', 'Mobile', '5000'),
	          array('2', 'Laptop', '1200'),
	          array('3', 'Computer', '1800'),

how to convert html to pdf in php using fpdf
how to convert html to pdf in php using fpdf

php convert html to pdf

Source Code
bellow a form where the user can html input some Form and convert it to PDF:

Read Also:  How to make the number pyramid program in php?


    $pdf = new createPDF(
        $_POST['html'],   // html text to publish
        $_POST['title'],  // article title
        $_POST['url'],    // article URL
        $_POST['company'], // company name
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>HTML2PDF -</title>
<style type='text/css'>
  <form name="pdfgen" method="post" target="_blank" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <fieldset><legend>HTML2PDF -</legend>
    <p>Title: <input type="text" maxlength="30" name="title"></p>
    <p>URL: <input type="text" maxlength="30" name="url"></p>
    <p>company: <input type="text" maxlength="30" name="company"></p>
    <p><textarea name="html" cols="50" rows="15"></textarea></p>
    <p><input type="submit" value="Generate PDF"></p>

How to Convert HTML to PDF Document in PHP using fpdf?

HTML form to submit the data

<form method="post" action="createpdf.php">
             <input id="name" name="name" type="text">
             <input id="email" name="email" type="text">
             <input id="mobile" name="mobile" type="text">
<button type="submit">Submit</button>




$pdf=new PDF_HTML();

$pdf->SetAutoPageBreak(true, 15);

//add logo image here


//set font style

$pdf->getTableData('<para><h1> - API and Web development Tutorial Website</h1><br>
Website: <u></u></para><br><br>How to Convert HTML to PDF with fpdf example');
//set the form of pdf
//assign the form post value in a variable and pass it. 

//Write HTML to pdf file and output that file on the web browser.



Creating PDF Files using PHP FPDF library - Convert HTML to PDF
Creating PDF Files using PHP FPDF library – Convert HTML to PDF

getTableData.php code file

class PDF_HTML extends FPDF
	var $B=0;
	var $I=0;
	var $U=0;
	var $HREF='';
	var $ALIGN='';

	function getTableData($html)
		//HTML parser
		$html=str_replace("\n",' ',$html);
		foreach($a as $i=>$e)
					$a2=explode(' ',$e);
					foreach($a2 as $v)

	function OpenTag($tag,$prop)
		if($tag=='B' || $tag=='I' || $tag=='U')
			if( !empty($prop['WIDTH']) )
				$Width = $prop['WIDTH'];
				$Width = $this->w - $this->lMargin-$this->rMargin;
			$x = $this->GetX();
			$y = $this->GetY();

	function CloseTag($tag)
		if($tag=='B' || $tag=='I' || $tag=='U')

	function SetStyle($tag,$enable)
		$this->$tag+=($enable ? 1 : -1);
		foreach(array('B','I','U') as $s)

	function PutLink($URL,$txt)

Ref :