How do I 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',14);
$pdf->Cell(40,10,'This is a test!');
$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 convertToPDF.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/convertToPDF.php

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

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.