I would like to convert information from a MySQL dbase table to PDF, but only certain columns within the table. I've heard about many PDF class programs, but the people who've recommended them can't seem assist me with using the program.

I may be a novice with PHP, but would appreciate someone helping me with a program they may be familiar with by giving me some step-by-step assistance.

I appreciate your time!

Thank you.

Recommended Answers

All 3 Replies

Member Avatar for diafol

The step-by-step should be covered in the program's documentation - if it's any good. FPDF is ok.

Are you familiar with FPDF? I'm currently receiving this error: Catchable fatal error: Object of class DOMPDF could not be converted to string in ... line 163.

Here's my code:

<?php require_once('..'); ?>
<?php require_once('../dompdf/dompdf_config.inc.php');

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_user, $user);
$query_getMem = "SELECT members_info.fname, members_info.lname, members_info.address, members_info.apt_suite, members_info.city, members_info.`state`, members_info.zip, members_info.phone, members_info.email FROM members_info ORDER BY lname";
$getMem = mysql_query($query_getMem, $user) or die(mysql_error());
$row_getMem = mysql_fetch_assoc($getMem);
$totalRows_getMem = mysql_num_rows($getMem);

function urls2linksComplex($text, $schemes = null, $tlds = 'normal'){
  //"urls2links - Complex" function by Martin Pain / m-bread ( [url]http://m-bread.com/resources/php/functions/urls2linksComplex[/url] )
  //This function can be distributed under the Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License
  //( [url]http://creativecommons.org/licenses/by-sa/2.0/uk/[/url] )
  //Please leave these comments intact.
  if($schemes == 'normal'){
    $scheme = '(?:[Hh][Tt]|[Ff])[Tt][Pp][Ss]?';
  }elseif( is_array($schemes) ){
    $scheme = '(?:' . implode('|', $schemes) . ')';
  }elseif( is_string($schemes) ){
    $scheme = $schemes;
  }else{
    $scheme = '[a-zA-Z][a-zA-Z0-9\-+.]*';
  };//EoIF
  if($tlds == 'normal'){
    $tldExclude = array('doc', 'xls', 'txt', 'rtf', 'jpeg', 'jpg', 'gif', 'png', 'exe', 'html', 'htm', 'zip', 'gz', 'scr', 'rar', 'php', 'php3', 'inc', 'ico', 'bmp', 'asp', 'jsp', 'dat', 'lnk', 'cab', 'csv', 'xml', 'xsl', 'xsd', 'svg', 'psp', 'psd', 'pdf', 'bak', 'wav', 'mp3', 'm4v', 'midi', 'wmv', 'wma', 'js', 'css', 'ppt', 'pps', 'mdb');
  }elseif( is_array($tlds) ){
    $tldExclude = $tlds;
  }elseif( is_string($tlds) ){
    $tldExclude = array($tlds);
  }else{
    $tldExclude = array();
  };//EoIF
    $userinfo = '(?:(?:[a-zA-Z0-9\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*@)?';
      $decOctet = '(?:[0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
     $ipv4 = '(?:'.$decOctet.'\.){3}'.$decOctet;
     $regname = '(?:(?:[0-9A-Za-z][0-9A-Za-z\-]*[0-9A-Za-z]|[0-9A-Za-z])\.)+[a-zA-Z]{2,6}';
    $host = '('.$ipv4.'|'.$regname.')';
    $port = '(?::[0-9]*)?';
   $authority = '((?://)?'.$userinfo.$host.$port.')';
   $path = '(?:/(?:[a-zA-Z0-9\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*?)*';
   $query = '(?:\?(?:[a-zA-Z0-9\-._~!$&\'()*+,;=:/?]|%[0-9A-Fa-f]{2})*?)?';
   $fragment = '(?:#(?:[a-zA-Z0-9\-._~!$&\'()*+,;=:/?]|%[0-9A-Fa-f]{2})*?)?';
  $pattern = '\b(('.$scheme.'\:)?'.$authority.$path.$query.$fragment.')($|[^\w/][<\s]|[<\s]|[^\w/]$)';
  $replacement = '( !in_array( substr(\'$4\', strrpos(\'$4\', \'.\')+1), $tldExclude) )?\'<a href="\'.((\'$2\' == \'\')?((strpos(\'$3\', \'@\'))?\'mailto:$1\':\'http://$1\'):\'$1\').\'">$1</a>$5\':\'$0\'';
 return preg_replace('/'.str_replace('/', '\x2F', $pattern).'/e', $replacement, $text);
};//EoFn urls2links

'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.
$html =
'<html xmlns="http://www.w3.org/1999/xhtml">'.
'<head>'.
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.
'<style type="text/css">
<!--
body {
margin: 1em;
}

.tablecen {
margin: 0px auto;
}

td {
text-align: left;
}

.caps {
    text-transform: capitalize;
    }
h1 {
    font: bold 18px Georgia, "Times New Roman", Times, serif;
    text-align: center;
}
-->
</style>'.
'</head>'.

'<body>'.
'<?php '.
'// define the starting year'.
'$startYear = 2009;'.
'// calculate the current year'.
'$thisYear = date("Y");'.

'if ($startYear==$thisYear) {'.
'// if both are the same, just show the starting year'.
'echo $startYear;'.
'}'.
'else {'.
'// if they"re different, show both'.
'echo "$startYear-$thisYear";'.
'}'.
'?></h1>'.
'<table class="tablecen" width="800" >'.
'<tr>'.
'<?php'.
'$getMem_endRow = 0;'.
'$getMem_columns = 3; // number of columns'.
'$getMem_hloopRow1 = 0; // first row flag'.
'do {'.
    'if($getMem_endRow == 0  && $getMem_hloopRow1++ != 0) echo "<tr>";'.
'?>'.
'<td><p><span class="caps"><?php echo $row_getMem["fname"]; ?> <?php echo $row_getMem["lname"]; ?>'.
'<br />'.
'<?php echo $row_getMem["address"]; ?> <?php echo $row_getMem["apt_suite"]; ?><br />'.
'<?php echo $row_getMem["city"]; ?>,</span> <span style="text-transform: uppercase;"><?php echo $row_getMem["state"]; ?></span> <?php echo $row_getMem["zip"]; ?><br />'.
'<?php if (!empty($row_getMem["phone"])) { echo $row_getMem["phone"] . "<br />"; }?>'.
'<?php echo urls2linksComplex($row_getMem["email"]); ?></p></td>'.
'<?php  $getMem_endRow++;'.
'if($getMem_endRow >= $getMem_columns) {'.
'?>'.
'</tr>'.
'<?php'.
'$getMem_endRow = 0;'.
'}'.
'} while ($row_getMem = mysql_fetch_assoc($getMem));'.
'if($getMem_endRow != 0) {'.
'while ($getMem_endRow < $getMem_columns) {'.
'    echo("<td>&nbsp;</td>");'.
'   $getMem_endRow++;'.
'}'.
'echo("</tr>");'.
'}?>'.
'</table>'.
'</body>'.
'</html>';
'<?php'.
'mysql_free_result($getMem);'.
'?>'.

$dompdf = new DOMPDF(); ***THIS IS LINE 163***
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("roster.pdf");
?>
Member Avatar for diafol

Sorry that's way too much code for me to wade through, especially as it's not set within code tags. FPDF - an example:

define('FPDF_FONTPATH',$_SERVER['DOCUMENT_ROOT'] . '/admin/scripts/font/');
require($_SERVER['DOCUMENT_ROOT'] . '/admin/scripts/fpdf.php');

class PDF extends FPDF
{
//Page header
function Header()
{
    //Logo
    $this->Image($_SERVER['DOCUMENT_ROOT'] . '/css/images/logo.gif',10,8,16);
    //Arial bold 15
    $this->SetFont('Arial','BU',25);
    //Move to the right
    $this->Cell(80);
    //Title
    $this->Cell(30,10,'CYHOEDDIADAU BOREOL YR YSGOL',0,0,'C');
    //Line break
    $this->Ln(10);
}

//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','I',8);
    //Page number with 'Tudalen' prefix
    $this->Cell(0,10,'Tudalen '.$this->PageNo().'/{nb}',0,0,'C');
}
}


//Instanciation of inherited class
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();

$ds = get_date('cy', $date); //custom date getter

$pdf->SetFont('Arial','I',15);
$pdf->Cell(190,10,$ds,0,1,'C'); //write date centred and italic
//Line break
$pdf->Ln(5);

$pdf->SetFont('Arial','B',14);
$pdf->Cell(0,10,'DISGYBLION',0,1,'C'); //write title, centred and big bold
$i = 1;
$q = "SELECT *, DATE_FORMAT(cyhoeddiadau.posted,'%d/%m/%Y') AS pst FROM cyhoeddiadau INNER JOIN user ON cyhoeddiadau.user_id = user.id WHERE teip = 'Disgyblion' AND commence <= now() AND finish >= now()";
$r = mysql_query($q);
while($d = mysql_fetch_array($r)){
        $pdf->SetFont('Arial','BI',14);
	$pdf->Write(5,stripslashes($d['title'])); //write title in bold italic
	$pdf->SetFont('Arial','',11);
	$pdf->Ln();
        $pdf->Write(5,stripslashes($d['body'])); //announcement body normal
	$pdf->SetFont('Arial','I',9);
	$pdf->Ln();
	$pdf->Cell(0,10,'Postiwyd ar: ' . stripslashes($d['pst']) . ' gan ' . strtoupper($d['initials']) ,0,1) ; //write posted by... in italic, small
$i = $i + 1;
}
$pdf->SetFont('Arial','B',14);
$pdf->Cell(0,10,'ATHRAWON',0,1,'C');

$q = "SELECT *, DATE_FORMAT(cyhoeddiadau.posted,'%d/%m/%Y') AS pst FROM cyhoeddiadau INNER JOIN user ON cyhoeddiadau.user_id = user.id WHERE teip = 'Athro' AND commence <= now() AND finish >= now()";
$r = mysql_query($q);
while($d = mysql_fetch_array($r)){
    $pdf->SetFont('Arial','BI',14);
    $pdf->Write(5,stripslashes($d['title']));
	$pdf->SetFont('Arial','',11);
	$pdf->Ln();

	$pdf->Write(5,stripslashes($d['body']));
	$pdf->SetFont('Arial','I',9);
	$pdf->Ln();
	$pdf->Cell(0,10,'Postiwyd ar: ' . stripslashes($d['pst']) . ' gan ' . strtoupper($d['initials']) ,0,1) ;
$i = $i + 1;
}

$pdf->Output($_SERVER['DOCUMENT_ROOT'] . '/admin/hardcopy/announcements_' . $ds . '.pdf' ,'F');

The above creates a document with a main title/logo and two columns of announcement info - one col for pupils [disgyblion] and one for teachers [athrawon]. This is saved to the admin/hardcopy directory with the prefix 'announcements_' followed by the date and '.pdf'. The above is just a quick example of something I threw together - it's not the prettiest nor the most optimized, but I hope it shows the ease of use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.