Add Images to PDF using PHP FPDF Image()

Adding images and dynamically generated charts to PDF documents using PHP FPDF

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.

Add an Image to a PDF Document Top ↑

Create 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

Image inserted into a PDF document using PHP FPDF

Set Image Position, Width, Height and Link Top ↑

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

FPDF Image() Parameters Top ↑

$pdf->Image(file,x,y,width,height,type,link);
file
Path of the image to display.
x
Horizontal position of the image.
y
Vertical position of the image.
width
Displayed image width.
height
Displayed image height.
type
Image type when it is supplied explicitly.
link
Optional destination when the image is clicked in the PDF.

Video: Add Images to FPDF Documents Top ↑

Adding images to FPDF documents using Image() with headers and footers

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

Insert Multiple Images into One PDF Page Top ↑

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.

Resize an Image Dynamically before Adding It to PDF Top ↑

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.

Create a Pie Chart and Embed It in a PDF using PHP Top ↑

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
DEMO: Generate PDF with Pie Chart

Insert a Dynamic Pie Chart into a PDF using FPDF and PHP GD

<?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);
}

How the Pie Chart PDF Example Works Top ↑

  • The GD library creates the image canvas.
  • The values in $data are converted into pie-chart angles.
  • imagefilledarc() creates each section of the pie chart.
  • The completed chart is saved as pie_chart.png.
  • FPDF Image() places the generated PNG into the PDF.
  • The temporary chart file is removed after the PDF is generated.

Create a Bar Chart and Embed It in a PDF Top ↑

Bar chart created using PHP GD imagefilledrectangle

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
DEMO: Generate PDF with Bar Chart

<?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);
}

How the Bar Chart PDF Example Works Top ↑

  • 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.
  • FPDF Image() embeds the chart into the PDF.
  • unlink() removes the temporary chart image after output.

Common Problems when Adding Images to FPDF Top ↑

The image does not appear in the PDF Top ↑

Check the image filename and path. The PHP script must be able to locate the image file used by Image().

The image appears in the wrong position Top ↑

Check the X and Y values passed to Image(). These coordinates determine the image position on the PDF page.

The image looks stretched Top ↑

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.

Header content overlaps the image Top ↑

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 dynamically generated chart is not available Top ↑

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 Tables

Database Records to PDF Table

Frequently Asked Questions Top ↑

Q1: How do I add an image to a PDF using PHP FPDF?

Create the FPDF document, add a page and call Image() with the image filename and its position on the page.

Q2: Can I control the position and size of an image in FPDF?

Yes. The Image() method accepts X and Y coordinates together with width and height values for controlling the displayed image.

Q3: Can I add more than one image to the same PDF page?

Yes. Call Image() multiple times and provide different coordinates for each image.

Q4: How do I repeat a logo or image on every PDF page?

Extend the FPDF class and place the Image() call inside the Header() or Footer() method so it is added when each page is generated.

Q5: How can I keep an image from becoming stretched?

Read the source dimensions with getimagesize() and calculate the displayed height from the chosen PDF width so the original proportion is retained.

Q6: Can PHP create a chart and then add it to an FPDF document?

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.

Q7: Can an image inside an FPDF document contain a clickable link?

Yes. The Image() method accepts an optional link value that can make the image clickable in the generated PDF.


Cell() Line()


Subscribe to our YouTube Channel here



plus2net.com







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.




PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer