Jump to content

PHP - PDF HELP


closerwalk

Recommended Posts

Here it goes... I have built this cool report creator that pulls information from our project database and outputs various information to create reports and even allows your to save queries.  Anyway moving on .   The next portion handles the query and outputs the report.  What I want to do because the reports could be quite wide and web browsers don't afford for size-to-fit and printing 50 column reports would be difficult.  I would like to output the report in pdf I know that it has the ability to print-to-fit or fit-to-print.  So how do I accomplish this with the current report script?

 

 

 

<html>
<head>

<title>Report Manager</title>
Created By K.Goddard
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php
///pm.php

/// Make the connection then chech for the state of the connection if the state 
///  of the connection is dead it will report the error.
$link = @mysql_connect('***', '***', '***');
if (!$link) {
    die('Could not connect to MySQL server: ' . mysql_error());
}
$dbname = 'project';
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
    die("Could not set $dbname: " . mysql_error());
}
//Connection String Above This Line.........................................................................
//Saved Query Section


echo '<form action="report.php" method="post" name="report" id="report">'; 

//Select Distinct Records Project Managers
$res = mysql_query('select DISTINCT Issued_to from project ', $link);
print '<table width="75%" border="2"><th ALIGN="left">Project Manager</th>';
echo '<th ALIGN="left">Clients</th>';
echo '<th ALIGN="left">Report Columns</th>';
echo '<tr>';

echo '<td>';

//Loop through the datebase and get the project managers and place them in a multi-select box which is passed into an array called projectmanager[]
print '<select multiple name ="PROJECTMANAGER[]">';


while ($row = mysql_fetch_array($res)) {

for($i=0;$i<mysql_num_fields($res);$i++) {

	}    

print  '<OPTION VALUE='.$row[mysql_field_name($res, $i)].'>'.$row[mysql_field_name($res, $i)].''; 

}	
echo '</select>';
echo '</td>';
$res = mysql_query('select DISTINCT CUSTOMER_NAME from project ', $link);
echo '<td>';

//Loop through the datebase and get the clients and place them in a multi-select box which is passed into an array called clients[]
print '<select multiple  name="CLIENTS[]">';
while ($row = mysql_fetch_array($res)) {
  

for($i=0;$i<mysql_num_fields($res);$i++) {

 }    

print  '<OPTION VALUE='.$row[mysql_field_name($res, $i)].'>'.$row[mysql_field_name($res, $i)].''; 

}	
echo '</select>';

echo '</td>';
echo '<td>';

$result = mysql_query('select * from project ', $link);

//Loop through the datebase and get the report columns and place them in a multi-select box which is passed into an array called REPORT_COLUMNS[]
print '<select multiple name ="REPORT_COLUMNS[]" >';

for ($i = 0; $i < mysql_num_fields($result); $i++) { 
    

print  '<OPTION VALUE='.mysql_field_name($result, $i).'>'.mysql_field_name($result, $i).''; 

} 
echo '</select>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th align=left>Date Range</th>';
echo '<td>';
echo 'From: <input name="from" type="text" size="25"> </td><td>To:  <input name="to" type="text" size="25">';
echo '</td>';
echo '<tr>';
echo '<th align=left>Project Number:</th><td><input name="projectno" type="text" size="25">';
echo '</td>';
echo '<td> </td>';
echo '</tr>';
echo '<th align=left>Saved Reports</th>';
echo '<th align=left>Save Your Report</th>';
echo '<th align=left>Current Project</th>';
//echo '<th> </th>';
echo '<tr>';
echo '<td>';

$queries = mysql_query('select query_name from saved_queries ', $link);
print '<select multiple name ="savedquery" >';

	while ($row = mysql_fetch_array($queries)) {

	for($i=0;$i<mysql_num_fields($queries);$i++) {

		}    

print  '<OPTION VALUE='.$row[mysql_field_name($queries, $i)].'>'.$row[mysql_field_name($queries, $i)].''; 

	}	
echo '</select>';
echo '<input name="delete" type="submit" value="Delete" />';
echo '<td>';
echo 'Report Name: <input name="queryname" type="text" size="25"> ';
echo '</td>';
//echo '<td> </td>';
echo '<td>Yes:<input name="current" type="checkbox" value="Y" checked>';
echo 'No: <input name="current" type="checkbox" value="N" ></td>';
echo '</tr>';

echo '</table>';
echo '<input name="submit" type="submit" value="Run Report" />';
echo '</form>';



?>
</body>
</html>

 

 

Report.php

 

<html>


<script src="./sorttable.js"></script>
<head>
<title>Report</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<?php




/* THIS LINE STARTS THE MEAT OF THE REPORT BUILDER */
// Make the connection then chech for the state of the connection if the state 
///  of the connection is dead it will report the error.
$link = @mysql_connect('****', '****', '****');
if (!$link) {
    die('Could not connect to MySQL server: ' . mysql_error());
}
$dbname = 'project';
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
    die("Could not set $dbname: " . mysql_error());
}
//Connection String Above This Line.........................................................................
//Delete Saved Reports Here 
if($_POST['delete']) {
$delete = "'". $_POST['savedquery'] ."'";

$remove = 'DELETE from saved_queries WHERE query_name = '.$delete.'';
  mysql_query($remove);
}
else {

if($_POST["current"]) {
$current = $_POST["current"];
$current =    ' AND ' . "`CURRENT_PROJECT` =  ".'"'.$current.'"';

}
//Handle Project Manager Queries
foreach ($_POST["PROJECTMANAGER"] as $v) {
$projectmanager =   $projectmanager. ' OR ' . "`Issued_to` LIKE	".'"'.$v.'%'.'"';
}

if($_POST["PROJECTMANAGER"]) {
$projectmanager = substr($projectmanager,3	); 
$projectmanager = "(".$projectmanager.")";
}
else {
$projectmanager = "("."`Issued_to` LIKE ".'"'."%".'"'.")";
			 }

//CLIENT SECTION IF NONE ARE SELECTED DEFAULT IS ALL

foreach ($_POST["CLIENTS"] as $v) {
$clients =   $clients. ' OR ' . "`CUSTOMER_NAME`"."LIKE ".'"'.$v.'%'.'"';

}
if($clients){
$clients = substr($clients,3	); 
$clients =  "("."".$clients."".")";
		} 
		else {
$clients = "("."`CUSTOMER_NAME`"." LIKE ".'"'."%".'"'.")";
			 }


//REPORT COLUMNS SECTION 
foreach ($_POST["REPORT_COLUMNS"] as $v) {
$columns=  $columns. ' , ' ."`".$v."`";
}
if($columns){
$columns = substr($columns,2	); 

		} 

		else {
			 $columns = "*";
		 	  }

if($_POST["from"]) {
$daterange = "AND ". "`DATE_RECEIVED`". " BETWEEN "."'" .date("Y/d/m",strtotime($_POST["from"])) ."'" ." AND " . "'" . date("Y/d/m",strtotime($_POST["to"])) ."'" ;
}
	else {
$daterange = "";			
	 }


//Grab the Project Number and Query it

if($_POST['projectno']) {
$projectno = $_POST['projectno'];
$projectno = "OR"."("."'"."`PROJECT_NO` ". "'" ."LIKE "." ".'"' .$projectno.'"'.")";
}
else {
$projectno = "";
}
//Building the sql statement from saved queries.
if($_POST['savedquery']) {
$savedfetch = "'".$_POST['savedquery']."'";
$savedquery = 'select query from saved_queries WHERE query_name = '.$savedfetch.'';
$query = mysql_query($savedquery, $link);

}
	while ($row = mysql_fetch_array($query)) {

	for($i=0;$i<mysql_num_fields($query);$i++) {

	}    
$sql = $row[mysql_field_name($query, $i)];
}

$sql = "SELECT $columns FROM project WHERE $clients AND $projectmanager $daterange $projectno $current";

if($_POST['queryname']) {
	$sql1 = '"'.$sql. '"'; 	 		
$queryname = $_POST['queryname'];
$save = "INSERT INTO `project`.`saved_queries` (`Query_id`, `Query_Name`, `Query`) VALUES  (NULL,'$queryname', $sql1)";
mysql_query($save);

					}
else {


}
//echo $sql;

/* THE OUTPUT OF THE REPORT BUILDER HAPPENS HERE */

$res = mysql_query($sql);
echo '<table class="sortable" width="75%" border="1" bordercolor="blue" ><tr>';
for($i=0;$i<mysql_num_fields($res);$i++) {
$order = mysql_field_name($res, $i);

$tableheadings = '<th nowrap>'. mysql_field_name($res, $i) .'</th>';

echo $tableheadings;

}
echo '</tr>';


while ($row = mysql_fetch_array($res)) {
   
echo '<tr nowrap>';
for($i=0;$i<mysql_num_fields($res);$i++) {
echo '<td nowrap>';
if($row[mysql_field_name($res, $i)]==""){
echo " ";
}
if(mysql_field_name($res, $i)=='DATE_RECEIVED') {

echo date("m-d-Y",strtotime($row[mysql_field_name($res, $i)]));
} 

elseif(mysql_field_name($res, $i)=='FORECAST_END_DATE') {
if(!$row[mysql_field_name($res, $i)]==""){
	echo date("m-d-Y",strtotime($row[mysql_field_name($res, $i)]));
  }
} 

else {
$tabledata= $row[mysql_field_name($res, $i)];

echo $tabledata;
}
echo '</td>';
}

}
}

echo '</tr>';
echo '</table>';

?>
<body>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/51001-php-pdf-help/
Share on other sites

This is not a simple task:

 

You will need to read up on the PDF functions in PHP: http://us2.php.net/manual/en/ref.pdf.php

 

Then you will need to go back and rewrite all of your functionality for displaying reports. With PDFs you cannot simply build a table like you do with HTML. For each piece of text you "print" to a PDF you need to specify the X & Y coordinates. However, I am sure there are some useful classes available to help you out or you can build functions to ease the process. But, it is not a matter of just saying createPDF()!

 

Edit, I saw MadTechie posted before I did, but decided to post anyway. Funny that our Step 1 was exactly the same!

Link to comment
https://forums.phpfreaks.com/topic/51001-php-pdf-help/#findComment-250956
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.