How to Create Table in Magento – Zend PDF

 Create Table in Magento – Zend PDF:

Create table in zend pdf is little tricky. Not like a html tags. Using PDF, everything (like height, weight,etc) we have fix it.

Take one simple example. We have to create a table and row will be coming from foreach then the Zend pdf code should be as like below

1. First declare Zend PDF and set font.
2. Draw rectangle (This rectangle for table header rows)
3. Draw text (once we declare the rectangle then we have to write the table headers)
4. Draw line (draw line helpful to declare the line between the table header columns)

Ex:
$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN);
$width = $page->getWidth();
$hight = $page->getHeight();
$x = 30;
$pageTopalign = 850; //default PDF page height
$this->y = 850 – 100; //print table row from page top – 100px
//Draw table header row’s
$page->drawRectangle(30, $this->y – 8, $page->getWidth()-30, $this->y + 12, Zend_Pdf_Page::SHAPE_DRAW_STROKE);
//Move text left to right – Header values ($x)
$page->drawText(‘Column1’, $x + 1, $this->y + 50, ‘UTF-8’);
$page->drawText(‘Column2’, $x + 35, $this->y + 50, ‘UTF-8’);
$page->drawText(‘Column3’, $x + 225, $this->y + 50, ‘UTF-8’);
$page->drawText(‘Column4’, $x + 500, $this->y + 50, ‘UTF-8’);
//above row having 4 column’s headers so we have declare 3 line’s between the columns header
$page->drawLine($x + 30, $this->y + 12, $x + 30, $this->y – 8);
$page->drawLine($x + 150, $this->y + 12, $x + 150, $this->y – 8);
$page->drawLine($x + 490, $this->y + 12, $x + 490, $this->y – 8);

5. Now table header is ready to show.
6. From below steps onwards we are going to design the table for showing values in a table. (This will be foreach)
$resultObj = “SOME values”; //$resultObj having all the row values. Consider we are fetching from DB

6.1 Draw rectangle (This rectangle for table values rows)
6.2 Draw text (once we declare the rectangle then we have to write the table values)
6.3 Draw line (draw line helpful to declare the line between the table values columns)

foreach($resultObj as $result){
$page->drawRectangle(30, $this->y – 8, $page->getWidth()-30, $this->y + 12, Zend_Pdf_Page::SHAPE_DRAW_STROKE);
$page->drawLine($x + 30, $this->y + 12, $x + 30, $this->y – 8);
$page->drawLine($x + 150, $this->y + 12, $x + 150, $this->y – 8);
$page->drawLine($x + 490, $this->y + 12, $x + 490, $this->y – 8);

$page->drawText(“value1”, $x + 5, $this->y, ‘UTF-8’);
$page->drawText(“value2”, $x+ 35, $this->y, ‘UTF-8’);
$page->drawText(“value3”, $x + 155, $this->y, ‘UTF-8’);
$page->drawText(“value4”, $x + 510, $this->y, ‘UTF-8’);

}
7. Write you text into PDF.
$pdf->pages[] = $page;
$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); //new page
$content = $pdf->render();
$currentTime = time();
$fileName = “MYFILENAME_$currentTime.pdf”;
$this->_prepareDownloadResponse($fileName, $content);

8. Consider $resultObj having 20 line items (mean 20 rows). Then some time we may get text overlapping. Then we need to calculate the pdf height and our collection count.

9. add below code before you start printing the result. Below code will check before start print the pdf, this code will check
text will fit or not (remainging page space). If not then it will one new page and continue to print.

$resultObj = “MYSQL QUERY”;
$resultCount = $resultObj->count();
if($lineItemTopInc – (20 * $resultCount) < 0 ){
array_push($pdf->pages, $page);
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$lineItemTopInc = 720;
}

10. Yes, done. File will generate while run above script

Final Code:

$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN);
$width = $page->getWidth();
$hight = $page->getHeight();
$x = 30;
$pageTopalign = 850; //default PDF page height
$this->y = 850 – 100; //print table row from page top – 100px
$resultObj = “MYSQL QUERY”;
$resultCount = $resultObj->count();
if($lineItemTopInc – (20 * $resultCount) < 0 ){
array_push($pdf->pages, $page);
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$lineItemTopInc = 720;
}
//Draw table header row’s
$page->drawRectangle(30, $this->y – 8, $page->getWidth()-30, $this->y + 12, Zend_Pdf_Page::SHAPE_DRAW_STROKE);
//Move text left to right – Header values ($x)
$page->drawText(‘Column1’, $x + 1, $this->y + 50, ‘UTF-8’);
$page->drawText(‘Column2’, $x + 35, $this->y + 50, ‘UTF-8’);
$page->drawText(‘Column3’, $x + 225, $this->y + 50, ‘UTF-8’);
$page->drawText(‘Column4’, $x + 500, $this->y + 50, ‘UTF-8’);
//above row having 4 column’s headers so we have declare 3 line’s between the columns header
$page->drawLine($x + 30, $this->y + 12, $x + 30, $this->y – 8);
$page->drawLine($x + 150, $this->y + 12, $x + 150, $this->y – 8);
$page->drawLine($x + 490, $this->y + 12, $x + 490, $this->y – 8);

$resultObj = “MYSQL QUERY”;
foreach($resultObj as $result){
$page->drawRectangle(30, $this->y – 8, $page->getWidth()-30, $this->y + 12, Zend_Pdf_Page::SHAPE_DRAW_STROKE);
$page->drawLine($x + 30, $this->y + 12, $x + 30, $this->y – 8);
$page->drawLine($x + 150, $this->y + 12, $x + 150, $this->y – 8);
$page->drawLine($x + 490, $this->y + 12, $x + 490, $this->y – 8);

$page->drawText(“value1”, $x + 5, $this->y, ‘UTF-8’);
$page->drawText(“value2”, $x+ 35, $this->y, ‘UTF-8’);
$page->drawText(“value3”, $x + 155, $this->y, ‘UTF-8’);
$page->drawText(“value4”, $x + 510, $this->y, ‘UTF-8’);

}

 

————————————————————————————————————————————————————————————-

Corner of Blog:

” Save Rain Water , HARVEST THE RAIN, FEED THE WORLD !!! “

———————————————————————————————————————————————————————————————-