Pages

Tuesday, March 16, 2021

Draw Lines in PDF By FPDF

Line(x1,y1,x2,y2);
PDF line generation

Here is a simple code to draw line
<?Php
require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();
$pdf -> Line(20, 20, 150, 200);

$pdf->Output();
?>

Draw a cross X using full length of page:

<?Php
require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();

$width=$pdf -> w; // Width of Current Page
$height=$pdf -> h; // Height of Current Page

$pdf->Line(0, 0,$width,$height); // Line one Cross
$pdf->Line($width, 0,0,$height); // Line two Cross

$pdf->Output();
?>

Draw border at a gap of 2 across the page:

<?Php
require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();

$width=$pdf -> w; // Width of Current Page
$height=$pdf -> h; // Height of Current Page
$edge=2; // Gap between line and border , change this value

$pdf->Line($edge, $edge,$width-$edge,$edge); // Horizontal line at top
$pdf->Line($edge, $height-$edge,$width-$edge,$height-$edge); // Horizontal line at bottom
$pdf->Line($edge, $edge,$edge,$height-$edge); // Vetical line at left 
$pdf->Line($width-$edge, $edge,$width-$edge,$height-$edge); // Vetical line at Right
$pdf->Output();
?>

Draw horizontal lines at a gap of 10 throughout the page:

require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();
$width=$pdf -> w; // Width of Current Page
$height=$pdf -> h; // Height of Current Page
$i=0;
for($i=0;$i<=$height;$i +=10){
$pdf -> Line(0, $i, $pdf -> w, $i);
}
$pdf->Output();
?>

Draw patterns of square boxes with reducing dimension across the page:

require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();

$width=$pdf -> w; // Width of Current Page
$height=$pdf -> h; // Height of Current Page
$edge=2; // Gap between line and border 

for($edge=2;$edge<=$width/2; $edge+=2){
$pdf->Line($edge, $edge,$width-$edge,$edge); // Horizontal line at top
$pdf->Line($edge, $height-$edge,$width-$edge,$height-$edge); // Horizontal line at bottom
$pdf->Line($edge, $edge,$edge,$height-$edge); // Vetical line at left 
$pdf->Line($width-$edge, $edge,$width-$edge,$height-$edge); // Vetical line at Right
}
$pdf->Output();
?> 

We can change line width ( default value is 0.2 )SetLineWidth(float width):

$pdf->SetLineWidth(0.8);

SetDrawColor(R,G,B) to change line color:

$pdf->SetDrawColor(10,10,10);
We can change our patern drawing code like this
<?Php
require('fpdf.php');
$pdf = new FPDF(); 
$pdf->AddPage();

$width=$pdf -> w; // Width of Current Page
$height=$pdf -> h; // Height of Current Page
$edge=2; // Gap between line and border 
$pdf->SetLineWidth(0.8);
$pdf->SetDrawColor(10,10,10);

for($edge=2;$edge<=$width/2; $edge+=5){
$pdf->SetDrawColor(10+(3*$edge),10+$edge,10+$edge);
$pdf->Line($edge, $edge,$width-$edge,$edge); // Horizontal line at top
$pdf->Line($edge, $height-$edge,$width-$edge,$height-$edge); // Horizontal line at bottom
$pdf->Line($edge, $edge,$edge,$height-$edge); // Vetical line at left 
$pdf->Line($width-$edge, $edge,$width-$edge,$height-$edge); // Vetical line at Right
}
$pdf->Output();
?>

No comments:

Post a Comment