<?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 )
<?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 )
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image('image1.jpg', 10, 10, 40);
$pdf->Image('image2.png', 60, 10, 40);
$pdf->Output();
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();
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<?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);
?>
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<?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);
?>
This method is useful for dynamic reporting and data visualization, allowing easy PDF export of statistical graphics.
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. |