
FPDF provides the Image() method for placing an image inside a PDF document. We can specify the image file, its X and Y coordinates, width, height, image type and an optional link.
$pdf->Image(file,x,y,width,height,type,link);
The same method can be used for logos, report headers, footer images and charts created dynamically by PHP.
Show Table of ContentsCreate an FPDF document, add a page and then call Image() with the path of the image.
<?php
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
$pdf->Image('images/pdf-header.jpg',0,0);
$pdf->Output('I','image-example.pdf');
View Image PDF Output
The X and Y coordinates control where the image appears on the PDF page. Width and height control the displayed dimensions.
<?php
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
$pdf->Image(
'images/pdf-header.jpg',
20,
60,
180,
20,
'JPG',
'https://www.plus2net.com'
);
$pdf->Output('I','positioned-image.pdf');
In this example the image begins at X = 20 and Y = 60 and is displayed with a width of 180 and height of 20. The image also acts as a link to Plus2net.
View Positioned Image Output$pdf->Image(file,x,y,width,height,type,link);
Images placed inside the FPDF Header() and Footer() methods are repeated whenever a new page is generated.
This example extends the FPDF class and creates enough rows to continue onto another page.
<?php
require 'fpdf.php';
class PDF extends FPDF
{
function Header()
{
$this->Image('images/pdf-header.jpg',0,0);
}
function Footer()
{
$this->SetY(-20);
$this->Image('images/pdf-footer.jpg');
}
}
$pdf=new PDF();
$pdf->SetMargins(10,60,10);
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=40;$i++){
$pdf->Cell(0,10,'This is line number '.$i,0,1);
}
$pdf->Output('I','header-footer-images.pdf');
View Header and Footer Image Output
Call Image() more than once to place several images on the same PDF page.
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
$pdf->Image('image1.jpg',10,10,40);
$pdf->Image('image2.png',60,10,40);
$pdf->Output();
The different X coordinates place the images next to each other.
PHP's getimagesize() can read the source dimensions. We can then calculate a PDF height that preserves the original image proportion.
require 'fpdf.php';
$pdf=new FPDF();
$pdf->AddPage();
[$image_width,$image_height]=getimagesize('image.jpg');
$pdf_width=80;
$pdf_height=$pdf_width*$image_height/$image_width;
$pdf->Image(
'image.jpg',
10,
10,
$pdf_width,
$pdf_height
);
$pdf->Output();
The source image width and height are used only to calculate the aspect ratio. The displayed PDF width is set to 80 and the height is calculated automatically by the PHP code.
We can create a pie chart with PHP's GD library, save it as a PNG file and then add that PNG to the PDF with FPDF.
Data
|
v
PHP GD
|
v
Pie chart PNG
|
v
FPDF Image()
|
v
PDF report
GD imagefilledarc(): Draw Filled Arcs
<?php
require 'fpdf.php';
function createPieChart($filename)
{
$width=300;
$height=300;
$image=imagecreatetruecolor($width,$height);
$white=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$white);
$colors=[
imagecolorallocate($image,255,0,0),
imagecolorallocate($image,0,180,0),
imagecolorallocate($image,0,102,204),
imagecolorallocate($image,255,190,0)
];
$data=[10,10,25,25];
$total=array_sum($data);
$start_angle=0;
$center_x=(int)($width/2);
$center_y=(int)($height/2);
$radius=100;
$last_index=count($data)-1;
foreach($data as $index=>$value){
$end_angle=($index===$last_index)
? 360
: (int)round($start_angle+($value*360/$total));
imagefilledarc(
$image,
$center_x,
$center_y,
$radius*2,
$radius*2,
$start_angle,
$end_angle,
$colors[$index],
IMG_ARC_PIE
);
$start_angle=$end_angle;
}
imagepng($image,$filename);
imagedestroy($image);
}
$chart_file='pie_chart.png';
createPieChart($chart_file);
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(0,10,'Pie Chart Example in FPDF',0,1,'C');
$pdf->Image($chart_file,50,40,100,100);
$pdf->Output('I','pie-chart.pdf');
if(is_file($chart_file)){
unlink($chart_file);
}
$data are converted into pie-chart angles.imagefilledarc() creates each section of the pie chart.pie_chart.png.Image() places the generated PNG into the PDF.
The same approach can be used for a bar chart. PHP GD generates the chart as an image, FPDF inserts that image into the report, and the temporary image can then be deleted.
GD imagefilledrectangle(): Draw Filled Rectangles<?php
require 'fpdf.php';
function createBarChart($filename)
{
$width=400;
$height=300;
$image=imagecreatetruecolor($width,$height);
$white=imagecolorallocate($image,255,255,255);
$black=imagecolorallocate($image,0,0,0);
$blue=imagecolorallocate($image,0,102,204);
imagefill($image,0,0,$white);
$data=[50,80,120,60,90];
$bar_width=40;
$gap=20;
$base=$height-30;
imageline($image,40,10,40,$base,$black);
imageline($image,40,$base,$width-10,$base,$black);
$x=50;
foreach($data as $value){
imagefilledrectangle(
$image,
$x,
$base-$value,
$x+$bar_width,
$base,
$blue
);
imagestring(
$image,
4,
$x+10,
$base-$value-15,
(string)$value,
$black
);
$x+=$bar_width+$gap;
}
imagepng($image,$filename);
imagedestroy($image);
}
$chart_file='bar_chart.png';
createBarChart($chart_file);
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(0,10,'Bar Chart Example in FPDF',0,1,'C');
$pdf->Image($chart_file,50,40,100);
$pdf->Output('I','bar-chart.pdf');
if(is_file($chart_file)){
unlink($chart_file);
}
imagecreatetruecolor() creates the chart canvas.imageline() creates the chart axes.imagefilledrectangle() draws one bar for each value.imagestring() displays the value above each bar.imagepng() saves the chart as a temporary image.Image() embeds the chart into the PDF.unlink() removes the temporary chart image after output.Check the image filename and path. The PHP script must be able to locate the image file used by Image().
Check the X and Y values passed to Image(). These coordinates determine the image position on the PDF page.
A fixed width and fixed height can change the original proportions. Read the source dimensions with getimagesize() and calculate one displayed dimension from the other when the original aspect ratio should be preserved.
Leave enough top margin for the header image before regular page content begins. The header/footer example uses SetMargins() to provide additional top space.
The chart image must be created successfully before FPDF calls Image(). Confirm that the temporary PNG file is created before generating the PDF.
Combine images with other PDF drawing and reporting features:
FPDF Cell() FPDF Line() Create PDF TablesCreate the FPDF document, add a page and call Image() with the image filename and its position on the page.
Yes. The Image() method accepts X and Y coordinates together with width and height values for controlling the displayed image.
Yes. Call Image() multiple times and provide different coordinates for each image.
Extend the FPDF class and place the Image() call inside the Header() or Footer() method so it is added when each page is generated.
Read the source dimensions with getimagesize() and calculate the displayed height from the chosen PDF width so the original proportion is retained.
Yes. PHP GD can generate a pie chart or bar chart as an image file, and FPDF Image() can then embed that generated image into the PDF.
Yes. The Image() method accepts an optional link value that can make the image clickable in the generated PDF.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
21-12-2022 | |
| i have to add image in fpdf cell can you have any idea how to do this | |
30-03-2023 | |
| how to convert a pdf file into an image file in PHP or scriptcase | |
31-10-2024 | |
| you can use the Imagick extension, which is part of ImageMagick. | |