PHP


Howto and PHP26 Mar 2008 05:08 am

When attempting to debug my program, zend studio for eclipse wasn’t recognizing that I had my mysql extension installed. I found a small work around.

Go to
[install_path]\plugins\org.zend.php.debug.debugger.win32.x86_5.2.12.v20071210\resources\php5

In there, you will see your php.exe’s and your php.ini that the debugger uses. I just copied my php.ini there for it to work.

Development and PHP19 Mar 2008 10:00 pm

After a few weeks of downtime, and helpful input and comments from my readers, I have finally submitted my Zend_Framework CLA and authored a Zend_Pdf_Cell proposal! Please feel free to read it and comment on any of the functionality! I really hope that this can get integrated within the Zend Framework, as I have put a lot of time into this and hope that the community can gain as much as possible from it.

I want to give everyone a great big thanks for the valuable feedback and excellent encouragement!

PHP28 Nov 2007 05:49 pm

Update! (3/31/2008): Now works with Zend Framework 1.5!
Update! (3/19/2008): I have submitted an official proposal for this to be included into the Zend Framework.
Update! (1/12/09): Dominik Deobald was kind enough to supply a UTF character encoding patch and bug fix. I have attached his fix as a .patch file at the end of the post.

I have recently been working with Zend_PDF to create PDF documents. One of the basic requirements for my project is the ability to center text within a screen. Needless to say, I was quite disappointed when I came to find out that Zend_Pdf doesn’t currently have any type of text layout support except for the exact position to place it.

I created a small extension to Zend_Pdf, and building off of an idea from FPDF, I created Zend_Pdf_Cell. Zend_Pdf_Cell is not currently supported by Zend, and is not officially in the Zend Framework, although I have posted my code for them to view it and give feedback (and hopefully have it incorporated into Zend Pdf). My first try I misunderstood parts of Zend_Pdf, but I have gone back and fixed it.

Features

These features have been most used by myself and mostly work.
* The ability to create a cell and place text in it.
* Specify the width and height of a cell
* Position a cell (one or more of these combined)
** To the left
** To the right
** At the bottom
** At the top
** Centered horizontally
** Centered vertically
* Align text within a cell
** Left
** Right
** Centered
** (To be done later) Justify
* Format different parts of the text in different fonts

Experimental

I just recently put these abilities in, so they may not fully work or even work properly.
* Create a border around the cell
* Word wrap text around the cell

Installation

To install this, just place the Cell.php file in Zend/Pdf/, then in your php file, just add:

include_once('Zend/Pdf/Cell.php');

Examples

The following is an example of how to use the Zend Pdf Cell:

pages[] =new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
 $font=Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ITALIC);
 $pdf->pages[0]->setFont($font,12);
 //Creates a cell in the specified page
 $cell=new Zend_Pdf_Cell($pdf->pages[0]);

 //adds a cell in the upper left with "Hello World"
 $cell->addText("Hello World");
 $cell->write();

 //creates a cell in the center of the page
 //To do top and right, then you would
 //or together POSITION_RIGHT and
 //POSITION_TOP.
 $cell=new Zend_Pdf_Cell($pdf->pages[0],
                                    Zend_Pdf_Cell::POSITION_CENTER_X |
                                    Zend_Pdf_Cell::POSITION_CENTER_Y);
 //add a 1 pixel border
 $cell->setBorder(1);
 //align to the right
 $cell->addText("The quick brown fox jumped over the lazy dog",
                        Zend_Pdf_Cell::ALIGN_RIGHT);
 $cell->write();
?>

If you have any questions or wish to report problems, please use the comment box below. I would also like to hear if you have successfully implemented this!

Files

Cell.php – Zend Framework 1.0.*
Cell.php – Zend Framework 1.5.*
(Updated 1/12/08)
UTF character patch by Dominik

Development and PHP25 Sep 2007 12:00 pm

**I HAVE UPDATED THIS, PLEASE READ THE UPDATE**
I have recently been working with Zend_Pdf and it appears that their ability to put text on a screen is very lacking compared to FPDF. I have recently been diving into expanding the functionality and have provided the following class: Zend_Pdf_Cell. This will allow a developer to create a “cell” within a page, center the cell and align the text within the cell.

