//jerrywalsh.org

coding, hacking, startups, computer security, technology and more

Euro Symbols on Dynamically Generated PDFs With PHP

Recently it became necessary for me to display a euro symbol in a PDF which was being generated using PDFlib on PHP.  After some trials and tribulations between googling for solutions and experimenting with different techniques I finally figured out what the problem is.

Firstly, the euro symbol can be passed to PDFLib as chr(128) but this will only work if the font has been loaded using the correct encoding. There’s two methods of using PDFLib, the non-OOP approach:

$font = pdf_findfont($pdf, "Helvetica-Bold", "host", 0);

and the OOP method:

$font = $objPDF->load_font("Helvetica", "host", "");

The problem with the above methods of loading the fonts is that the encoding is determined by the OS which in FreeBSD is something other than winansi. So the solution is simple, modify your font loading call accordingly, specifying winansi as the encoding.  For the non-OOP approach:

$font = pdf_findfont($pdf, "Helvetica-Bold", "winansi", 0);

or the OOP method:

$font = $objPDF->load_font("Helvetica", "winansi", "");

I hope somebody finds this useful - there’s not much information on googles search index which explains how to do this!

TextBuddy Now Supports Meteor!

Yes indeed.. it’s been awhile, but it’s finally here…

TextBuddy v1.1.41 has been released and I’m delighted to announce that Meteor is now supported!

Scorpion and the Turtle

A turtle was happily swimming along a river when a scorpion hailed it from the shore.

“Dear friend turtle!” called the scorpion. “Please let me climb upon your back and swim me to the other side of the river!”

“No,” replied the turtle, “for if I do, you shall sting me, and I shall die.”

“Nonsense!” replied the scorpion. “If I kill you in the middle of the river, you shall sink, and I shall drown and die with you.”

The turtle thought this over, and saw the truth of the scorpion’s statement. He let it upon his back and began swimming towards the other side of the river. Halfway across, he felt a sharp pain in the back of his neck.

“Why have you stung me?!” cried the turtle as his body began to stiffen. “Now you shall die as well!”

“Because it is in my nature. I can not do different.” replied the scorpion as the turtle sank beneath the waters.

Quote of the Day: Perfection

It seems that perfection is reached not when there is nothing left to add, but when there is nothing left to take away. – Antoine de Saint-Exupéry

Photos From Lermoos, Austria, Jan 2009

The #2 Black (my favourite) at the top of Lermoos:
Sunset on Mount Zugspitze - highest mountain in Germany:
Heavy snowfall:
Awesome off-piste terrain at Lermoos:

Load Balanced MySQL With Failover Using PHP

Simple MySQL database redundancy (with load balancing) can easily be obtained using PHP:

function connectDatabase($sDatabaseName) {
  $sUsername = 'myusername';
  $sPassword = 'mypassword';
  // database server hostnames
  $arrDBServers = array(
    "192.168.100.101",
    "192.168.100.102",
    "192.168.100.103",
  );
  shuffle($arrDBServers);

  // While we have available servers which we can connect to...
  while (count($arrServers)) {
    $sHostname = array_shift($arrDBServers);
    if ($resSQL= @mysql_connect($sHostname, $sUsername, $sPassword, true))
      break;
  }

 if ($resSQL)
   if (mysql_select_db($sDatabaseName, $resSQL))
     return $resSQL;

 // if we get here, something is very wrong!
 // you should replace this line with your own error handling
 die( mysql_error() );
}

Once implemented, ensure you modify your php.ini accordingly to allow failover to happen in a timely manner in the event of a connection timeout..  In a default PHP installation the mysql_connect_timeout is set to 60 seconds, I changed this on my install to just 1 second. Don’t forget to restart apache once you’re done modifying php.ini !

I hope you will find this snippet useful when your web app starts to get busy!