DOMPDF in ZF2

If you’re using Zend Framework 2 (beta 4 beta 5) and need to easily generate PDFs, and you’d like to generate those PDFs from an HTML template, then boy do I have some good news for you!

For those of you who have dealt with generating dynamic PDFs, you know how much of a pain in the ass it can be (e.g. dealing with x, y coordinates, word-wrap (or lack of), etc.). There are a few options out there, some paid, some free, but none of them are as nice as DOMPDF (simple API, powerful, and free).

The true power behind DOMPDF comes from rendering standard HTML/CSS files instead of having to write ugly and unmanageable code.

Excerpt from their usage docs:

That’s it. That will take your HTML and stream a PDF file to the end-user. Awesome, right?

Ok, let’s bring this back around to ZF2. About a month or so ago I started using ZF2 beta4 for a new project that I’m working on and wouldn’t you know it, I have to generate PDF reports, receipts, etc. I had a choice; I could just drop in DOMPDF, ignore the conventions set by ZF2, and move forward with my work, or I could spend a little extra time and create a clean and elegant solution that my project,  future projects, and the ZF2 community could benefit from.

What I came up with is DOMPDFModule. A drop-in 3rd-party module that wraps DOMPDF and takes full advantage of ZF2′s implementation of the ViewModel pattern.

Example:

That’s it. The proper headers and template rendering happens behind the scenes. If you’d like the browser to prompt the user where to save the PDF file, call:

But wait there’s more! The installation of DOMPDFModule couldn’t be any easier, utilizing the increasingly popular PHP Composer dependency manager via Packagist repository. See README.md for installation instructions.

DoctrineORMModule and OracleSessionInit

I recently ran into an issue with date/time formats with ZF2, Doctrine (PHPComposer install), and Oracle where I would receive the following error:

Could not convert database value “2012-06-19″ to Doctrine Type date. Expected format: Y-m-d 00:00:00

The fix is easy but I couldn’t find it documented anywhere, so here it is:

Open up your module.doctrine_orm.local.php config file and under your di instances add the following:

Update: 07/09/2012

Please note that DoctrineORMModule no longer ships with “module.doctrine_orm.local.php”. The above config options should be placed in your config file (global.php or whatever you use).

Also note, the config keys have changed. The array should now look like this:

Cleaner Code in PHP 5.4

Produces

object(A)#1 (1) { ["options":"A":private]=> array(2) { ["name"]=> string(3) “Ray” ["sex"]=> string(4) “Male” } }

Zend, Twitter, and OAuth Made Easy

I have been working on a Twitter service as of late and ran into a problem that I am sure others have as well and ended up solving it in their own way. However, after searching high and low on the Internet for an elegant solution, I came up with nothing and found I was on my own. This post is for those of you who are, or will be, using Zend_Service_Twitter and need Twitter users to grant your application access to their account information in a secure way.

The project I am working on requires that users allow my Twitter service to access their account information. The only reason I require this is because this service makes several API requests over the course of time, and instead of having users use up all of my allotted API service requests, I use their’s instead, It’s a little more complicated than that, and a full explanation falls outside the scope of this post. All you need to keep in mind for this post is that I need to have access to their account so API calls are counted on their behalf and not my server’s.

Currently, Zend Framework 1.9 only supports basic authentication for the Twitter API, which works well, but leaves, or should leave, a bad taste in the end-user’s and developer’s mouth. I say this because a developer who wants access to a user’s account at a later time (e.g. if you are running scheduled API calls) will have to store the user’s credentials on the server. This means A) the user may be iffy about using your service since they may not trust your site and/or want their password stored on your server, and B) this creates more overhead for you because you now have to keep track of user information, keep it secure, etc. The way we get rid of this bad taste is to implement a system such as OAuth.

OAuth is an authentication protocol that allows secure API authorization for applications. What this means is that a user can grant you, the 3rd party developer, access to their information without providing you with their username and password. User login takes place at the source, in this case at Twitter.com. For more information about OAuth, please see the Twitter API Wiki / OAuth FAQ

If you are currently using the Twitter API, it is probably better to change over to using OAuth sooner than later since support for basic authentication will be going away in the future.

In order to get Zend_Service_Twitter working with OAuth, you will need to obtain OAuth via subversion since it is not in the currently released version of Zend Framework. It can be obtained here.

The code you are about the see takes place in my IndexController, meaning I am using Zend’s MVC setup. I have also put configuration information inline so you can see exactly what is passed into each object.

Solar e-mail helper that stops spammers

Tonight I created a helper for Solar that encodes mailto href addresses so SPAMers that scrape HTML pages will never get your addresses.

To use this helper, just do the following in your views/layouts:

Here is the helper code:

“The Party of ‘No’” says “Yes we can” to Obama’s web site design

obama_otten_logo_ripoff

Bangor Daily News has an article out right now about how Republican Les Otten, exploring the possibility of running for governor of Maine in 2010, has ripped off Barack Obama’s “O” logo. However, what they fail to point is the fact that Otten’s entire website is a rip-off.

The website not only has the same layout (navigation placement aside), but the font colors, font sizes, placement of content, and content borders are almost 100% the same.

To really get the full impact of how horribly ripped off this site/logo is, let me show you some screen shots–you be the judge.

www.barackobama.com as of July 1st, 2009
www.barackobama.com as of July 1st, 2009
www.lesotten.com as of July 1st, 2009
www.lesotten.com as of July 1st, 2009

What’s worse is the blatant logo ripoff. I have compiled a progression from Obama’s logo to Otten’s, increasing Otten’s logo by 25% opacity until it was 100% opaque. I also blew up the image so you could see it in more detail. I apologize for the pixelation.

Obama/Otten logo overlay as of July 1st, 2009
Obama/Otten logo overlay as of July 1st, 2009

After studying the logos on top of each other you notice a few things:

  • The “O”s both have starbursts around them, Obama’s has 40 “arms,” Otten’s has 50
  • The distance from the outer edge of the “O” to the beginning of the whitespace within is the same. The difference is that Otten’s has the stem that encircles his “O” making his border appear thicker.
  • The stripes are in the same quadrant of the circle and have the same perspective–from left to right.

If you saw Otten’s logo independent of his web site/campaign, you might assume this was Obama 2.0, the launch of a new PR push within the Obama administration. It’s no coincidence that Obama’s campaign won a couple of advertising awards from Cannes Lion, those guys knew what they were doing–you see the “O”, you think “Obama.”

According to the Bangor Daily News, Edith Smith, Otten’s spokeswoman, explained that the web site design firm “used standard industry templates and colors found on political Web sites nationwide.”

Here’s a random list of other politicians’ sites that meet Smith’s “industry standards” without reusing Obama’s layout and logo. In fact, I can’t seem to find another site that does.

Savant3 URI Plugin

I started working on a small project last weekend for a [H]ard|Forum member that would allow viewing maillog log info from a web site. I wanted to make an app that would have a small footprint and would be easy to manage (code base wise as well as UI). My first instinct was to use a MVC framework like Solar or Zend but those two have more features than what I needed for this project. Remember, I wanted a small footprint.

I decided to give Savant3 a shot. This at least allows me to separate my controller logic from my view logic. I only had one controller and one template so code overhead wasn’t an issue.

One of the things missing from this system was a way to easily handle URIs/URLs. Since Savant3 is a template engine, I wouldn’t necessarily expect a URI handler. However, I felt that having a plugin that handles URIs was worth the effort, even with a project of this size.

Since the plugin code is still a bit messy (missing inline docs, needs a little bit of refactoring, etc) I can only show you usage examples for now. I plan on cleaning up the code and releasing the plugin on this site shortly.

Here is how one might use this URI plugin in a Savant3 template:

The rest of the functionality works much like Solar_Uri (currently w/the exception of path and query behavior), being able to set each piece of the URI before returning a newly built URI.