Zend_PDF

Tuesday, March 8th, 2011

The use of PDF documents to insure formatting to the end is almost universally understood. With the recent application that I have recently written and deployed for Larkin Mortuary there are some forms that already are PDF I just need to write info to the PDF and spit it out. For this application is not using Zend Framework completely. Just certain components. I use the Zend_PDF component here. Below is the php that opens, writes, and outputs the PDF document.

<?
//get our Zend stuff. Once we have gotten the Autoloader we can call Zend functions at will.
require_once ‘Zend/Loader/Autoloader.php’;
$autoloader = Zend_Loader_Autoloader::getInstance();

//Perform query and get information
$db = new PDO(‘mysql:host=localhost;dbname=dbname;’, ‘username’, ‘password’);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$case_number = $_REQUEST['case_number'];
$stmt = $db->prepare(“SELECT * FROM tables WHERE case_number=?”);
$stmt->execute(array($case_number));
$f = $stmt->fetch();

//Load our PDF and start the manipulation
$document = ‘./forms/ssa-721.pdf’;
$pdf = Zend_Pdf::load($document); //<- this creates a new instance of the Zend_Pdf class and loads the existing document.
$page1 = $pdf->pages['0']; //<- with Zend_Pdf you must specify the page that we are working with
$width = $page1->getWidth();
$height = $page1->getHeight();

//set Document Meta Data
$pdf->properties['Title'] = “SSA721 of {$f['decedents_given_names']} {$f['decedents_last_name']} {$f['decedents_suffix']}”;
$pdf->properties['Author'] = “Larkin Mortuary”;

//We must specify the Font and its size that we will be writing with.
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
$page1->setFont($font, 12);

//Draw our text, we have a bunch of things to draw
$DecedentInfo1 = “{$f['decedents_given_names']} {$f['decedents_last_name']} {$f['decedents_suffix']}”;
$page1->drawText($DecedentInfo1, 54, 705, ‘UTF-8′); // All drawn text requires three options. The actual text, the left starting place, measured in points from the left, and the bottom starting place, measured in points from the bottom.
$pdfData = $pdf->render();
header(“Content-Disposition: inline; filename={$f['decedents_last_name']}ssa-721.pdf”);
header(“Content-type: application/x-pdf”);
echo $pdfData;
?>

There are some things that are kind of annoying with this. Each life must be put out one at a time. Further, text does not wrap but is put out as one long line. You must use another function to break wrapped text up and feed that to Zend_Pdf. Also, it would be really nice to be able to feed Zend_Pdf an html page, and have that parsed and output as PDF. In the meantime this is a pretty easy way to manipulate, save, and create pdf’s.

If anyone has any hints or tips with Zend_PDF I would love to hear it.

PHP ini variables in apache config

Monday, February 28th, 2011

I think we all come across times where we need to have custom php.ini settings for a web-application. One such instance has come up with an application that I have written and recently deployed for Larkin Mortuary. We needed Zend Framework components and I needed session times to be longer. Now the second need is something that is very specific to just this application. There are other applications on this server that we would not want a longer session time.

So I decided to use the apache config file to get custom settings per application. I got a bunch of info from this php page http://php.net/manual/en/configuration.changes.php.

Now my apache config file contains the following:
<IfModule mod_php5.c>
php_value include_path “.:/usr/local/lib/php:/opt/ZendFramework-1.10.8/library”
php_value session.gc_maxlifetime “28800″
php_value session.cookie_lifetime “28800″
</IfModule>

We can see these changes taken place if we look at the output from phpinfo():
Directive                                   Local Value    Master Value
session.cookie_lifetime         28800              0
session.gc_maxlifetime         28800              1440

I also understand that you can have custom php.ini files for each application. But why when you are going to have a VirtualHost section for each application anyways?

First Experience with Zend Framework

Friday, September 3rd, 2010

I wrote a review of Zend Enterprise PHP Patterns a while back. While I read the book, I had yet to jump into the Zend framework and really make a project that used it, or any of it for that matter. While doing a project for a client I had need of making several forms. Nothing is more tedious to me than making forms. The html tags for labels, the form input, and then the PHP on top of it to give default values. This is OK when making a short form. But I get extra sick of it when doing a form with even ten or more fields. When faced with such a situation this last week I decided to try just a piece of the Zend Framework.

This is possibly my favorite thing about the framework, the ability to use as much or as little as you would like. It gives great flexibility and allows you to include parts into a currently existing project.

To get started, you need to tell your PHP application where zend is, in addition, starting the autoloader at this time really makes life easy. The following code does just that:

  1. set_include_path(get_include_path() . PATH_SEPARATOR . ‘<wherever Zend is>’);
  2. require_once ‘<wherever Zend is>/Autoloader.php’;
  3. $autoloader = Zend_Loader_Autoloader::getInstance();

With these two items in place you can just begin to call Zend Classes and Functions without another thought about it. I put this in my included header.php file, where I have all kinds of includes. Now my functions are available to me wherever I am.

To start your form it is very easy.

  1. $form = new Zend_Form();

That is is, we have started a form. Everything else will use the newly created $form object and go forward from there.

Next, you must add attributes and elements to your form. I found certain parts of this very poorly documented. I wish there was a list of acceptable options, etc for each form addition type but there isn’t. Maybe I should just not complain and write it, but I digress. Adding attributes is rather easy

  1. $form->setAction(‘./action.php’)
  2.      ->setMethod(‘post’)
  3.      ->setAttrib(‘id’, ‘form_id’);

Now adding form fields, a couple options here, both quite easy. First is to just use the $form object and add directly:

  1. $form->addElement(‘text’, ‘text_input’, array(‘value’ => $_REQUEST[‘text_input’], ‘label’ => ‘Text Input:’));

The arguments passed to the addElement method are type, name, array of  options.

The second option is to create a Zend_Form_Element object and add that to the $form object:

  1. $select_input = new Zend_Form_Element_Select(‘select_input’);
  2. $select_input->setLabel(‘Select Input:’)
  3.      ->addMultiOptions(array(
  4.           ‘Option0′ => ‘Option0′,
  5.           ‘Option1′ => ‘Option1′,
  6.           ‘Option2′ => ‘Option2′))
  7.      ->setOptions(array(‘value’ => $_REQUEST[‘select_input’]));
  8. $form->addElement($select_input);

The last bit, actually rendering your form. Some caveats here, Zend Framework components can be used outside of the Zend MVC environment, but you still must define a view in order for the form to actually come up. If you do not, you will get an exception. It isn’t hard though, the following will do it for you:

  1. $form->setView(new Zend_View());
  2. echo $form;

Viola! The form magically appears in front of your eyes. A couple things to remember, anything that can be attribute of an HTML form of input elements can be added to the Zend_Form in the options array. This includes, value, class, specifically defined ID, etc. I will be using Zend_Form much more often. It is so easy to get forms together quickly and easily.

Introducing Open By Design

Monday, May 24th, 2010

I want to take just a moment to introduce my company, Open By Design. There are many reasons that I made this company and picked this name.

First, the reason that I started this company. My job responsibilities at work took a sudden change in December. Basically instead of just being the ‘Computer Guy’ I now manager one of our satellite locations. With that I meet nearly every family that is served there, and go on all of their services. This has severely curbed my ability to dedicate time to computer issues. So, I made this company to offload work from my job, to my business, which saves overtime for them, and gets me extra money. Truth be told extra money is always good.

The name is really a reference to my passion about the use of open source software. Seemed better than Wild Smith Productions. In addition my goal is to leverage open source software in any way that I can for the clients that I have/get. I am really targeting small businesses where I can help with infrastructure and web design. We’ll see how it goes.

Webkit VS. Gecko

Monday, May 17th, 2010

This post may be better stated as Google Chrome VS. Firefox. I have found that I am not super pleased with any browser right now. I guess in part because what I want is simply to use the web and see pictures while I do that.

I have used Firefox for years, and rather like it, but it seems to me that we have reached the point where there are no new useful features to add so we just encumber the software with stuff. Add-ons, and obscure features that no longer behave the way that we anticipate. For example, Firefox used to ask me when I quit it if I wanted to quit, or save my session. Very useful, because sometimes I wanted the one and sometimes the other. Now however it only will let me have the last session that I had (yes, I realize that I can make it never do it in the settings, what I want is the choice, like I used to have). On occasion I find that I have enough tabs open that I just need to start fresh. I can’t find a way to do that anymore. It just is not an option.

Chrome is easy enough to use, and really has enough features to please the average joe. In addition for web development it has a built in ‘inspect element’ option in the context menu, which is almost like a firebug of sorts for the browser. But I find that Chrome is sometimes unstable. Occasionally the flash plugin will chew up 100% CPU for no apparent reason. I can kill it, and then I don’t get flash. Also I find that sometimes it just does not behave the same on a page as I would expect. Forms that behave differently on submission in Chrome than they do Firefox. Even then this behaviour is sporadic at best.

I guess all-in-all I want a browser that is functional and predictably stable. Does anyone have other browsers that they use that would perform the task at hand better?

Weird Injections in my Contact Forms

Thursday, May 13th, 2010

On one of my sites I have some standard contact us forms. These use the PHP mail() function to send the submitted information to a general purpose email account so that it can be handled correctly. Lately though I have been having something weird. On occasion instead of the mail being sent to the general purpose account it is sent to another account, one that is aliased to my user. At first I thought this was just spam that was posting straight to the PHP mail() function, but then I got some legitimate emails on it also. I have no idea how it is happening.

