WBP22202A0023PR07
WBP22202A0023PR07
Experiment No: 7
Title of Experiment Write a simple PHP program to create PDF document buy using graphics
concepts
Example:
<?php
// Create an image (300x200 pixels)
$image = imagecreatetruecolor(300, 200);
// Allocate colors
$white = imagecolorallocate($image, 255, 255, 255);
$red = imagecolorallocate($image, 255, 0, 0);
$blue = imagecolorallocate($image, 0, 0, 255);
imagefill($image, 0, 0, $white);
// Draw a red rectangle
imagerectangle($image, 50, 50, 250, 150, $red);
// Draw a blue ellipse (circle)
imageellipse($image, 150, 100, 100, 100, $blue);
// Set content type and output the image
header("Content-Type: image/png");
imagepng($image);
// Free memory
imagedestroy($image);
?>
Output:
☆ Exercise
Ans:
<?php
require('fpdf.php'); // Include the FPDF library
// Create a new PDF instance
$pdf = new FPDF();
$pdf->AddPage(); // Add a page
// Set font (Arial, Bold, Size 16)
$pdf->SetFont('Arial', 'B', 16);
// Add title
$pdf->Cell(190, 10, 'Hello, This is a PDF File!', 1, 1, 'C');
// Set font for body
$pdf->SetFont('Arial', '', 12);
$pdf->Ln(10); // Line break
$pdf->Cell(190, 10, 'This PDF is generated using PHP and FPDF.', 0, 1, 'C');
// Output the PDF to browser
$pdf->Output('example.pdf', 'I'); // 'I' to display in browser, 'D' to download
?>