How to modify PDF files, inserting text and image / signature

This post is not related to PHP nor software development, it’s just a a simple tip about speeding up a small everyday task I’ve been having:

Adding text and signature (image) to a PDF (definitely more practical then print + fill it in + sign + scan and send it back).

PDF-XChange Viewer (v 2.5 free, for windows) is a simpler alternative to PDF editing software, and is able to add images (including your scanned signature) and text into a PDF, and save the changes. If the link is broken or not available, or the app is not free anymore, maybe I still have a copy on my dropbox.

Steps to add a signature

  • (only the first time) Tools -> Comment and markup tools -> show Stamps Palette. Create new collection -> From Image
  • Tools -> Comment and markup tools -> Stamp Tool -> [select your signature]

Steps to add text

  •  Tools -> Comment and markup tool -> Typewriter tool

Last time I found a free sofware able to do that I then forgot the name and I was unable to find a valid one. Next time it happens I’ll search in my blog first 🙂

Xdebug debug with Netbeans on Linux

XDebug is a free PHP extension to debug variables of your applications. Combined with Netbeans for PHP debug features (break points, variable watching and flow execution navigation, see screenshot on the left), it allows to have a complete debug environment.

What it’s really interesting is the possibility to place breakpoints in any file, even though not called directly in the local URL. That is required when working with web applications with URL routing/rewriting sudh as MVC applications (Zend Framework, Symfony etc..)  where the PHP code in a controller action does not match with its path in the local URL.

Here are the easy steps to follow in order to configure Xdebug for Linux Ubuntu 10.04 LTS. I’ll write the full commands to make it easier by copy & paste.

-) download the xdebug extension

sudo apt-get install php5-xdebug

-) check where the extension is installed

sudo find /usr/lib/php5 | grep xdebug

-) open the php.ini file (inside apache)

sudo vim /etc/php5/apache2/php.ini

and add these lines at the end of the file (replace with the right module path)

[xdebug]
;zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so
zend_extension=/usr/lib/php5/20090626/xdebug.so
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000

note1: xdebug.so could be elsewhere. use find /usr/lib -name xdebug.so to find the correct path
note2: see other xdebug settings

-) restart apache

sudo /etc/init.d/apache2 restart

-) Netbeans: tools->options->PHP
Tick ”Watches and Baloon Evaluation” (*), Untick “Stop at first Line” (not really useful except when debugging the application bootstrap).

(*): Xdebug is unstable with this options, however I found it very useful and enough stable for my purposes. It’s possible that Netbeans crashes with the current version, restart it in case.

5) Netbeans: right click on the project ->properties->Run configuration -> run as local website. Insert the (virtualhost) local URL. Do not insert any index file if the application uses URL routing, or Netbeans will launch the relative script path in the URL.

6) Netbeans: place your breakpoints by clicking on the line number, then start the debug (Under Debug->debug or Ctrl+F5). Netbeans will open the browser at the local URL, and when reaching the breakpoitns, netbeans will show a red icon on the line.
Depending on the browser, the focus will return to Netbeans (go manually if it does not).
Select the code / expression to watch/evaluate. Netbeans will show a baloon tip with the value. Alternatively, right click and select “new watch” to have the expression and the values appearing in the debug panel.
Use F5 to go to the next breakpoint, F7/F8 to step into/over.

As mentioned, Xdebug is unstable with the option set in the step 4. If watching some expressions crashes netbeans, use the “new watch” option instead.

Chrome and Internet Explorer ignore Windows hosts file

I’ve recently had some issue with local websites (in addition to ” localhost”->127.0.0.1). Chrome and Internet Explorer didn’t use the hosts file to solve the other local addresses (e.g: “zendapp.localhost”->127.0.0.1) I’ve previously set.

I’ve had this problem on Windows Seven. After some googling its seems that the issue is not OS-dependent but it’s a default setting, that developers do not like 🙂

Solution: open Internet Explorer, got to “Internet options” -> “Connection” -> “Local area network (LAN)” and untick “Automatic configuration”

Upload a tree of files and subdirectory to a remote FTP server

I’ve recently been asked to move a site (1Gb of files) from a Italian hosting linux (no ssh) to the same cheap hosting but to another server windows (damn ! no ssh, no console commands from PHP, the only way to upload files is the old slow uncool FTP).

Problem: I’ve got an extremely poor upload bandwith in my house in UK. So, I’ve used another hosting with SSH (dreamhost, in US) and:
1) got data from the 1st hosting (step1). Uploaded a php files that runs “tar -cf all.tar *”m, executed a “wget” to get the tarball, extracted the tarball.
2) uploaded to the second Italian windows hosting (step2).
ncftpput -R -v -u “username” ftp://ftp.site.it /fpt-root-folder .

UPDATE: To download the site recursively: use ncftpget. Digit “ncftp” and press “tab” to see other commands

Weird tricky situation, isn’t it ?

How to search complex source code using regular expressions

Today I needed to search the source code that ware calling a function with a complex array of parameters

URL::Make( ‘site/store.inc.php’, array( ‘action’=>’view’,’id’=> [… STH…] ,’idTwo’=> [… STH…] ) );

Considering this requirements:

– there are lots of similar function (to exclude from the search) with an argument less or an argument more

– there may are additional spaces in the line

– some other delopers have probably used double quotes instead of single quotes for array keys

– the value of the keys may be a PHP code

– every function call is in one line

An elegant solution is to search using regular expression. Netbeans (my favorite editor) supports POSIX extended regular expression and is very quickly to search a complex epression in a huge amount of source code.

Solution 1

#using char length and patter “.{0,MAX}” (that is: every sequence, MAX maximum chars). Easy !

URL::Make.{0,10}site/store.{0,40}action.{0,9}view.{0,20}id.{0,200}idTwo.{0,30}).{0,10})

Solution 2

#using regexpr for separators. more complex. more accurate in some cases but fails in some cases (eg: comments inside)

URL::Make.*([ ]*[‘”]site/store.inc.php[‘”][ ]*,[ ]*array[ ]*([ ]*[‘”]action[‘”][ ]*=>[ ]*[‘”]view[‘”][ ]*,[ ]*[‘”]id[‘”][ ]*=>.{0,100}[‘”]idTwo[‘”][ ]*=>.{0,100}[ ]*)[ ]*)

UTF8 in LAMP applications: overview and how to solve the common issues

The problem
In a LAMP application the text is frequently saved/retrieved from/to a database and files. We must consider all the different encoding (mapping characher-byte value): latin1 iso8859-1, latin9, UTF-8 (utf8) etc…
Lots of applications use ISO8859 encoding and some PHP functions to convert the characters (htmlentities, htmlspecialchars etc…)
The solution
Converting all the text from an encoding to another using PHP functions is unsafe, difficult and annoying.
Ad example the character “é” (encoded as iso8859) will printed as “é” if it’s supposed to be encoded as utf8.
The solution is to use only one charset for files, Content-Type of the pages and the db. UTF-8 [wiki] is the best choice: a variable-lenght char encoding for the standard Unicode. If you use this charset in the HTML file, it won’t need to convert the characters to the respective entities.
How To use UTF-8
  • set your IDE to save and open source files using utf8 encoding
  • set the content-type of your application to utf-8 (better a apache/htaccess rule instead of the meta tag).
  • set the database server to use utf8 encoding (also tables must be converted). If the db is utf8 but client encoding is latin1, execute first of all the query “SET NAMES utf8”
  • if the application was using latin1 and PHP convert functions, remove all the existing function to encode/decode special characters/entities.