Create PDF without using PHP PDFlib
I never used PDFlib on my website because the extension isn’t available in the PEAR Modules of my web host. PDFlib is a PHP extension for generating PDF documents dynamically. PHP extensions need to be installed separately apart from PHP. In my one year experience with PHP, I only used GD extension to generate images for use with CAPCHA and other random images. Therefore I don’t have enough experience to talk about these extensions.
Recently when I was searching for some useful PHP tutorials, I stumble across a PHP class that can generate PDF documents without PDFlib extension. I thought this is very useful since my web host doesn’t provide the extension. It’s called FPDF. After some experiments by following its tutorials, I felt it’s not too difficult to use once you know the Object Oriented Programming way of PHP. The class also has dozens of extended classes. They can be copied and used as the extensions of FPDF. One thing that I don’t like about is its website. When I went to the site, the first thing I noticed was that it uses iframe. I don’t like iframe since it isn’t bookmarkable. I wonder why it doesn’t use PHP include instead of iframe since this is a PHP-based website. Fortunately, I can open the frame in a new tab by right-clicking appropriate frame in Firefox so I can add the Scripts page to Update Scanner to check new scripts update.
Site Menu Animation Prototype
I have been playing around with MooTools recently and came up with an idea to allow users to change the orientation of the menu for my updated site with animation. MooTools is an Object Oriented JavaScript framework similar to jQuery. The main reason I chose this framework instead of jQuery is that it can easily create customized animations. Although I’m still learning how to use MooTools’ full features, I wrote a HTML page that toggles between vertical and horizontal menu with custom animations just by using MooTools framework. Note that this is a prototype to the upcoming updated site menu. It only contains the background color and doesn’t contain any content. Here is the MooTools code for the page:
window.addEvent("domready", function() {
$('hori').setStyle('opacity','0');
$("change").addEvent("click", function() {
$('change').setStyles({
'visibility':'hidden',
'opacity':0
});
var changeMenu = new Fx.Tween($('menu'));
changeMenu.start('width','50px')
.chain(function() {
changeMenu.start('height','400px');
})
.chain(function() {
changeMenu.start("left", "10px");
})
.chain(function() {
changeMenu.start("top", "40px");
})
.chain(function() {
$('hori').fade('in');
});
return false;
});
$("hori").addEvent("click", function() {
$('hori').setStyles({
'visibility':'hidden',
'opacity':0
});
var changeMenu = new Fx.Tween($('menu'));
changeMenu.start("top", "50px")
.chain(function() {
changeMenu.start("left", "100px");
})
.chain(function() {
changeMenu.start('height','50px');
})
.chain(function() {
changeMenu.start('width','600px');
})
.chain(function() {
$('change').fade('in');
});
return false;
});
});
Notice that I used CSS property names and MooTools functions to create animations. You can view the whole source code by right-click the prototype page since it is just a plain HTML and JavaScript page.
I created this prototype with the help of MooTools 1.2 Beginner’s Guide and MooTools Docs.
Starting to learn C Programming Language
I have decided to learn a new programming language during this summer break. I chose C because I have already learned the basics of C++. The C++ programming language is the object oriented version of C after all. Another reason I chose to learn C because the source code for many of the open source projects that I’ve read are written in C language. It seems that the language is very popular in open source community along with C++ and Java. I’m starting with VTC C Programming video tutorials. This is the first video tutorials that I came across when I was searching for C programming language tutorials online. Therefore I downloaded it and am beginning to watch and learn C language. I will post any tips that inspired by the tutorials as I learn instead of post what I learned from the tutorials.
Here are the ISO images of this video tutorial series if you are a copylefter or don’t care about the copyright:
http://www.downtr.net/162643-vtc-c-coding-video-tutorial.html
HTML Template PHP class
This is my first fully-written PHP class to ease my HTML page creation process by pre-set the DOCTYPE and other required tags in a normal HTML page. The class is inspired by this article where it talks about wp_header and wp_footer of WordPress. I thought that this is a great exercise to increase my OOP (Object-Oriented Programming) skill. Therefore I wrote the following code based on my creation habit for HTML pages:
<?php
class template {
public $title;
public $style;
function header($headerContent="") {
echo "<!doctype html>n";
echo "<html>n";
echo "<head>n";
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
echo "<title>";
echo $this->title;
echo "</title>n";
if (isset($this->style) || !empty($this->style)) {
echo "<link href='{$this->style}' rel='stylesheet' type='text/css' />n";
}
// Output additional header content if available
if (function_exists($headerContent)) {
$headerContent();
}
echo "</head>n";
}
function body($bodyContent="") {
echo "<body>n";
// Output body content
if (function_exists($bodyContent)) {
$bodyContent();
}
}
function script($script='') {
if (strstr($script, ".js") || strstr($script, "://")) {
echo "<script type='text/javascript' src='{$script}'></script>n";
}
else {
if (strstr($script, "()")) {
$function = str_replace("()", "", $script);
if (function_exists($function)) {
$function();
}
}
else {
echo "<script type='text/javascript'>n";
echo $script;
echo "</script>n";
}
}
}
function footer($footerContent="") {
if (function_exists($footerContent)) {
$footerContent();
}
echo "</body>n";
echo "</html>n";
}
}
?>
The header, body, and footer methods include an optional string variable. As you can see in the code above, this variable needs to be a function name. The function name in these three methods doesn’t require parentheses. However, the function name needs to have parentheses in the script method. The script method is different from the above three methods as it can be reused in the same HTML page. For example, the following is a simple page with the class applied:
<?php
$ca282 = new template();
$ca282->title = "Robby Chen CA 282";
$ca282->style = "styles/global.css";
$ca282->header("headerContent");
$ca282->body("bodyContent");
$ca282->script("http://www.google.com/jsapi");
$ca282->script("google.load('jquery','1')");
$ca282->script("jquery()");
$ca282->footer();
?>
Notice that the script method has three formats in the above example, external JS, on-line code, and a function. I didn’t pre-set the script tag between function because I need the syntax highlighting and auto-completion features in NetBeans. The following is my jquery() function in case you don’t know how to activate these two features within a PHP function:
<?php
function jquery() {
?>
<script type="text/javascript">
$(document).ready(function() {
$("#logo").html("Logo");
});
</script>
<?php
}
?>
Basically, the JavaScript code is outside the php tag but inside the PHP function. This way the script content will only appear after the jquery function is called.
You can download the class file here.