Create a PDF file in PHP

December 28, 2013 | Last tested: May 2015 | 742 views | Comments

Use Free PDF (FPDF) PHP class to generate PDF files on the fly.

For example.

Create a basic PDF file and display it on the browser.

require('fpdf17/fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',16);
$pdf->Cell(40,10,'The quick brown fox jumps over the lazy dog.');
$pdf->Output();

 

Create a basic PDF file and save it to the server.

require('fpdf17/fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',16);
$pdf->Cell(40,10,'The quick brown fox jumps over the lazy dog.');
$pdf->Output("create-a-pdf-file-in-php.pdf");

 

Result.

Please mouse over here to view the image.

Create a basic PDF File with header and footer, and display it on the browser.

class PDF extends FPDF
{
	// Page header
	function Header()
	{
		// Logo
		$this->Image('images/logo.jpg',10,6,30);
		// Arial bold 15
		$this->SetFont('Arial','B',15);
		// Move to the right
		$this->Cell(80);
		// Title
		$this->Cell(30,10,'PDF Title',0,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().'/',0,0,'C');
	}
}

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',12);
$pdf->Cell(40,10,'The quick brown fox jumps over the lazy dog.');
$pdf->Output();

 

Related Tutorials


Must Read