WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PDF class - This is a useful class to make a pdf file with php functions.
Categories : PHP, PDF, PHP Classes Click here to Update Your Picture
Diana Ortega
Date : Jun 09th 2003
Grade : 4 of 5 (graded 9 times)
Viewed : 10185
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Diana Ortega
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

To get it work you need the PDFlib that could be download from www.pdflib.com
In Unix you need to install the PDFlib and compile the php with PDFlib support.
In Windows you need to copy the pdflib.dll to the system32 folder.

<?php
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.



How To Create a PDF Using PHP
Categories : PHP, PDF, PHP Classes, HTML and PHP
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Url To Pdf Report By Remote Application
Categories : PHP, PHP Classes, PDF, CURL
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
Authorize.net AIM Interface Class v1.0.0
Categories : PHP, PHP Classes, Ecommerce, Payment Gateways
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
News management class
Categories : PHP, PHP Classes, Beginner Guides
The class to check load time of your script VERY usefull for relatively slow applications, but not only..
Categories : PHP, PHP Classes, Debugging
Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself.
Categories : PHP, PHP Classes, Templates, Complete Programs
Simple Template Class/Example
Categories : PHP, Templates, PHP Classes
RSS parser. Parses RSS into an array. Quick and nasty but does the job. No checking is done for correct Tags, only correct XML. PHP4 needed to display result (uses print_r).
Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS)
MS Word Mail Merge Automation (COM)
Categories : PHP, PHP Classes, COM
Very minimal templating engine
Categories : PHP, PHP Classes, Templates
ElfReader: An ELF (Executable and Linking Format) header information in PHP. Shows how to use the UNPACK function to read data.
Categories : PHP, Linux, PHP Classes
an example of the cyberlib payment class
Categories : PHP, PHP Classes, Ecommerce, Credit Cards
 matthew waygood wrote : 978
PDFlib licence is US$450 - to have the DEMO removed from your creations.

So I don`t think I would ever get to use your example.

Perhaps an example using the FPDF class from http://www.fpdf.org would be better (ITS FREE, SMALLER AND DOESNT REQUIRE INSTALLATION).
 
 Indeo Indeo wrote : 1029
Fatal error: Call to undefined function: bof() in g:\usr\krasnal\www\test\pdf.php on line 255
 
 David Perez wrote :1031
The PDFlib lite is for free, it comes in the ports install from FreeBSD