So my question to everyone is twofold. How are these people doing this and how do I stop it? I am baffled.

The lastest with Stackable

Monday, May 10th, 2010

I have been meaning to write this post for a long time, and since I couldn’t sleep tonight at 11:00 pm, and like so many others my best writing happens when I am tired and not thinking straight, here it goes.

I have written about stackable before here and here. The VPS-like solution offered by xmission. I am actually thinking of removing my colo and just using stackable for other things. They really do offer simple, no hassle but lots of control hosting here.

DNS Controller ScreenshotI wanted to highlight a couple of features if you are holding out. They now offer the DNS manager. It really is a simple yet powerful DNS tool available to any stackable customer. Also, you can do DNS for any domain that you have control over, not just the ones hosted on stackable.

Other useful tools include the IP Manager, which allows you to look at all the IP’s that you have. In addition, it will point out when you have IP problems or DNS conflicts, which has proven very useful.

An SSL Manager, for you know, SSL certificates that you have. It has signing requests, and certificates already signed.

And lastly in the new and improved tools FTP User management. Very handy for those that need to offer different or temporary FTP users.

Now, I want to emphasize what stackable is and what it is not. It is not a colo server, it is not a VPS. It is VPS-like. You are given a virtual machine that you do NOT have root access on. But you can host sites and control the virtual machine through the ‘dashboard’, a custom built web interface for Virtual Machine control. Currently stackable offers PHP and MySQL. But they do have plans to offer other hosted languages as well.

I really have to hand it to the guys at stackable. This really is as easy as it gets with web hosting.

On the death of IE6

Tuesday, March 2nd, 2010

I have seen many funeral services in my life. Of the ones I have seen it seems there basically are three kinds. Those that are sad, a lamentation for something taken to early, something stolen from us. At these types of funerals we see the greatest sorrow. Next is those that are quiet. This may sound like an odd term but at these services we either see that no one is there or those that are there don’t care. The services are usually short, and devoid of any sadness or joy. They are something bland and people are glad to have them over and out of the way. The last type of funeral service is Happy. Strange though it may seem at this service a celebration is had for a life well lived. Celebration is also given that the person or thing was worn out, suffering, ready to move on to whatever realm or sphere awaited them. These are often full of laughter, and although there is some sadness, it is usually overshadowed by a great sense of joy.

I am sure that the funeral of Internet Explorer 6 will be something like the last one. A celebration that the old, worn out, overused and under-supported browser will be gone. I know that I will be ecstatic when I can look at my stats and see that the number of IE6 browsers visiting my sites has diminished to the point that I don’t have to support the buggy and hackish code to make things look even semi right.

This has been a long time coming as we have watched IE6 wane. Like the hospital patient that the doctors don’t bother to stop by anymore I am sure IE6 felt this coming. If we watch closely we can trace the timeline of the ever growing demise.

First, Google drops IE6 support. Chine attacks google using an IE6 security hole and France and Germany encourage the discontinued use of IE6. Youtube follows suite and will stop support March 13th. Possibly the last nail in the coffin is Microsoft. Even though the creator of IE says they won’t discontinue the support of IE6, it is subverting itself by encouraging people to upgrade to IE8 with tricks like helping the hungry.

Truly, I am excited to see this spiraling death of IE6. I understand that some of the market share that the browser still has are people in large corporations. Where upgrading or using something else just isn’t an option and their IT staff just doesn’t seem to care. Maybe they are all fixing the PC Load Letter ticket requests or cleaning viruses. Who knows.

More on Stackable

Wednesday, February 24th, 2010

I know that I have harped on how much I enjoy this hosting solution. I have really enjoyed using the versions feature lately with my own stuff and as a sandbox for others as I work on their website.

But rather than me just going on and on let me point you to my friend Emily Higbee’s article on it.

More Stackable Features

Friday, February 19th, 2010

At the Utah PHP Users group meeting on Thursday night Mike Place presented on Stackable. I wrote a post on Stackable a couple of weeks ago. At tonights meeting not only did MP give a great overview of what Stackable is and gave out some really fun deadline dice, but he also talked about some new features that I think are pretty exciting.

First, my favorite new feature to be added in the next 6 to 8 weeks: DNS. Yep, stackable will have dns with full control through the dashboard. This will be a great improvement and super useful feature.

Second, integration of SVN, so you can have commits to your project that will push onto your webserver. When you are ready to deploy push that branch live using stackable’s simple versions feature and start a new branch for development. Super cool.

Last to talk about DJANGO stacks coming soon. MP wouldn’t give a for sure date but it is in active development. I don’t use django, but if you do you should be happy.

There were some other things. ROR stacks in the future. A resellers option in the future also. Possibilities of supporting different databases as well. Really, the guys at Stackable are willing to listen to ideas of what can be done to make this the best hosting solution ever. You should check it out.