Home
/
Website Help
/
PHP Questions
/
How to generate PDF files with PHP?

How to generate PDF files with PHP?

FPDF is a PHP class which allows you to generate PDF files and does not depend on additional PHP libraries.

FPDF is free and can be downloaded from the official website’s download section. The download package contains all necessary files, along with some tutorials on how to use it.

To upload the package on your account, use FTP or File Manager in Site Tools and extract it after that. Place the extracted contents in any folder on your account, which will then turn into the FPDF installation folder.

A basic example of using FPDF is with the following PHP code (you must extract the FPDF package in the folder where the PHP file with the code is located):

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

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

Let’s say that you have downloaded and extracted the FPDF package inside a folder called ~/public_html/fpdf/. Create a new PHP file called toPDF.php inside the same folder and insert the above code. Save the file and try to access it through your browser:

http://yourdomain.com/fpdf/toPDF.php

Upon execution, the PHP script will generate a PDF file in your browser.

Share This Article