Php Programming Code Examples
Php > Php Classes Code Examples
PDF class
PDF class
<?
class PDFFile
{
var $pdf;
var $FileName;
var $Author;
var $Creator;
var $Subject;
var $Title;
var $font;
var $file;
var $xpos;
var $xpos;
var $pageSizeX;
var $pageSizeY;
function PDFFile(){
$this->pdf=pdf_new();
$this->FileName="";
$this->Author="";
$this->Creator="";
$this->Subject="";
$this->Title="";
$this->file="";
$this->pageSizeX=612;
$this->pageSizeY=792;
$this->font="Helvetica-Bold";
}
function OpenPDF(){
//Opens a new pdf object
pdf_open_file($this->pdf);
$this->Data();
}
function ClosePDF(){
//Closes a pdf document
pdf_close($this->pdf);
}
function OpenPDFFile($path){
//Opens a new pdf document
$this->file = fopen($path,"w");
$this->pdf= PDF_open($this->file);
$this->Data();
}
function SavePDFFile(){
//saves a pdf document
fclose($this->file);
}
function Data(){
//Fills a field of the document information
pdf_set_info($this->pdf, "Author",$this->Author);
pdf_set_info($this->pdf, "Title",$this->Title);
pdf_set_info($this->pdf, "Creator",$this->Creator);
pdf_set_info($this->pdf, "Subject",$this->Subject);
}
function ShowPDF(){
//Fetch the buffer containig the generated PDF data.
$this->file = pdf_get_buffer($this->pdf);
$this->SendToBrowser();
echo $this->file;
}
function SendToBrowser(){
header("Content-type: application/pdf");
header("Content-disposition: inline; filename=" . $this->filename);
header("Content-length: " . strlen($this->file));
}
function StartPage(){
//Starts new page
pdf_begin_page($this->pdf, $this->pageSizeX, $this->pageSizeY);
}
function ClosePage(){
//Ends a page
pdf_end_page($this->pdf);
}
function Text($Text,$fontsize,$x,$y){
//Output text at given position ($x,$y)
$this->fontfunction($fontsize);
pdf_show_xy($this->pdf, $Text,($this->pageSizeX)-($x),($this->pageSizeY)-($y));
}
function TextCont($Text,$ydist){
//Outputs text in next line
pdf_set_leading ($this->pdf,$ydist);
pdf_continue_text($this->pdf, $Text);
}
function TextRend($num){
//Determines how text is rendered
PDF_set_text_rendering($this->pdf,$num);
}
function SpacingChar($num){
//defines the space between characters
pdf_set_char_spacing ($this->pdf,$num);
}
function SpacingWord($num){
//defines the space between words
pdf_set_word_spacing ($this->pdf,$num);
}
function fontwide($num){
//defines letters wide
pdf_set_horiz_scaling ($this->pdf,$num);
}
function fontfunction($fontsize){
//defines font and fontsize
pdf_set_font($this->pdf,$this->font,$fontsize, "winansi");
}
function Line($x1,$y1,$x2,$y2){
//draws a line
//starting at point ($x1,$y1) and ending at pont ($x2,$y2)
pdf_moveto($this->pdf,$this->pageSizeX-$x1,$this->pageSizeY-$y1);
pdf_lineto($this->pdf,$this->pageSizeX-$x2,$this->pageSizeY-$y2);
pdf_stroke($this->pdf);
}
function arc($x,$y,$radius,$angleStart,$angleend){
//Draws an arc
//with center at point ($x,$y) and radius $radius, starting at $anglestart and ending at $angleend.
pdf_arc($this->pdf,$this->pageSizeX-$x,$this->pageSizeY-$y,$radius,$angleStart,$angleend);
pdf_stroke($this->pdf);
}
function Rectangle($x,$y,$xsize,$ysize){
//Draws a rectangle with its lower left corner at point ($x,$y)
//this width is set to $xsize and this height is set to $ysize
pdf_rect($this->pdf,$this->pageSizeX-$x,$this->pageSizeY-$y,$xsize,$ysize);
pdf_stroke($this->pdf);
}
function RoundRect($x,$y,$xsize, $ysize, $radius){
//Draws a rectangle with its lower left corner at point ($x,$y)
//this width is set to $xsize and this height is set to $ysize
//with a corners angle ($radius)
$xpos=$this->pageSizeX-$x;
$ypos=$this->pageSizeY-$y;
pdf_moveto($this->pdf, $xpos,$ypos-$radius);
pdf_lineto($this->pdf, $xpos,$ypos-$ysize+$radius);
pdf_arc($this->pdf, $xpos+$radius, $ypos-$ysize+$radius, $radius, 180, 270);
pdf_lineto($this->pdf, $xpos+$xsize-$radius, $ypos-$ysize);
pdf_arc($this->pdf, $xpos+$xsize-$radius, $ypos-$ysize+$radius, $radius, 270, 360);
pdf_lineto($this->pdf, $xpos+$xsize, $ypos-$radius);
pdf_arc($this->pdf, $xpos+$xsize-$radius, $ypos-$radius, $radius,0,90);
pdf_lineto($this->pdf, $xpos+$radius, $ypos);
pdf_arc($this->pdf, $xpos+$radius, $ypos-$radius, $radius,90,180);
pdf_stroke($this->pdf);
}
function Circle($x,$y,$radius){
//draws a circle
//with center at point ($x,$y) and radius $radius
pdf_circle ($this->pdf,$this->pageSizeX-$x,$this->pageSizeY-$y,$radius);
pdf_stroke($this->pdf);
}
function rotate($xtranslate,$ytranslate,$numrot){
//sets the origin of coordinate system to the point ($xtranslate,$ytranslate) relativ the current
origin.
//and rotate the coordinate system by $numrot degrees.
$this->SavePdf();
$this->Translate($this->pdf, $xtranslate, $ytranslate);
pdf_rotate($this->pdf, $numrot);
}
function SavePdf(){
//Saves the current environment
//it is useful if you want to translate or rotate an object without effecting other objects.
pdf_save($this->pdf);
}
function Translate($xTranslate, $ytranslate){
//sets the origin of coordinate system to the point ($xtranslate,$ytranslate) relativ the current
origin.
pdf_translate($this->pdf, $xTranslate, $ytranslate);
}
function Restore(){
//Restore the graphics state to the way it was
pdf_restore($this->pdf);
}
function OpenJpg($path,$x,$y,$scale){
//opens a Jpg image
//places an image on the page at postion ($x,$y) and scaled ($scale)
$pdfimage = pdf_open_image_file($this->pdf, "jpeg", $path);
PDF_place_image($this->pdf,$pdfimage,$this->pageSizeX-$x,$this->pageSizeY-$y,$scale);
}
function OpenTiff($path,$x,$y,$scale){
//open a tiff image
//places an image on the page at postion ($x,$y) and scaled ($scale)
$pdfimage = pdf_open_image_file($this->pdf, "tiff", $path);
PDF_place_image($this->pdf,$pdfimage,$this->pageSizeX-$x,$this->pageSizeY-$y,$scale);
}
function Opengif($path,$x,$y,$scale){
//open a Gif image
//places an image on the page at postion ($x,$y) and scaled ($scale)
$pdfimage = pdf_open_image_file($this->pdf, "gif", $path);
PDF_place_image($this->pdf,$pdfimage,$this->pageSizeX-$x,$this->pageSizeY-$y,$scale);
}
function OpenPng($path,$x,$y,$scale){
//open a Png image
//places an image on the page at postion ($x,$y) and scaled ($scale)
$pdfimage = pdf_open_image_file($this->pdf, "png", $path);
PDF_place_image($this->pdf,$pdfimage,$this->pageSizeX-$x,$this->pageSizeY-$y,$scale);
}
}//end of class
/* lets see an example */
//instance a new pdf object
$pdf = new PDFFile;
//Fills the document information
$pdf->Filename="ExampleFileName";
$pdf->Author="Diana Ortega";
$pdf->Title="ExampleTitle";
$pdf->Creator="Diana Ortega";
$pdf->Subject="PDFExample";
//Opens the new pdf object
$pdf->openPDF();
//starts the first page
$pdf->BOF();
//draw a rectangle with its lower left corner at point ('pageSizeX->612'-300,'pageSizeY->792'-300)
//and its width is 200 and height is 250
$pdf->rectangle(300,300,200,250);
//Output text at given position ('pageSizeX->612'-400,'pageSizeY->792'-400)
//and its fontsize is 15
$pdf->Text("HELLO1",15,400,400);
//output the text 30 coordinates bellow
$pdf->TextCont("HELLO2",30);
//change the parameter for this text
$pdf->TextRend(2);
$pdf->spacingchar(25);
$pdf->spacingword(35);
$pdf->fontwide(80);
$pdf->Text("Hello :)",15,450,400);
//Draws an arc
//with center at point ('pageSizeX->612'-500,'pageSizeY->792'-500)
/and radius 50, starting at angle 90 and ending at 180
$pdf->arc(500,500,50,90,180);
//draws a circle
//at the point ('pageSizeX->612'-400,'pageSizeY->792'-400)
$pdf->circle(400,400,25);
//End of page
$pdf->EOF();
//Closes the pdf document
$pdf->closePDF();
//Show in window the PDF document
$pdf->Showpdf();
?>
The result should be a PDF file with the indicated draws.