Berner Fachhochschule - HTI PHP Miscellaneous Dr. E. Benoist Winter Term 2005-2006 PHP Miscellaneous 1
PHP Miscellaneous Generating PDF Documents � FPDF Library Image Generation � GD Library Example: Barcode Generator Authorization � Using HTTP Using HTML form Template � Web Services � Conclusion � PHP Miscellaneous 2
Miscellaneous functionalities of PHP Generate Portable Document Format (PDF) Files ◮ Two Libraries ◮ PDFLib : A C library that must be included at compilation time (not free for commercial use); ◮ fpdf : A free, PHP written, PDF library. Authentification ◮ Use HTTP authentification scheme (which open a small pop-up); ◮ Create your own authentification scheme using forms. Image Generation ◮ Use the GD library, a C written lib included durring the compilation ◮ Creation of jpeg and png files (no gif support for rights problems) PHP Miscellaneous 3
FPDF library Freely downloadable ◮ http://www.fpdf.org/ ◮ In PHP written ◮ Contains a PHP class: FPDF ◮ Needs to be copied in the directory where PHP can find it ◮ Contains also font metrics (should be in a fonts subdirectory in the same directory. Functionalities ◮ Creates a PDF document and makes the output ◮ Insert text, images, headers and footers ◮ Defines page size (A4, letter, landscape or portrait) ◮ Change the margins ◮ . . . PHP Miscellaneous Generating PDF Documents: FPDF Library 4
Create your first document Initialize and use the FPDF object ◮ Load the class ◮ Create an instance of this class $pdf=new FPDF(); • Can be more explicit: $pdf=new FPDF(’P’ ’mm’,’A4’);, • ’ P ’ stands for portrait, could be ’ L ’ • ’ mm ’ stands for millimeters, could be cm , in or pt • The format could be Letter or Legal ◮ Create a new page $pdf->AddPage(); ◮ Write a message $pdf − > SetFont(’Arial’,’B’,16); $pdf − > Cell(40,10,’Hello World!’); ◮ Change the header and write the content of the document $pdf->Output(); PHP Miscellaneous Generating PDF Documents: FPDF Library 5
Create your first document (Cont.) Example < ?php require(’../fpdf.php’); $pdf=new FPDF(); $pdf − > AddPage(); $pdf − > SetFont(’Arial’,’B’,16); $pdf − > Cell(40,10,’Hello World!’); $pdf − > Output(); ? > PHP Miscellaneous Generating PDF Documents: FPDF Library 6
Write Header and Footer 1 < ?php 2 require(’../fpdf.php’); 3 class PDF extends FPDF 4 { 5 function Header() { 6 $this − > Image(’logo pb.png’,10,8,33); 7 $this − > SetFont(’Arial’,’B’,15); 8 //Move to the right 9 $this − > Cell(80); 10 //Title 11 $this − > Cell(30,10,’Title’,1,0,’C’); 12 //Line break 13 $this − > Ln(20); 14 } PHP Miscellaneous Generating PDF Documents: FPDF Library 7
Write Header and Footer (Cont.) 15 //Page footer 16 function Footer() 17 { 18 //Position at 1.5 cm from bottom 19 $this − > SetY( − 15); 20 $this − > SetFont(’Arial’,’I’,8); 21 $this − > Cell(0,10,’Page ’.$this − > PageNo().’/ { nb } ’,0,0,’C’); 22 } 23 } 24 $pdf=new PDF(); 25 $pdf − > AliasNbPages(); 26 $pdf − > AddPage(); 27 $pdf − > SetFont(’Times’,’’,12); 28 for($i=1;$i < =40;$i++) 29 $pdf − > Cell(0,10,’Printing line number ’.$i,0,1); 30 $pdf − > Output(); 31 ? > PHP Miscellaneous Generating PDF Documents: FPDF Library 8
Insert Header and Footer (Cont.) Header ◮ Overwrite the Header() function, ◮ Insert an image $this->Image(’logo_pb.png’ 10,8,33); , ◮ Move to the right $this->Cell(80); ◮ Line break of 20mm $this->Ln(20); Footer ◮ Overwrite the Footer() function, ◮ Position at 1.5 cm from bottom $this->SetY(-15); ◮ Includes the page number: $pdf − > AliasNbPages(); $pdf − > AddPage(); PHP Miscellaneous Generating PDF Documents: FPDF Library 9
Add color in the cells A cell can have 3 colors ◮ Draw color for the rectangle (is set if we define a width) ◮ Fill color for the inside ◮ Text color for text $this − > SetDrawColor(0,80,180); $this − > SetFillColor(230,230,0); $this − > SetTextColor(220,50,50); //Thickness of frame (1 mm) $this − > SetLineWidth(1); $this − > Cell($width,9,$title,1,1,’C’,1); PHP Miscellaneous Generating PDF Documents: FPDF Library 10
Parameters of a Cell Compute the width of a text $w=$this − > GetStringWidth($title); A Cell can be configured $this − > Cell($width,9,$title,1,1,’C’,1); ◮ width ◮ height ◮ text ◮ if borders should be drawn ◮ where the current position moves after it (to the right, below or to the beginning of the next line) ◮ If the text is centered (C) or aligned ◮ Fill or not fill (boolean) To output justified text $this − > MultiCell(0,5,$txt); PHP Miscellaneous Generating PDF Documents: FPDF Library 11
Multicolumn Output 1 //Current column 2 var $col=0; 3 //Ordinate of column start 4 var $y0; 5 function Header() 6 { 7 ... 8 $this − > y0=$this − > GetY(); 9 } 10 11 function SetCol($col) 12 { 13 //Set position at a given column 14 $this − > col=$col; 15 $x=10+$col ∗ 65; 16 $this − > SetLeftMargin($x); 17 $this − > SetX($x); 18 } PHP Miscellaneous Generating PDF Documents: FPDF Library 12
Multicolumn Output (Cont.) 19 function AcceptPageBreak() { 20 //Method accepting or not automatic page break 21 if($this − > col < 2) { 22 //Go to next column 23 $this − > SetCol($this − > col+1); 24 //Set ordinate to top 25 $this − > SetY($this − > y0); 26 //Keep on page 27 return false; 28 } 29 else { 30 //Go back to first column 31 $this − > SetCol(0); 32 //Page break 33 return true; 34 } 35 } 36 //Output text in a 6 cm width column 37 $this − > MultiCell(60,5,$txt); PHP Miscellaneous Generating PDF Documents: FPDF Library 13
Create a table A very basic one function BasicTable($header,$data) { //Header foreach($header as $col) $this − > Cell(40,7,$col,1); $this − > Ln(); //Data foreach($data as $row) { foreach($row as $col) $this − > Cell(40,6,$col,1); $this − > Ln(); } } PHP Miscellaneous Generating PDF Documents: FPDF Library 14
An improved table Supports the following improvements ◮ Each column has its own width, ◮ Titles are centered ◮ Figures right aligned. ◮ Horizontal lines have been removed. (Left and Right LR are shown) PHP Miscellaneous Generating PDF Documents: FPDF Library 15
An improved table (Cont.) function ImprovedTable($header,$data) { //Column widths $w=array(40,35,40,45); for($i=0;$i < count($header);$i++) //Header $this − > Cell($w[$i],7,$header[$i],1,0,’C’); $this − > Ln(); foreach($data as $row) { $this − > Cell($w[0],6,$row[0],’LR’); $this − > Cell($w[1],6,$row[1],’LR’); $this − > Cell($w[2],6,number format($row[2]),’LR’,0,’R’); $this − > Cell($w[3],6,number format($row[3]),’LR’,0,’R’); $this − > Ln(); } $this − > Cell(array sum($w),0,’’,’T’); } PHP Miscellaneous Generating PDF Documents: FPDF Library 16
Insert Text The Write() method ◮ $pdf->Write(5,’Hello World’); ◮ Goes to the end of the line ◮ Current position moves at the end of the text ◮ We can mix up text styles PHP Miscellaneous Generating PDF Documents: FPDF Library 17
Insert Links Links inside a page ◮ Creates a link and write it $link=$pdf − > AddLink(); $pdf − > Write(5,’here’,$link); ◮ Where does it go: $pdf − > SetLink($link); Get external URL $pdf − > Image(’logo.png’,10,10,30,0,’’,’http://www.fpdf.org’); PHP Miscellaneous Generating PDF Documents: FPDF Library 18
PHP and Image Generation What for? ◮ HTTP allows programmers to generate anything as response. ◮ It is possible to generate images for Maps, Menu items, ... How to create images? ◮ Use the GD Library. GD library is a C library that allows programmers to create images. Actually GD library implements JPEG and PNG standards. ◮ Compile the PHP module with the GD library, ◮ Write scripts that create images. PHP Miscellaneous Image Generation: GD Library 19
Example Create an image 50 x 100 < ?php // Create a new image (50 x 100) $im = @ImageCreate (50, 100) or die (”Cannot Initialize new GD image stream”); // Set background color to white $background color = ImageColorAllocate ($im, 255, 255, 255); // Set the pen color $text color = ImageColorAllocate ($im, 233, 14, 91); // Write a string in the image ImageString ($im, 1, 5, 5, ”A Simple Text String”, $text color); // Write out a header for the HTTP and then the image header (”Content − type: image/png”); ImagePNG ($im); ? > PHP Miscellaneous Image Generation: GD Library 20
Mime types Content type must contain a valid mime type if (function exists(”imagegif”)) { Header(”Content − type: image/gif”); ImageGIF($im); } elseif (function exists(”imagejpeg”)) { Header(”Content − type: image/jpeg”); ImageJPEG($im, ””, 0.5); } elseif (function exists(”imagepng”)) { Header(”Content − type: image/png”); ImagePNG($im); } elseif (function exists(”imagewbmp”)) { Header(”Content − type: image/vnd.wap.wbmp”); ImageWBMP($im); } else { die(”No image support in this PHP server”); } PHP Miscellaneous Image Generation: GD Library 21
Recommend
More recommend