The following is a small sample code of how to use the cell:


	$pdf=new Zend_Pdf();
	$pdf->pages[] =new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);

        $font=Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ITALIC);
        $pdf->pages[0]->setFont($font,12);  

	//create and attach the cell to the first page, and center in the X and Y direction
        $cell=new Zend_Pdf_Cell($pdf->pages[0],Zend_Pdf_Cell::POSITION_CENTER_X | Zend_Pdf_Cell::POSITION_CENTER_Y);
	//align the text in the center
        $cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_CENTER);
        $cell->newLine();
	//change the font
        $cell->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_BOLD),28);
	//align this to the right
        $cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_RIGHT);
        $cell->newLine();
        $cell->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES),10);
	//align this to the left
        $cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_LEFT);
        $cell->newLine();
        $cell->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ITALIC),28);
        $cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_CENTER);
	//finally write the cell to the page.  Text will not show up unless you write to the page.
        $cell->write();
        //create a new cell and center on just the X coordinate
        $cell=new Zend_Pdf_Cell($pdf->pages[0],Zend_Pdf_Cell::POSITION_CENTER_X);
        $font=Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
        $cell->setPosition(Zend_Pdf_Cell::POSITION_TOP);
        $cell->addText("The quick brown fox jumped over the lazy dog.");
        $cell->write();

        $cell=new Zend_Pdf_Cell($pdf->pages[0],Zend_Pdf_Cell::POSITION_RIGHT,$pdf->pages[0]->getWidth());
        $font=Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);
        $cell->setPosition(Zend_Pdf_Cell::POSITION_BOTTOM);
        $cell->addText("The quick brown fox jumped over the lazy dog.",Zend_Pdf_Cell::ALIGN_RIGHT);
        $cell->write();

	$pdf->save($this->uploadDir.'/TEST-CELL2.pdf');

I have attached not only the Cell patch, but also the diff of font resource files separately.

In the origional source for the font’s, we can see that they didn’t use the correct widths:

/* The glyph numbers assigned here are synthetic; they do not match the
*  actual glyph numbers used by the font. This is not a big deal though
*  since this data never makes it to the PDF file. It is only used
* internally for layout calculations.
*/

So when attempting to calculate things like a string width, you would get the wrong numbers. I looked up the numbers on Adobe’s Font and Type Technology Center particularly the Unix fonts. From there I generated a small PHP script that would output the ASCII value and the corresponding glyph width. Using these numbers, I updated the Zend font resource files which gives you the diff below. Because these fonts only contain width values for the ASCII numbers 32 – 251ish, those are all that were updated.

Zend Font Width Patch

Cell Patch

Development and PHP13 Aug 2007 08:04 am

I recently read about how some of the source code behind Facebook was leaked. I read through some of it and as a PHP developer, it surprised me at some of the things that they have done.

Here is a small list of the features of their code:

  1. There is no object oriented code.
  2. There are a _lot_ of includes
  3. They use Smarty (or something built to feel like Smarty)
  4. They don’t use camelCase
  5. Many
    • Many
      • Many
        • Nested if’s
Opinion and PHP10 Aug 2007 03:18 pm

I recently read an article at ONLamp.com about why people write free documentation. As being part of the PHP documentation team, I thought that the article would be an interesting read.

The author goes to show that most people tend to write the documentation because they want a thriving community and personal growth, but they don’t write for thrills or because they enjoy writing. Let me reemphisize that last point: People do not write the documentation because they enjoy writing. I have often been asked why I started to join the PHP team, and I can honestly say that it’s not because I enjoy writing. My father often asks why I contribute to something where I get no monetary reward. It is hard for him to understand how this is like community service, but on a different level. I joined the PHP documentation team to help out and give back to the community, to learn PHP on a more intimate level, and to hopefully be able to study the source and contribute to PHP 6 and beyond.

I enjoy working with the PHP team and have been slacking off in my contributions, but I hope to find some time to help out during the school year.

PHP07 Jun 2007 10:39 pm

I’ve been thinking about attending BARcamp Chicago. It would be about a two hour train ride but I think it would be a fun experience. It takes place near the end of June and they have around 50 or so people signed up.

Anyone else interested?

PHP06 Jun 2007 11:03 pm

Philip Olsen has a short article on PHP [lack of] naming conventions.

Maybe ogo is the solution?

Development and PHP16 May 2007 04:55 pm

So for the two-five people that read my blog know, I have joined the PHP documentation team and I have full CVS karma. My original involvement was looking at bug#41108 in PHP’s documentation. I just closed the bug report on it today as I felt that the solution was correct.

Yay me, first bug report fixed. More to come :)

PHP03 May 2007 01:30 pm

So after getting my CVS account, I have posted my first patch to the documentation! You should be able to browse to the PHP posix documentation and view many of the new functions I have documented!

Next Page »