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 : How To Create a PDF Using PHP
Categories : PHP, PDF, PHP Classes, HTML and PHP Click here to Update Your Picture
Joshua Walcher
Date : Jun 03rd 2008
Grade : 4 of 5 (graded 3 times)
Viewed : 5831
File : 4804.zip
Images : No Images for this code example.
Search : More code by Joshua Walcher
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

As a web developer or site owner, you may find that you need to create a PDF on the fly. For example, in the past I've had requests for a PDF app for business card generators, invoices, customizable letters/letterhead, and for a presentation of sales numbers. I would like to show you a basic example of how to create a PDF using a PHP Class called EZPDF. I have included all the files you'll need in the zip file attached to this example. However, I'm not going to go through the EZPDF files in my review mainly because they have separate documentation, but also because, frankly, if you're a beginning PHP programmer, you'd most likely be lost in seconds.

We're going to be making an application for a photo description contest. We'll show the users a picture and give them an opportunity to include a title and a subtitle/description. It would be VERY easy to allow users to upload their own pictures, but I'll leave that to you to decide. For now, it's a picture of my niece taken by my 3 year old's 1MP camera.

If you're like me and want to see the example first, you may view this example at: http://www.pkob.info/pdf/

First we need an PHP file for the user to start from.

-------------- index.php --------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Josh's PDF Example #1 - Picture Title & Description</title>
<style type="text/css">
#Layer1 {
    position:absolute;
    width:344px;
    height:257px;
    z-index:1;
    left: 437px;
    top: 101px;
}
body,td,th {
    font-family: Verdana;
    font-size: 14px;
}
.style1 {
    font-size: 24px;
    font-weight: bold;
}
-->
</style>
</head>

<body>
<form action="create.php" method="post" enctype="multipart/form-data">
  <p align="center" class="style1">Join the fun! Add a title and description for this picture! </p>
  <p align="left">#1) Title <br />
    <label>
    <input name="header" type="text" style="height:30px; font-size:28px; font:Helvetica,Verdana;" id="header" />
    </label>
  </p>
  <p align="left">#2) Add Description </p>
  <p align="left">
    <text area name="body" cols="45" rows="15"></text area>
  </p>
  <div id="Layer1"><img src="jawbone.jpg" width="346" height="262" /></div>
  <p align="left">#3) Click Button </p>
  <p align="left">
    <input name="input" type="submit" value="Create PDF" />
  </p>
  <p align="left"> </p>
  <p> </p>
</form>
 
</body>
</html>


There was absolutely no PHP in that code, so feel free to name it with an HTM or HTML extension if you like. All we did there was show the picture that the user will be describing and give them a chance a describe it before clicking a button labeled "Create PDF".

Now we need a PHP file to catch the submission and do something with it.
The only validation I'll be performing is the removal of '<' and '>' to eliminate hackers from trying to use malicious code. If someone forgets to type something into either of the two input fields, we'll print nothing.

-------------- create.php --------------

<?php
# Catch the posted data, validate by getting rid of > and <

$header =$_POST['header'];
# Replace > and < with a blank space
$header = str_replace('<','',$header);
$header = str_replace('>','',$header);
$body =$_POST['body'];
# Replace > and < with a blank space
$body = str_replace('<','',$body);
$body = str_replace('>','',$body);

# Include the file that does all of the work
include ('class.ezpdf.php');

# Start a new PDF file
$pdf =& new Cezpdf();

# Add the Contest's JPG to the PDF and place it in a specific position
# This particular command requires you to have the PHP GD Library.
$pdf->addJpegFromFile('jawbone.jpg',199,$pdf->y-200,200,0);

# Select the font we'll be using. There are more fonts in the zip file.
$pdf->selectFont('./fonts/Helvetica.afm');

# include the header, then move down a couple of lines, font size 25
# justification centered (centred if you're from the UK)
$pdf->ezText($header . "\n\n",25,array('justification'=>'centre'));

# include the body after moving down 7 lines to get under the pic.
# Font size 16, justification centered.
$pdf->ezText("\n\n\n\n\n\n\n" . $body,16,array('justification'=>'centre'));

#create the pdf and stream it to the page
$pdf->output();
$pdf->ezStream();
?>




Class to build a select tag in html, useful to build select boxes from a data base
Categories : PHP, HTML and PHP, PHP Classes
Vote-Poll script that has a wrapper class that allows the user to create multiple polls on the same page with little trouble.
Categories : PHP, PHP Classes, HTML and PHP
Automatic generation of HTML code for a table. OO interface. Can define colspan, rowspan, table style, cell style, and data style. Simple, but effective.
Categories : PHP, PHP Classes, HTML, HTML and PHP
PDF class - This is a useful class to make a pdf file with php functions.
Categories : PHP, PDF, PHP Classes
class formHTML build your HTML Forms from PHP
Categories : PHP, PHP Classes, HTML and PHP, HTML
Customizable Calendar Class
Categories : HTML and PHP, Date Time, PHP, PHP Classes, Calendar
XTemplate, a template class for PHP
Categories : PHP Classes, HTML and PHP, PHP
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP
PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use!
Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP
phpFormGenerator for Dynamic Form Generation from MySQL
Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP
Url To Pdf Report By Remote Application
Categories : PHP, PHP Classes, PDF, CURL
Simple class to build tables with style sheets
Categories : HTML and PHP, PHP Classes, PHP
A simple class with some HTML output functions that would come in handy for consistent page layout etc.
Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation
very simple ftp class
Categories : PHP, PHP Classes, FTP
 Brian Tafoya wrote : 1772
Nice submission... but only one detail; Where does one go to find ezpdf? :)
 
 Joshua Walcher wrote : 1773
I attached a zip file to this example that has the EZPDF files.
 
 Harold Tor wrote : 1774
Thank you so much for this script!! I will definitely go try it out! :)

Thanks!!! :))
 
 Tim Mikula wrote : 1775
How does it compare against fpdf (www.fpdf.org)?
 
 Joshua Walcher wrote : 1776
I have used both, and I can say that in my opinion, EZPDF is a little easier to use and get started with.  
Way back when I downloaded both and tried to get started with FDPF, but found myself frustrated by the 
sheer enormity of everything I could be doing and the bad documentation to get me there. With EZPDF, 
everything just made sense.  

Still, let's be honest. I doubt a professional printer would think either FPDF or EZPDF is good enough 
for production-quality image manipulation for things like vector fonts and image resizing.  They'd 
probably accept its quality for business cards and for letterhead, but anything else would probably 
turn them off. 

EZPDF is an excellent (and free, just like FPDF) product for simple tasks like invoicing, billing, 
and printing database contents, though, and that's what I use it for. If you find yourself trying 
to please a print shop owner, get in touch with me and I'll give you some places to find some good 
alternatives. It seems like I get one or two print shop owners wanting a custom web app every single 
year.
 
 Lauri Laatikainen wrote :1783
Hi, 

any chance I could get a peek of those "some good 
alternatives" cause i'm really am trying to please a print 
shop owner whose is being a quite nasty word.


anychance you could email me to latikainen at gmail.com ?