FPDF PDF Generator Support

petewinter
petewinter used Ask the Experts™
on
I needs help with coding FPDF PDF Generator. Not sure if I'm asking the question in the correct zone as I could find a dedicated zone.

http://www.fpdf.org/

I need to do the following

- check where you are -> GetY() and see how much space is available
- calculate the height of next table - that´s up to you
- check if table fits into free space
- add a new page if table does not fit.

See my code attached. I will explain further in my next post.
//============================================================+
// File name   : example_011.php
// Begin       : 2008-03-04
// Last Update : 2009-09-30
// 
// Description : Example 011 for TCPDF class
//               Colored Table
// 
// Author: Nicola Asuni
// 
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com s.r.l.
//               Via Della Pace, 11
//               09044 Quartucciu (CA)
//               ITALY
//               www.tecnick.com
//               info@tecnick.com
//============================================================+

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: Colored Table
 * @author Nicola Asuni
 * @copyright 2004-2009 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
 * @link http://tcpdf.org
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
 * @since 2008-03-04
 */

require_once('../../tcpdf/config/lang/eng.php');
require_once('../../tcpdf/tcpdf.php');

// extend TCPF with custom functions

class MYPDF extends TCPDF {
	
	// Load table data from file
	public function LoadData($file) {
		// Read file lines
		$lines = file($file);
		$data = array();
		foreach($lines as $line) {
			$data[] = explode(';', chop($line));
		}
		return $data;
	}
		
	// Colored table
	public function ColoredTable($header,$data) {
		// Colors, line width and bold font
		$this->SetFillColor(81, 98, 126);
		$this->SetTextColor(255);
		$this->SetDrawColor(153, 153, 153);
		$this->SetLineWidth(0.25);
		$this->SetFont('helvetica', 'B', 9);
		/** Header
		$w = array(80, 40, 30, 30);
		for($i = 0; $i < count($header); $i++)
		$this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
		$this->Ln();
		*/
		// Header
		$w = array(41, 33, 30, 43, 35);
		for($i = 0; $i < count($header); $i++)
		
		foreach($header as $header_row) {
			$this->Cell($w[0], 7, $header_row[0], 1, 0, 'L', 1);
			$this->Cell($w[1], 7, $header_row[1], 1, 0, 'L', 1);
			$this->Cell($w[2], 7, $header_row[2], 1, 0, 'L', 1);
			$this->Cell($w[3], 7, $header_row[3], 1, 0, 'L', 1);
			$this->Cell($w[4], 7, $header_row[4], 1, 0, 'R', 1);
			$this->Ln();
		}
		// Color and font restoration
		$this->SetFillColor(224, 235, 255);
		$this->SetTextColor(0);
		$this->SetFont('');
		// Data
		$fill = 0;
		foreach($data as $row) {
			$this->Cell($w[0], 6, $row[0], 'LR', 0, 'L', $fill);
			$this->Cell($w[1], 6, $row[1].'mm', 'LR', 0, 'L', $fill);
			$this->Cell($w[2], 6, $row[2].'m', 'LR', 0, 'L', $fill);
			$this->Cell($w[3], 6, $row[3], 'LR', 0, 'L', $fill);
			$this->Cell($w[4], 6, $row[4], 'LR', 0, 'R', $fill);
			$this->Ln();
			$fill=!$fill;
		}
		$this->Cell(array_sum($w), 0, '', 'T');
	}
}

	/** header title */
	define ('PDF_HEADER_TITLE2', 'Media Price List');

	/** header description string */
	define ('PDF_HEADER_STRING2', "Call 01628 560892\nwww.selectechdirect.co.uk");

	/** image logo 2 */
	define ('PDF_HEADER_LOGO2', 'Selectech_Logo.jpg');

	/** header logo image width [mm] 2 */
	define ('PDF_HEADER_LOGO_WIDTH2', 65);

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Selectech');
$pdf->SetTitle('Selectech Price List');
$pdf->SetSubject('Media Price List');
$pdf->SetKeywords('Selectech, PDF, Media, Price List');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO2, PDF_HEADER_LOGO_WIDTH2, PDF_HEADER_TITLE2, PDF_HEADER_STRING2);


// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

//set some language-dependent strings
$pdf->setLanguageArray($l); 

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', '', 10);

// add a page
$pdf->AddPage();

// create some HTML content
$pdf->writeHTML('<h1>' .$row_rs_customer_selected['customer_name']. ' - Media Price List<br/></h1>', true, 0, true, 0);

$Num2 = 1;

mysql_select_db($database_conn_selectech, $conn_selectech);
$query_rs_media_category_list = "SELECT * FROM media_category";
$rs_media_category_list = mysql_query($query_rs_media_category_list, $conn_selectech) or die(mysql_error());
$row_rs_media_category_list = mysql_fetch_assoc($rs_media_category_list);
$totalRows_rs_media_category_list = mysql_num_rows($rs_media_category_list);

do {

mysql_select_db($database_conn_selectech, $conn_selectech);
$query_rs_media_group = "SELECT * FROM media_group WHERE media_category_id = " . $row_rs_media_category_list['id'];
$rs_media_group = mysql_query($query_rs_media_group, $conn_selectech) or die(mysql_error());
$row_rs_media_group = mysql_fetch_assoc($rs_media_group);
$totalRows_rs_media_group = mysql_num_rows($rs_media_group);


$pdf->writeHTML('<div style="color:#424856; font-size: 14pt; font-weight: bold;">' .$row_rs_media_category_list['category_name']. '</div>', true, 0, true, 0);


do {
	
mysql_select_db($database_conn_selectech, $conn_selectech);
$query_rs_media = "SELECT me.*, mg.selectech_group_code, mpo.*
				   FROM media me
				   JOIN media_group mg
				   ON me.media_group_id = mg.id
				   LEFT JOIN media_price_override mpo
				   ON me.id = mpo.media_roll_id AND mpo.customer_id = ". $CustomerCookieValue. "
				   WHERE me.media_group_id = " . $row_rs_media_group['id'] . "
				   ORDER BY me.width ASC";
$rs_media = mysql_query($query_rs_media, $conn_selectech) or die(mysql_error());
$row_rs_media = mysql_fetch_assoc($rs_media);
$totalRows_rs_media = mysql_num_rows($rs_media);

mysql_select_db($database_conn_selectech, $conn_selectech);
$query_rs_media_group_override = "SELECT * FROM media_group_override WHERE customer_id = ".$CustomerCookieValue." AND media_group_id = ".$row_rs_media_group['id'];
$rs_media_group_override = mysql_query($query_rs_media_group_override, $conn_selectech) or die(mysql_error());
$row_rs_media_group_override = mysql_fetch_assoc($rs_media_group_override);
$totalRows_rs_media_group_override = mysql_num_rows($rs_media_group_override);

mysql_select_db($database_conn_selectech, $conn_selectech);
$query_rs_media_brand = "SELECT * FROM media_brand WHERE id = " . $row_rs_media_group['brand_id'];
$rs_media_brand = mysql_query($query_rs_media_brand, $conn_selectech) or die(mysql_error());
$row_rs_media_brand = mysql_fetch_assoc($rs_media_brand);
$totalRows_rs_media_brand = mysql_num_rows($rs_media_brand);


$pdf->writeHTML('<div style="color:#000000; font-size: 10pt; font-weight: bold;"><br/>' .$row_rs_media_brand['brand'] . ' ' . $row_rs_media_group['name']. '<br/></div>', true, 0, true, 0);

//Column titles
$header=array();
$header[] = array('Code', 'Width', 'Length', 'Weight / Thickness', 'Price');

//Data loading
$data=array();
do  {
	
	  $mark_up_adjusted = $row_rs_mark_up_selected['mark_up_rate'] - 100;
	  
	  if ($mark_up_adjusted < 0) { $mark_up_adjusted_revised = -$mark_up_adjusted; } else  {  $mark_up_adjusted_revised = $mark_up_adjusted; }
	  
	  if (is_null($row_rs_media['single_price_override'])) {  
	  $single_price_adjusted = $row_rs_media['single_price'] / ($mark_up_adjusted_revised / 100);
	  } else {
	  $single_price_adjusted = $row_rs_media['single_price_override']  ;
	  }
	  
	  if ($row_rs_customer_selected['id'] == 1) {
	  
	  $single_price_converted = $row_rs_media['single_price'] * $row_rs_exchange_rate_selected['rate'];
	  } else { 
	  
	  $single_price_converted = $single_price_adjusted * $row_rs_exchange_rate_selected['rate'];
	  }
	  
	  if (is_null($row_rs_media['ten_price_override'])) {  
	  $ten_price_adjusted = $row_rs_media['10_price'] / ($mark_up_adjusted_revised / 100);
	  } else {
	  $ten_price_adjusted = $row_rs_media['ten_price_override'];
	  }
	  
	  if ($row_rs_customer_selected['id'] == 1) {
	  $ten_price_converted = $row_rs_media['10_price'] * $row_rs_exchange_rate_selected['rate'];
	  } else { 
	  $ten_price_converted = $ten_price_adjusted * $row_rs_exchange_rate_selected['rate'];
	  }
	  
	  if (is_null($row_rs_media['pallet_price_override'])) {  
	  $pallet_price_adjusted = $row_rs_media['pallet_price'] / ($mark_up_adjusted_revised / 100);
	  } else {
	  $pallet_price_adjusted = $row_rs_media['pallet_price_override'];
	  }
	  
	  if ($row_rs_customer_selected['id'] == 1) {
	  $pallet_price_converted = $row_rs_media['pallet_price'] * $row_rs_exchange_rate_selected['rate'];
	  } else { 
	  $pallet_price_converted = $pallet_price_adjusted * $row_rs_exchange_rate_selected['rate'];
	  }

if(empty($row_rs_media['weight'])) { 
	$weight = ""; 
	} else { 
	$weight = $row_rs_media['weight']. "gsm";
	}
if(empty($row_rs_media['thickness'])) { 
	$thickness = "";
	} else { 
	$thickness = $row_rs_media['thickness']. "mic";
	}
	

$data[] = array(
	$row_rs_media['selectech_group_code'] . "-" . $row_rs_media['width'] . "-" . $row_rs_media['length'],
	$row_rs_media['width'],
	$row_rs_media['length'],
	$weight. $thickness,
	utf8_encode($row_rs_exchange_rate_selected['symbol']). number_format(round($single_price_converted, 2), 2)
   );
 
}  while ($row_rs_media  = mysql_fetch_assoc($rs_media));

// print colored table

$pdf->ColoredTable($header, $data);

// ---------------------------------------------------------

if (is_null($row_rs_media_group_override['notes_override'])) {
	  $notes = $row_rs_media_group['notes'];
	  } else {
	  $notes = $row_rs_media_group_override['notes_override'];
	  }

if (!is_null($notes)) {
	
	$pdf->writeHTML('<div style="color:#F00; font-size: 9pt; font-weight: bold;"><br/>' .$notes. '<br/></div>', true, 0, true, 0);
 }

} while ($row_rs_media_group = mysql_fetch_assoc($rs_media_group));

if ($Num2++ != $totalRows_rs_media_category_list) {

$pdf->AddPage();

}


} while ($row_rs_media_category_list = mysql_fetch_assoc($rs_media_category_list));

//Close and output PDF document
$pdf->Output('Selectech_Price_List.pdf', 'I');

//============================================================+
// END OF FILE                                                 
//============================================================+

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
The -> GetY() should be positioned on line 295

then

I need to calculate the height of the next repeat area / table which will be lines 163 to 295

then

check if table fits into remaining free space of the page

then

add a new page if table does not fit on this page. $pdf->AddPage();

Can anyone help me code this? Thanks
leakim971Multitechnician
Top Expert 2014
Commented:
See the function CheckPageBreak($h) here : http://www.fpdf.de/downloads/addons/3/
Commented:
One suggestion:
1. Check each row space required; for example: $totalRows_rs_media_category_list = 10;
1 row required 12 pt, thus you need $max_line_pt 120 pt space.
2. Look for  $end_of_page_pt, for example 210 pt,
3. if ($pdf->gety() + $max_line_pt  > end_of_page_pt) $pdf->addpage();

Author

Commented:
Thanks for your help. I will come back and look into your suggestion further asap,

Author

Commented:
Thanks for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial