PDA

View Full Version : pdf file..anybody can help me?



syazyan
09-20-2006, 06:53 AM
i have to print out a pdf report. the information in the pdf file coming from the database..how could i do it using php n mysql? i don't know...

is there anyone who can help me??
:( :confused:

codeexploiter
09-20-2006, 11:04 AM
posted by syazyan
i have to print out a pdf report


Hope you are talking about displaying a PDF report using PHP/MySQL combination.

You can find my code below for that purpose



<?php
//PDF file creation
$p = PDF_new();

/* open new PDF file; insert a file name to create the PDF on disk */
if (PDF_begin_document($p, "", "") == 0) {
die("Error: " . PDF_get_errmsg($p));
}

PDF_set_info($p, "Creator", "pdf.php");
PDF_set_info($p, "Author", "codeexploiter");
PDF_set_info($p, "Title", "Report");

PDF_begin_page_ext($p, 595, 842, "");

$font = PDF_load_font($p, "Helvetica-Bold", "winansi", "");

PDF_setfont($p, $font, 10.0);

PDF_set_text_pos($p, 1, 810);
PDF_show($p, "Company Report");
PDF_continue_text($p,"");




//Retrieving the information from the database
$user = "root";
$password = "";
$dbhost = "localhost";
$dbname = "PDF";

$link = mysql_connect($dbhost, $user, $passwd);

if(!link)
{
error_log(mysql_error(), "tmp/phplog.err");
}
else
{
$db = mysql_select_db($dbname, $link);

if(!db)
{
error_log(mysql_error(), "tmp/phplog.err");
}
else
{
$query = "SELECT matter from PDF";
$result = mysql_query($query,$link);

while($record = mysql_fetch_row($result))
{

PDF_continue_text($p, $record[0]);

}
PDF_end_page_ext($p, "");

PDF_end_document($p, "");

$buf = PDF_get_buffer($p);
$len = strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $buf;

PDF_delete($p);

}

}

?>


The above code will retrieve some information, the info you want to furnish in the PDF file, from the database and put into the pdf and you can view the pdf file.

Hope this will help you

syazyan
09-21-2006, 02:03 AM
Hope you are talking about displaying a PDF report using PHP/MySQL combination.

You can find my code below for that purpose



<?php
//PDF file creation
$p = PDF_new();

/* open new PDF file; insert a file name to create the PDF on disk */
if (PDF_begin_document($p, "", "") == 0) {
die("Error: " . PDF_get_errmsg($p));
}

PDF_set_info($p, "Creator", "pdf.php");
PDF_set_info($p, "Author", "codeexploiter");
PDF_set_info($p, "Title", "Report");

PDF_begin_page_ext($p, 595, 842, "");

$font = PDF_load_font($p, "Helvetica-Bold", "winansi", "");

PDF_setfont($p, $font, 10.0);

PDF_set_text_pos($p, 1, 810);
PDF_show($p, "Company Report");
PDF_continue_text($p,"");




//Retrieving the information from the database
$user = "root";
$password = "";
$dbhost = "localhost";
$dbname = "PDF";

$link = mysql_connect($dbhost, $user, $passwd);

if(!link)
{
error_log(mysql_error(), "tmp/phplog.err");
}
else
{
$db = mysql_select_db($dbname, $link);

if(!db)
{
error_log(mysql_error(), "tmp/phplog.err");
}
else
{
$query = "SELECT matter from PDF";
$result = mysql_query($query,$link);

while($record = mysql_fetch_row($result))
{

PDF_continue_text($p, $record[0]);

}
PDF_end_page_ext($p, "");

PDF_end_document($p, "");

$buf = PDF_get_buffer($p);
$len = strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $buf;

PDF_delete($p);

}

}

?>


The above code will retrieve some information, the info you want to furnish in the PDF file, from the database and put into the pdf and you can view the pdf file.

Hope this will help you

i've read the code...but i don't understand where should i put these code..in a new php page?..how could i know whether the file retrieve the data from db or not..besides the select statement??..

can u explain more detail??..i'm very new in creating pdf report using php...

blm126
09-21-2006, 02:07 AM
You seem to be new to PHP in general. I suggest you take the time to get your head around the basics before trying something a little more complex like generating a PDF.If you just want to plow ahead anyway...a couple questions. What does the database look like? what exactly do you expect to happen?

syazyan
09-21-2006, 02:29 AM
You seem to be new to PHP in general. I suggest you take the time to get your head around the basics before trying something a little more complex like generating a PDF.If you just want to plow ahead anyway...a couple questions. What does the database look like? what exactly do you expect to happen?


yes..i'm very new in php...i'm not just trying to generate the pdf..now i'm working and i need to generate the pdf...b4 this i've used the php but...not generating the pdf file...that's y i'm asking a detailed explaination..so i can study about it...:(

i have a table called "comments" ...what i want to do is...when user click button report...a pdf report containing data from table "comments" will be generated and can be viewed by the user...:confused:

codeexploiter
09-22-2006, 04:16 AM
You should have a database in which at least a table with some information on it.



Quoted by: codeexploiter

//Retrieving the information from the database
$user = "root";
$password = "";
$dbhost = "localhost";
$dbname = "PDF";




Quoted by: codeexploiter

$user = "root";
you must specify the database user account through which you want to access the table. In my case i've used account root.



Quoted by: codeexploiter

$password = "";

Here you must specify the password for the user account root



Quoted by: codeexploiter

$dbhost = "localhost";

If you are testing this application in a single machine/your machine you can keep it like that or you can replace the localhost with the machine name on which your web server runs.



Quoted by: codeexploiter

$dbname = "PDF";

This is the database name in which i've my table from which i am gathering the information for generating a PDF report. You can replace the name with your own database name.



Quoted by: codeexploiter

$query = "SELECT matter from PDF";

This is based on my table and the field in it. I have a table named PDF in which i have a field matter which contains the data that I want to put into a PDF file.
You can replace this with your table name and field name.

Once you make the necessary changes in the code I provided in a way that it is compatible with your settings you can put this into a .PHP page and into the document root of your web server or any folder which is accessible from the document root.

The assumption here is that the database resides in the same machine of the web server.



Quoted by: syazyan
i have a table called "comments" ...what i want to do is...when user click button report...a pdf report containing data from table "comments" will be generated and can be viewed by the user...

You can mention the action attribute of a form where you have a button so whenever the user clicks that button it will execute the PHP code that I had provided.

If you know how to invoke a php page using a front-end interface file then this wont' be much difficult for you to implement

djr33
09-22-2006, 05:09 AM
"if(!db)"

Shouldn't that be "if(!$db)"?

codeexploiter
09-22-2006, 05:55 AM
posted by: djr33
"if(!db)"

Shouldn't that be "if(!$db)"?


Yes it should be sorry for the typing mistake. Thanks for pointing it out

syazyan
09-22-2006, 08:02 AM
i've copy the code n change it according to my db and table..but when user click report button..the following error occur...

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE = ''' at line 1"

this is my select statement..

$query = "SELECT * FROM comments WHERE rec_date = '$rec_date'";
$result = mysql_query($query,$link);

what does it means??...the code error at "where" statement?

syazyan
09-26-2006, 06:11 AM
i've done a pdf file...n i've succeed..but the problem is..i just could read n display the first row of data only...how can i view all the row of data based on specific date??

this is sample of my code...


<?php
//include ("../connectdb.pdf");
include ('class.ezpdf.php');

$db = @mysql_connect('localhost','root','') or die(mysql_error());

mysql_select_db('e-account2', $db);
$query2 = "SELECT * FROM comments WHERE rec_date = '$rec_date'";
$result2 = mysql_query($query2,$db)or die(mysql_error());
$query_data2 = mysql_fetch_array($result2);
$no = $query_data2["no"];
$usr_cc = $query_data2["usr_cc"];
$rec_date = $query_data2["rec_date"];
$rec_time = $query_data2["rec_time"];
$call_name = $query_data2["call_name"];
$call_no = $query_data2["call_no"];
$company = $query_data2["company"];
$issue = $query_data2["company"];
$comments = $query_data2["comments"];


$pdf =& new Cezpdf();
$pdf->setEncryption('','rc',array('print'));
$i=$pdf->ezStartPageNumbers(500,720,9,'','',1);

$uppertext1 = "Record based on Date Call Received";
$pdf->selectFont('./fonts/Verdana.afm');
$pdf->setColor(0,0,0);

$pdf->addText(30,800,9,$uppertext1);

$data = array(
array('No'=>$no, 'Agent'=>$usr_cc, 'rec_time'=>$rec_time,'call_name'=>$call_name,'call_no'=>$call_no,
'company'=>$company, 'issue'=>$issue, 'comments'=>$comments));
}

could anybody help me??

codeexploiter
09-26-2006, 06:29 AM
I think you need to extract only those record or records based on a particular date.

It is mainly depends upon the SELECT statement you used and the date data type you have used in your table.

Sometime your database date format is not compatible with the date format used in your table.

suppose if you've got a date from the user from a form for example: 26-Sep-2006

Then using the following code I can convert the above mentioned date to a format that supported by MySQL


date("Y-m-d",strtotime("26-Sep-2006"));

The o/p would be - 2006-09-26 which is the date format of MySQL

syazyan
10-09-2006, 03:47 AM
plz...help me..i've successfully done with the pdf file...but i still got one problem..which is...i cannot break the record into several page...using the code it display all the total record so..the page being truncated...coula anyone help me...

this is my code...for the page...


<?php

error_reporting(E_ALL);

include('class.ezpdf.php');

// $pdf =& new Cezpdf();
$pdf =& new Cezpdf('A4','Landscape');
$pdf->selectFont('./fonts/Helvetica');
$pdf->setEncryption('','rc',array('print'));
$page=$pdf->ezStartPageNumbers(560,85,8,'','',1);
$pdf->addText(60,550,10,'Issues : ');
$pdf->addText(100,550,10,$issue);
//--------------------------------------------------

$host = ' localhost';
$user = 'root';
$password = ' ';
$database = 'e-account2';
$query = "select usr_cc, call_name, rec_time, rec_date, call_no, company, issue, comments from comments WHERE issue='$issue' ORDER BY rec_date";
//--------------------------------------------------

// open the connection to the db server
$link = mysql_connect($host,$user,$password);
// change to the right database
mysql_select_db($database);
// initialize the array
$data = array();
// do the SQL query
$result = mysql_query($query);
$noRows = mysql_num_rows($result);
// step through the result set, populating the array, note that this could also have been written:
// while($data[] = mysql_fetch_assoc($result)) {}

while($data[] = mysql_fetch_array($result, MYSQL_ASSOC)) {}
// make the table
$pdf->ezColumnsStart(array('num'=>2,'gap'=>10));
// $pdf->ezTable($data);
$pdf->ezTable($data,array('usr_cc'=>'Agent','rec_date'=>'Date','rec_time'=>'Time','call_name'=>'Caller Name','call_no'=>'Phone','company'=>'Company','comments'=>'Comments'),'Report based on Issues',
array('xPos'=>430,'xOrientation'=>'center','width'=>750));

// do the output, this standard testing output code, adding ?d=1
// to the url puts the pdf code to the screen in raw form, good for checking
// for parse errors before try to generate the pdf file.
if (isset($d) && $d){
$pdfcode = $pdf->output(1);
$pdfcode = str_replace("\n","\n<br>",htmlspecialchars($pdfcode));
echo '<html><body>';
echo trim($pdfcode);
echo '</body></html>';
} else {
$pdf->stream();
}
?>



:( :confused: :confused: