Adding Image and Pie chat to PDF file by FPDF

PDF Image

Adding image to fpdf class PDF documents by using Image function with header and footer of all pages

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

$pdf = new FPDF();  
$pdf->AddPage();  
$pdf->Image('images/pdf-header.jpg', 0, 0);  
$pdf->Output();  
?>
The ouput of above code is here . ( Show Output ) We can add position with height, width and link to above code.
<?php  
require('fpdf.php');  

$pdf = new FPDF();  
$pdf->AddPage();  
$pdf->Image('images/pdf-header.jpg',20, 60, 180,20,'JPG','www.plus2net.com');
$pdf->Output();  
?>
The ouput of above code is here . ( Show Output )

Adding image at header and footer

adding image to pdf page
We can place image at header and footer of the page so it will be repeated in all pages. We will create one page which is extended up to 2nd page. We will use image inside our header so it will be repeated in 2nd page also. Same way we will add one image to footer of the page.
<?php  
require('fpdf.php');  

class PDF extends FPDF  
{  
    // Page header  
    function Header()  
    {  
        $this->Image('images/pdf-header.jpg', 0, 0);  
    }  

    // Page footer  
    function Footer()  
    {  
        $this->SetY(-20);  
        $this->Image('images/pdf-footer.jpg');  
    }  
}  

// Instanciation of inherited class  
$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();  
?>
The output of above code is here . ( Show Output )

Example: Inserting Multiple Images in a PDF

require('fpdf.php');  

$pdf = new FPDF();  
$pdf->AddPage();  
$pdf->Image('image1.jpg', 10, 10, 40);  
$pdf->Image('image2.png', 60, 10, 40);  
$pdf->Output();

Example: Resizing Image Dynamically

require('fpdf.php');  

$pdf = new FPDF();  
$pdf->AddPage();  

list($width, $height) = getimagesize('image.jpg');  

$pdf->Image('image.jpg', 10, 10, $width / 4, $height / 4);  // Resizing  

$pdf->Output();

Creating a Pie Chart and Embedding It in a PDF Using PHP

In this example, we generate a pie chart using PHP's GD library and embed it into a PDF using FPDF. This is useful for dynamic report generation, allowing users to visualize data in a structured PDF format.

GD imagefilledarc() : Draw filled arcs
DEMO: generating PDF with Pie Chat

📄 How to Insert a Dynamic Pie Chart into a PDF Using FPDF in PHP | GD Library Integration

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

// Function to create a pie chart using GD
function create_pie_chart($filename) {
    $width = 300;
    $height = 300;
    $image = imagecreate($width, $height);
    
    // Colors
    $white = imagecolorallocate($image, 255, 255, 255);
    $colors = [
        imagecolorallocate($image, 255, 0, 0),   // Red
        imagecolorallocate($image, 0, 255, 0),   // Green
        imagecolorallocate($image, 0, 0, 255),   // Blue
        imagecolorallocate($image, 255, 255, 0)   // Yellow
    ];
    
    // Sample data for the pie chart
    $data = [10, 10, 25, 25]; 

    $my_sum = array_sum($data); // Calculate sum of data values
    $start_angle = 0;
    $center_x = $width / 2;
    $center_y = $height / 2;
    $radius = 100;
    
    // Draw Pie Chart
    foreach ($data as $index => $value) {
        $end_angle = $start_angle + ($value * 360 / $my_sum);
        imagefilledarc($image, $center_x, $center_y, $radius * 2, $radius * 2, 
            $start_angle, $end_angle, $colors[$index], IMG_ARC_PIE);
        $start_angle = $end_angle;
    }
    
    // Save image as PNG
    imagepng($image, $filename);
    imagedestroy($image);
}

// Generate Pie Chart Image
$chart_file = "pie_chart.png";
create_pie_chart($chart_file);

// Create PDF and insert Pie Chart
$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);

// Output PDF
$pdf->Output();

// Delete temporary image
unlink($chart_file);
?>

Code Explanation

  • Pie Chart Generation: The script creates a pie chart using the GD library, assigns colors, and calculates segment angles.
  • Saving the Chart: The generated pie chart is stored as a PNG file.
  • Embedding in PDF: The FPDF library is used to create a PDF file, insert the pie chart, and format the document.
  • Final Output: The generated PDF is displayed in the browser, and the temporary pie chart image is deleted.

Generating a Bar Chart in PHP and Embedding it in a PDF

Bar grpah by using imagefilledrectangle() to create filled  Rectangles

This PHP script dynamically generates a bar chart using the GD library and saves it as an image file. The generated chart is then embedded into a PDF document using the FPDF library. Once the PDF is generated, the temporary image file is deleted. This ensures efficient memory usage while keeping the process dynamic.

GD imagefilledrectangle() : Draw filled rectangles
DEMO: generating PDF with Bar Chat

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

// Function to create a Bar chart using GD
function create_bar_chat($filename) {
    // Set the image width and height
    $width = 400; 
    $height = 300;
    $image = imagecreate($width, $height);

    // Define colors
    $white = imagecolorallocate($image, 255, 255, 255); 
    $black = imagecolorallocate($image, 0, 0, 0);  
    $blue = imagecolorallocate($image, 0, 102, 204);  

    // Sample Data
    $data = [50, 80, 120, 60, 90];  
    $bar_width = 40;
    $gap = 20; // Gap between bars  
    $base = $height - 30;

    // Draw axes
    imageline($image, 40, 10, 40, $base, $black);  
    imageline($image, 40, $base, $width - 10, $base, $black);  

    // Draw bars
    $x = 50;
    foreach ($data as $value) {
        imagefilledrectangle($image, $x, $base - $value, $x + $bar_width, $base, $blue);
        imagestring($image, 4, $x + 10, $base - $value - 15, $value, $black);
        $x += $bar_width + $gap;
    }

    // Output the image
    imagepng($image, $filename);
    imagedestroy($image);
}

// Generate Bar Chart Image
$chart_file = "bar_chart.png";
create_bar_chat($chart_file);

// Create PDF and insert Bar Chart
$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, 100);

// Output PDF
$pdf->Output();

// Delete temporary image
unlink($chart_file);
?>

Code Explanation:

  • require('fpdf.php'): Includes the FPDF library for PDF generation.
  • function create_bar_chat($filename): Defines a function to create the bar chart and save it as an image.
  • $image = imagecreate($width, $height): Creates an empty image canvas.
  • imagecolorallocate: Defines colors for background, bars, and axes.
  • $data = [50, 80, 120, 60, 90]: An array containing sample values for the bars.
  • imagefilledrectangle: Draws the bars dynamically based on the provided data.
  • imagepng($image, $filename): Saves the generated image as a PNG file.
  • imagedestroy($image): Frees up memory by destroying the image resource.
  • $pdf = new FPDF(): Creates a new PDF document.
  • $pdf->AddPage(): Adds a new page to the PDF.
  • $pdf->Image($chart_file, 50, 40, 100, 100): Embeds the generated chart image into the PDF.
  • $pdf->Output(): Generates and outputs the final PDF document.
  • unlink($chart_file): Deletes the temporary bar chart image after the PDF is created.

This method is useful for dynamic reporting and data visualization, allowing easy PDF export of statistical graphics.


Questions



Cell Line()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    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 FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer