Location: PHPKode > scripts > PDF documents generates > php-pdf-class/index.php
<?php
//	This is a PHP PDFlib class for creating formatted PDF documents with PHP4

//	calculate pixels to inches
//	this function multiplies inches by 72 and returns that value.
function pxsize($n) {
	$s = $n*72;
	return $s;
}

#####################################################
#	Begin PDPh Class Definition
#	This class by Brian Abent
#	5 June, 2000
#	Extended by Rip Sumrall
#	26 June, 2000

//	Define pdph class
	class pdph {
		/*This is the class definition which includes
		all properties and methods of my class */

		//	Properties:
		var $doc_root;				#Document Root
		var $curfile;				#filename
		var $outline;				#holds outline request
		var $fp;					#local file pointer
		var $pdf;					#pdf doc handle
		var $position = array();	#keeps track of [x] and [y] position
		var $myfont = array();		#holds font [size] and [face] info
		var $header = array();		#holds header [author], [title], etc. info
		var $width;					#width of document in px
		var $height;				#height of doc in px
		var $margin;				#margin of doc in px
		var $workspace;
		var $isbullet;
		var $pagenum;
		var $numfooter;
		var $fax_header;

		//	Methods:

		function pdph($doc_root,$curfile, $height, $width, $margin) {
		#	Constructor.  Receive environment and do something with it
			$this->pagenum = 1;

			#	add ".pdf" extension to the filename
			$this->curfile = $doc_root.$curfile.".pdf";

			#	assign outline to environment
			$this->outline = $outline;

			#	assign margin to environment
			$this->margin = $margin;

			#	assign doc height to environment
			$this->height = $height;

			#	assign doc width to environment
			$this->width = $width;

			#	assign workspace (width - (margin * 2))
			$this->workspace = $this->width-($this->margin*2);

			#	open specified file for writing
			$this->fp = fopen($this->curfile, "w");

			#	assign file handle for pdflib functions
			$this->pdf = PDF_open($this->fp);

			#	build header array
			$this->header[author] = pdf_set_info_author($this->pdf, "Online Information");
			$this->header[title] = pdf_set_info_title($this->pdf, "Test for PHP wrapper of PDFlib 0.6");
			$this->header[creator] = pdf_set_info_creator($this->pdf, "Rip");
			$this->header[subject] = pdf_set_info_subject($this->pdf, "Testing");


			#	set up and begin writing pdf
			pdf_begin_page($this->pdf, $this->width, $this->height);


			#	place cursor at upper left of doc. with regard to the margin
			$this->position[x] = $this->margin;
			$this->position[y] = $this->height-$this->margin;
			$this->move($this->position[x],$this->position[y]);


			#	assign default font
			$this->myfont[face] = "Times-Roman";
			$this->myfont[size] = "12";
			pdf_set_font($this->pdf, $this->myfont[face], $this->myfont[size], "winansi",1);

		}
			#	add an outline to our pdf doc
		function outline($want) {
			if($want == "yes"){
				pdf_add_outline($this->pdf, "Page ".$this->pagenum);
			}
		}

		function transition($transition,$duration) {
			pdf_set_transition($this->pdf, $transition);
			pdf_set_duration($this->pdf, $transition);
		}

		function move($x, $y) {
		#	move the cursor position to x and y and return the x position, called internally.
			pdf_set_text_pos($this->pdf, $x, $y);
			$this->position[x] = $x;
			$this->position[y] = $y;
			return $this->position[x];
		}

		function move_cursor($x, $y) {
		#	move the cursor position to x and y and return the x position, called externally.
			$y = $this->height - $y;
			pdf_set_text_pos($this->pdf, $x, $y);
			$this->position[x] = $x;
			$this->position[y] = $y;
			return $this->position[x];
		}

		function chfont($b, $string) {
		#	change typeface attributes
			if ($b == "face") {
				pdf_set_font($this->pdf, $string, $this->myfont[size], "winansi",1);
				$this->myfont[face] = $string;
			} elseif ($b == "size") {
				pdf_set_font($this->pdf, $this->myfont[face], $string, "winansi",1);
				$this->myfont[size] = $string;
			}
		}

		function write($string) {
		#	write string of text to pdf
			if (is_array($string)) {
				$n = count($string);
				for ($i = 0; $i < $n; ++$i) {
					$this->newpage();
					pdf_show($this->pdf, $string[$i]);
					if ($i != ($n - 1)) {
						$this->nl();
						if ($this->isbullet) {
							$this->tab(1);
							pdf_show($this->pdf, " ");
						}
					}
				}
			$this->isbullet = "";
			} else {
				pdf_show($this->pdf, $string);
			}
			$this->position[x] += pdf_stringwidth($this->pdf, $string);
		}

		function str_width($str) {
		#	shorter notation for getting width of a string.
			$str_width= pdf_stringwidth($this->pdf, $str);
		}

		function tab($n) {
		#	move text position a bit to the right.  This is used internally for bullets.  If
		#	you want to format text, tables are nicer.
			$this->position[x] = $this->move($this->position[x]+(pxsize(.5)*$n), $this->position[y]);
		}

		function nl($leading = 20) {
		#	move cursor to a new line.
			$this->newpage();
			$h = $this->myfont[size] + ($this->myfont[size] * ($leading / 100));
			$h = $this->position[y] - $h;
			$this->position[x] = $this->move($this->margin, $h);
			$this->position[y] = $h;
		}

		function super_script($string) {
		#	change typeface attributes
			$this->text_rise($this->myfont[size] * .5);
	        $this->chfont("size",$this->myfont[size] * .5);
	        $this->write($string);
			$h = $this->position[y] - $this->myfont[size] * 2;
			$this->position[y] = $h;
	        $this->chfont("size",$this->myfont[size]);

		}

		function style_line($param,$toggle) {
		#	change typeface attributes
			if ($param == "ul"){
				pdf_set_parameter($this->pdf,underline, $toggle);
			}
			if ($param == "ol"){
				pdf_set_parameter($this->pdf,overline, $toggle);
			}
			if ($param == "so"){
				pdf_set_parameter($this->pdf,strikeout, $toggle);
			}
		}

		function boxed($string,$box_x,$box_y,$box_w,$box_h,$box_align) {
		#	change typeface attributes
			pdf_show_boxed($this->pdf,$string,$box_x,$box_y,$box_w,$box_h,$box_align);
		}

		function text_rise($rise) {
		#	change typeface attributes
			pdf_set_text_rise($this->pdf,$rise);
		}

		function text_leading($leading) {
		#	change typeface attributes
			pdf_set_leading($this->pdf,$leading);
		}

		function bullet($l,$leading,$col_1 = "",$col_2 = "") {
		#	create a bulletted list.  Input should be an array.
			if (!$col_1) {
				$col_1 = $this->margin;
			}
			if (!$col_2) {
				$col_2 = $this->workspace;
			}
			while (list($key, $value) = each($l)) {
			$this->isbullet = 1;
				if ($value != "") {
					$value = chr(149)." ".$value;
					$this->align_left($value,$leading,$l,$col_1,$col_2);
					$this->nl($leading);
				}
			}
		}

		function align_left($string,$leading,$num_lines,$col_1 = "",$col_2 = "",$url="") {
			if (!$col_1) {
				$col_1 = $this->margin;
			}
			if (!$col_2) {
				$col_2 = $this->workspace;
			}
			$this->position[x] = $this->move(($col_1), $this->position[y]);
			$foo = " ";
			$bar = split($foo, $string);
			$string = "";
			$i = 0;
			$j = 0;
			while (list($key, $value) = each($bar)) {
				$string[$i] = " $value";
				$pad = " ";
				if (pdf_stringwidth($this->pdf, $string[$i]) + pdf_stringwidth($this->pdf, $pad)<= ($col_2 - ($this->position[x]))) {
					pdf_show($this->pdf, $string[$i]);
					if($url){
						$ll_x = $a-$w/2;
						$ur_x = $a+$w/2;
						$ll_y = $this->position[y]+$this->myfont[size] / ($leading / 100);
						$ur_y = $start_y;
						$this->weblink($ll_x,$ll_y,$ur_x,$ur_y, $url);

					}
				} else {
					if ($j < $num_lines){
						++$j;
						$this->nl($leading);
						if($this->isbullet == 1){
							$indent = $this->myfont[size]/2;
						}
						$this->position[x] = $this->move(($col_1 + $indent), $this->position[y]);
						pdf_show($this->pdf, $string[$i]);
					}
				}
				$this->position[x] += pdf_stringwidth($this->pdf, $string[$i]);
				++$i;
			}
		}

		function align_center($string,$leading,$num_lines,$col_1,$col_2,$url="") {
			$start_y = $this->position[y];
			$this->position[x] = $this->move(($col_1), $this->position[y]);
			if (pdf_stringwidth($this->pdf, $string) <= ($col_2 - $col_1)) {
				$w = pdf_stringwidth($this->pdf, $string);
				$a = (($col_1+$col_2)/2);
				$this->position[x] = $this->move($a-$w/2, $this->position[y]);
				pdf_show($this->pdf, $string);
				if($url){
					$ll_x = $a-$w/2;
					$ur_x = $a+$w/2;
					$ll_y = $this->position[y]+$this->myfont[size] / ($leading / 100);
					$ur_y = $start_y;
					$this->weblink($ll_x,$ll_y,$ur_x,$ur_y, $url);
				}
				return;
			}
			$foo = " ";
			$bar = split($foo, $string);
			$string = "";
			$i = 0;
			while (list($key, $value) = each($bar)) {
				$string[$i] = " $value";
				$string_frag .= $string[$i];
				if (pdf_stringwidth($this->pdf, $string_frag) <= ($col_2 - $col_1)) {
					++$i;
					continue;
				}
				$w = pdf_stringwidth($this->pdf, $string_frag);
				$a = (($col_1+$col_2)/2);
				$this->position[x] = $this->move($a-$w/2, $this->position[y]);
				pdf_show($this->pdf, $string_frag);
				$this->nl($leading);
				$string_frag = "";
			}


		}
		function align_right($string,$leading,$num_lines,$col_1,$col_2,$url="") {
			$this->position[x] = $this->move(($col_1), $this->position[y]);
			if (pdf_stringwidth($this->pdf, $string) <= ($col_2 - $col_1)) {
				$w = pdf_stringwidth($this->pdf, $string);
				$this->position[x] = $this->move(($col_2)-$w, $this->position[y]);
				pdf_show($this->pdf, $string);
				if($url){
					$ll_x = $a-$w/2;
					$ur_x = $a+$w/2;
					$ll_y = $this->position[y]+$this->myfont[size] / ($leading / 100);
					$ur_y = $start_y;
					$this->weblink($ll_x,$ll_y,$ur_x,$ur_y, $url);
				}
                return;
			}
			$foo = " ";
			$bar = split($foo, $string);
			$string = "";
			$i = 0;
			while (list($key, $value) = each($bar)) {
				$string[$i] = " $value";
				$string_frag .= $string[$i];
				if (pdf_stringwidth($this->pdf, $string_frag) <= ($col_2 - $col_1)) {
					++$i;
					continue;
				}
				$w = pdf_stringwidth($this->pdf, $string_frag);
				$this->position[x] = $this->move(($col_2)-$w, $this->position[y]);
				pdf_show($this->pdf, $string_frag);
				$this->nl($leading);
				$string_frag = "";
			}
		}


//end of text stuff
//start graphics stuff
// Coordinates have been translated to upper left is (0,0)

 		function rotate($angle){
            pdf_save($this->pdf);
			pdf_rotate($this->pdf, $angle);
		}

 		function translate($x, $y){
			$y = $this->height - $y;
            pdf_save($this->pdf);
			pdf_translate($this->pdf, $x, $y);
		}

 		function skew($x_deg, $y_deg){
            pdf_save($this->pdf);
            pdf_skew($this->pdf, $x_deg, $y_deg);
        }

		function restore(){
            pdf_restore($this->pdf);
        }


		function color($red,$green,$blue) {
		#	change graphic attributes
				pdf_setrgbcolor($this->pdf, $red / 255, $green / 255, $blue / 255);
		}

//draw lines
		function line($x1,$y1,$x2,$y2) {
		      	//$y1 = $this->height - $y1;
		      	$y2 = $this->height - $y2;
				pdf_moveto($this->pdf, $x1, $y1);
				pdf_lineto($this->pdf, $x2, $y2);
				pdf_stroke($this->pdf);
		}

//draw filled or stroked circles
		function circle($circle_x,$circle_y,$circle_radius,$fill) {
			$circle_y = $this->height - $circle_y;
			pdf_circle($this->pdf,$circle_x,$circle_y,$circle_radius);
			if($fill == "fill"){
				pdf_fill_stroke($this->pdf);
			} else {
				pdf_stroke($this->pdf);
			}
		}

//draw filled or stroked rectangles
		function rectangle($rect_x,$rect_y,$rect_w,$rect_h,$fill) {
			$rect_y = $this->height - $rect_y;
			pdf_rect($this->pdf,$rect_x,$rect_y,$rect_w,$rect_h);
			if($fill == "fill"){
				pdf_fill_stroke($this->pdf);
			} else {
				pdf_stroke($this->pdf);
			}
		}

//draw filled or stroked arcs.
		function arc($arc_x,$arc_y,$arc_radius,$arc_start,$arc_end,$fill) {
			$arc_y = $this->height - $arc_y;
			pdf_arc($this->pdf,$arc_x,$arc_y,$arc_radius,$arc_start,$arc_end);
			if($fill == "fill"){
				pdf_fill_stroke($this->pdf);
			} else {
				pdf_stroke($this->pdf);
			}
		}

//draw filled or stroked curves.
		function curve($curve_x1,$curve_y1,$curve_x2,$curve_y2,$curve_x3,$curve_y3,$curve_x4,$curve_y4) {
			//$curve_y1 = $this->height - $curve_y1;
			//$curve_y2 = $this->height - $curve_y2;
			//$curve_y3 = $this->height - $curve_y3;
			//$curve_y4 = $this->height - $curve_y4;
			pdf_moveto($this->pdf, $curve_x1, $curve_y1);
			pdf_curveto($this->pdf,$curve_x2,$curve_y2,$curve_x3,$curve_y3,$curve_x4,$curve_y4);
			pdf_stroke($this->pdf);
		}

//draw a polygon
		function polygon($side_pairs,$fill) {
                             pdf_save($this->pdf);
				$foo = ";";
				$points = split($foo, $side_pairs);
				$numElements = count($points);
				for ($i=0;$i<$numElements;++$i) {
							$foo = ",";
							$pairs = split($foo, $points[$i]);
								$x[$i] =  $pairs[0];
								$y[$i] =  $pairs[1];

				}
				$numElements = count($x);
							pdf_moveto($this->pdf, $x[0], $this->height - $y[0]);
				$i = $numElements;
					do {
						pdf_lineto($this->pdf, $x[$i - 1], $this->height - $y[$i - 1]);
						--$i;
					} while ($i>1);
                             pdf_closepath($this->pdf);

								if($fill == "fill"){
									pdf_fill_stroke($this->pdf);
								}
								else{
									pdf_stroke($this->pdf);
								}
                             pdf_restore($this->pdf);
		}

//functions to display images
		function place_gif($image,$image_x,$image_y,$scale) {
				$image = $this->doc_root.$image;
				$size = GetImageSize ($image);
				$image_y = $this->height - $image_y - $size[1];
				$im = pdf_open_gif($this->pdf, "$image");
				pdf_place_image($this->pdf, $im, $image_x, $image_y, $scale);
				pdf_close_image($this->pdf, $im);
			if($url){
				$ll_x = $image_x;
				$ur_x = $image_x + $size[0];
				$ll_y = $image_y;
				$ur_y = $image_y + $size[1];
				$this->weblink($ll_x,$ll_y,$ur_x,$ur_y, $url);
			}
		}

		function place_jpeg($image,$image_x,$image_y,$scale,$url) {
				$image = $this->doc_root.$image;
				$size = GetImageSize ($image);
				$image_y = $this->height - $image_y - $size[1];
				$im = pdf_open_jpeg($this->pdf, "$image");
				pdf_place_image($this->pdf, $im, $image_x, $image_y, $scale);
				pdf_close_image($this->pdf, $im);
			if($url){
				$ll_x = $image_x;
				$ur_x = $image_x + $size[0];
				$ll_y = $image_y;
				$ur_y = $image_y + $size[1];
				$this->weblink($ll_x,$ll_y,$ur_x,$ur_y, $url);
			}
		}

		function place_png($image,$image_x,$image_y,$scale) {
				$image = $this->doc_root.$image;
				$size = GetImageSize ($image);
				$image_y = $this->height - $image_y - $size[1];
				$im = pdf_open_png($this->pdf, "$image");
				pdf_place_image($this->pdf, $im, $image_x, $image_y, $scale);
				pdf_close_image($this->pdf, $im);
			if($url){
				$ll_x = $image_x;
				$ur_x = $image_x + $size[0];
				$ll_y = $image_y;
				$ur_y = $image_y + $size[1];
				$this->weblink($ll_x,$ll_y,$ur_x,$ur_y, $url);

			}
		}

//draw a horizontal rule by amount of pixels or by percent of page or to margins
		function hr($w, $n, $leading,$lw) {
			#	draw by percent
   				pdf_save($this->pdf);
				#pdf_setlinewidth($this->pdf, $lw);
			if ($n == "%") {
				$w = $w * .01;
				$w = $w * $this->width;
				$space = (($this->width-$w)/2);
				pdf_moveto($this->pdf, $space, $this->position[y]);
				pdf_lineto($this->pdf, ($w + $space), $this->position[y]);
				$this->position[x] = $w;
				pdf_stroke($this->pdf);

			#	draw by pixels
			} elseif ($n == "px") {
				$w += $this->position[x];
				pdf_moveto($this->pdf, $this->position[x], $this->position[y]);
				pdf_lineto($this->pdf, $w, $this->position[y]);
				$this->position[x] = $w;
				pdf_stroke($this->pdf);

			#	to margins
			} elseif ($n == "mrgn") {
				$w = $this->width-$this->margin;
				pdf_moveto($this->pdf, $this->margin, $this->position[y]);
				pdf_lineto($this->pdf, $w, $this->position[y]);
				$this->position[x] = $w;
				pdf_stroke($this->pdf);
			}
		pdf_restore($this->pdf);
        $this->nl($leading);
		}

//draw a vertical rule
		function vr($h,$col_1 = "") {
			if (!$col_1) {
				$col_1 = $this->margin;
			}
			$h = $this->height-$h;
			pdf_moveto($this->pdf, $col_1, $this->position[y]);
			pdf_lineto($this->pdf, $col_1, $h);
			pdf_stroke($this->pdf);
		}

//end of graphics stuff


//Un-documented stuff
		function weblink($ll_x,$ll_y,$ur_x,$ur_y, $url) {
			pdf_set_border_style($this->pdf,solid,0);
			pdf_add_weblink($this->pdf, $ll_x,$ll_y,$ur_x,$ur_y, $url);
		}

//end of Un-documented stuff

//automatic new page
		function newpage($force = false) {
			if ($this->position[y] < $this->margin || $force == true) {
				$this->endpage();
				pdf_begin_page($this->pdf, $this->width, $this->height);
				pdf_add_outline($this->pdf, "Page ".$this->pagenum);
				$this->position[x] = $this->margin;
				$this->position[y] = $this->height-$this->margin;
				pdf_set_font($this->pdf, $this->myfont[face], $this->myfont[size], "winansi",1);
				pdf_set_text_pos($this->pdf, $this->position[x], $this->position[y]);
			}
		}

//forced new page
		function pagenew() {
			# this is the same as newpage().. i left it here for backward compatability.
			# all this function does is call this->newpage() with the force var set.
			/*
				$this->endpage();
				pdf_begin_page($this->pdf, $this->width, $this->height);
				pdf_add_outline($this->pdf, "Page ".$this->pagenum);
				$this->position[x] = $this->margin;
				$this->position[y] = $this->height-$this->margin;
				pdf_set_font($this->pdf, $this->myfont[face], $this->myfont[size], "winansi",1);
				pdf_set_text_pos($this->pdf, $this->position[x], $this->position[y]);
			*/
			$this->newpage("force");
		}

		function endpage() {
			$datestr = shortdate();
			if ($this->numfooter) {
				$w = pdf_stringwidth($this->pdf, "Page ".$this->pagenum);
				$this->position[x] = $this->move(($this->width-$w)/2, ($this->margin / 2));
				pdf_show($this->pdf, "Page ".$this->pagenum);
				$w = pdf_stringwidth($this->pdf, $datestr);
				$this->position[x] = $this->move(($this->width-$this->margin)-$w, ($this->margin / 2));
				pdf_show($this->pdf, $datestr);
			}
			++$this->pagenum;
			pdf_end_page($this->pdf);
		}

		function fax_header($name,$phone) {
			$datestr = shortdate();
			if ($this->fax_header) {
			$w = pdf_stringwidth($this->pdf, $datestr);
			$this->position[x] = $this->move($this->margin*.75, $this->height -($this->margin*.25));
				pdf_show($this->pdf, $datestr);
			$w = pdf_stringwidth($this->pdf, $name);
			$this->position[x] = $this->move($this->width*.5-($w/2), $this->height -($this->margin*.25));
				pdf_show($this->pdf, $name);
			$w = pdf_stringwidth($this->pdf, $phone);
			$this->position[x] = $this->move($this->width-($w)-$this->margin*.75, $this->height -($this->margin*.25));
				pdf_show($this->pdf, $phone);

			}
		}

		function close() {
		#	end and close the current pdf doc
			$this->endpage();
			pdf_close($this->pdf);
		#	close file pointer
			fclose($this->fp);
		}

	#	End class Definition
	}
?>
Return current item: PDF documents generates