Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

FPDF PDF Generator Support

Avatar of petewinter
petewinterFlag for United Kingdom of Great Britain and Northern Ireland asked on
PHPAdobe Acrobat
5 Comments1 Solution1016 ViewsLast Modified:
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                                                 
//============================================================+
ASKER CERTIFIED SOLUTION
Avatar of chwong67
chwong67Flag of Malaysia image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers