Denisvt
asked on
Creating PDF files from a Mysql BD
Hello,
I have a simple script that lets users manage their address book online. I need to connect to that script an online PDF creation option, so that when they click on a button the Linux server generates a list of their addresses as PDF pages, ready to print on stickers for a postal mailing.
That server is recent and already support on-the-fly PDF creation on another site. (Add-on to OScommerce).
Is there a script I can use, what can I do to add that "export to PDF" feature ?
thank you.
I have a simple script that lets users manage their address book online. I need to connect to that script an online PDF creation option, so that when they click on a button the Linux server generates a list of their addresses as PDF pages, ready to print on stickers for a postal mailing.
That server is recent and already support on-the-fly PDF creation on another site. (Add-on to OScommerce).
Is there a script I can use, what can I do to add that "export to PDF" feature ?
thank you.
ASKER
I have made progress, what I seem to need is actually a hint on how to write the proper PHP lines to fetch the data in my MySQL DB to feed it to the PDF generator.
Thanks for the links but like I said my server was PDF-ready.
Now I have a MySQL DB, structure / a dump can be provided if needed, and found that script :
<?php
define('FPDF_FONTPATH','fo nt/');
require_once('PDF_Label.ph p');
$odbc = odbc_connect ('databasename', 'root', '') or die( "Could Not Connect to ODBC Database!" );
if(!($pdfinfo == "nada")){
$query = odbc_exec($odbc, stripslashes($pdfinfo)) or die (odbc_errormsg());
$pdf2 = new PDF_Label('5160', 'mm', 1, 2);
$pdf2->Open();$pdf2->AddPa ge();
// Print labels
while($Row=odbc_fetch_arra y($query)) {$pdf2->Ad d_PDF_Labe l(sprintf( "%s\n%s\n% s\n%s, %s, %s", "", "$Row[Firstname] $Row[Lastname]", "$Row[Address1] $Row[Address2]", "$Row[City]","$Row[State]" ,"$Row[Zip ]"));}$pdf 2->Output( );
}
?>
However on my server it says
Fatal error: Call to undefined function: odbc_connect() in /home/httpd/vhosts/etc..
Can anybody advise ?
thanks !
Thanks for the links but like I said my server was PDF-ready.
Now I have a MySQL DB, structure / a dump can be provided if needed, and found that script :
<?php
define('FPDF_FONTPATH','fo
require_once('PDF_Label.ph
$odbc = odbc_connect ('databasename', 'root', '') or die( "Could Not Connect to ODBC Database!" );
if(!($pdfinfo == "nada")){
$query = odbc_exec($odbc, stripslashes($pdfinfo)) or die (odbc_errormsg());
$pdf2 = new PDF_Label('5160', 'mm', 1, 2);
$pdf2->Open();$pdf2->AddPa
// Print labels
while($Row=odbc_fetch_arra
}
?>
However on my server it says
Fatal error: Call to undefined function: odbc_connect() in /home/httpd/vhosts/etc..
Can anybody advise ?
thanks !
The odbc function are for connections with odbc-database-drivers. As you want to connect to a Mysql-Server youn need the mysql-functions:
mysql_connect
mysq_query
See http://de2.php.net/manual/en/function.mysql-db-name.php
e.g your code should look like (untested):
<?php
define('FPDF_FONTPATH','fo nt/');
require_once('PDF_Label.ph p');
$odbc = mysql_connect ('root', '') or die( "Could Not Connect to ODBC Database!" );
mysql_select_db('databasen ame');
if(!($pdfinfo == "nada")){
$query = mysql_query(stripslashes($ pdfinfo), $odbc) or die (odbc_errormsg());
$pdf2 = new PDF_Label('5160', 'mm', 1, 2);
$pdf2->Open();$pdf2->AddPa ge();
// Print labels
while($Row=mysql_fetch_arr ay($query) ){$pdf2->A dd_PDF_Lab el(sprintf ("%s\n%s\n %s\n%s, %s, %s", "", "$Row[Firstname] $Row[Lastname]", "$Row[Address1] $Row[Address2]", "$Row[City]","$Row[State]" ,"$Row[Zip ]"));}$pdf 2->Output( );
}
mysql_connect
mysq_query
See http://de2.php.net/manual/en/function.mysql-db-name.php
e.g your code should look like (untested):
<?php
define('FPDF_FONTPATH','fo
require_once('PDF_Label.ph
$odbc = mysql_connect ('root', '') or die( "Could Not Connect to ODBC Database!" );
mysql_select_db('databasen
if(!($pdfinfo == "nada")){
$query = mysql_query(stripslashes($
$pdf2 = new PDF_Label('5160', 'mm', 1, 2);
$pdf2->Open();$pdf2->AddPa
// Print labels
while($Row=mysql_fetch_arr
}
You need to install the php-odbc*.rpm file if you are using redhat or Fedora Core
ASKER
Isn't there a way to simply use MySQL ?
I have almost found my answers based on the FPDF online PDF generator, it already generates the good PDF files with the layout of the labels I need, I seem to only need to find out how to read data from my MySQL data and send it to the pdf creation file.
I currently have, as a sample found in the pdf generator,
for($i=1;$i<=40;$i++)
$pdf->Add_PDF_Label(sprint f("%s\n%s\ n%s\n%s, %s, %s", "Name $i", 'Test', 'av. sample', '90210', 'BH', 'USA'));
whereas I need the real data from my database to come there, by reading it in PHP from MySQL.
Any idea ?
I have almost found my answers based on the FPDF online PDF generator, it already generates the good PDF files with the layout of the labels I need, I seem to only need to find out how to read data from my MySQL data and send it to the pdf creation file.
I currently have, as a sample found in the pdf generator,
for($i=1;$i<=40;$i++)
$pdf->Add_PDF_Label(sprint
whereas I need the real data from my database to come there, by reading it in PHP from MySQL.
Any idea ?
As noted your problem has absolutely nothing to do with generating pdf. What you need to do is pay attention to hernst42. He is absolutely correct.
use the mysql_connect and mysql_query functions instead of using odbc. It's faster too.
use the mysql_connect and mysql_query functions instead of using odbc. It's faster too.
ASKER
Do I understand incorrectly or do you mean the opposite ? You said
"use the mysql_connect and mysql_query functions instead of using odbc", which is precisely what I want as I do not have odbc not can install a new RPM on that Plesk server that hosts other important stuff.
"use the mysql_connect and mysql_query functions instead of using odbc", which is precisely what I want as I do not have odbc not can install a new RPM on that Plesk server that hosts other important stuff.
The code I posted is using mysql (the code you posted was using odbc) so i transformed the odbc_function the the mysql-pendant.
ASKER
Ok I guess I must be close to what I need...
I had a problem with the "or die (odbc_errormsg());" part (gave me "Call to undefined function: odbc_errormsg()") so I replaced with a simple error display.
My code is now
<?php
define('FPDF_FONTPATH','fo nt/');
require_once('PDF_Label.ph p');
$odbc = mysql_connect ("localhost", "dbname", "123") or die( "Could Not Connect to Database!" );
mysql_select_db('dbname');
if(!($pdfinfo == "nada")){
$query = mysql_query(stripslashes($ pdfinfo), $odbc) or die("Something went wrong....");
$pdf2 = new PDF_Label('5160', 'mm', 1, 2);
$pdf2->Open();$pdf2->AddPa ge();
// Print labels
while($Row=mysql_fetch_arr ay($query) ){$pdf2->A dd_PDF_Lab el(sprintf ("%s\n%s\n %s\n%s, %s, %s", "", "$Row[Firstname] $Row[Lastname]", "$Row[Address1] $Row[Address2]", "$Row[City]","$Row[State]" ,"$Row[Zip ]"));}$pdf 2->Output( );
}
?>
but you get it I see "Something went wrong..."
What exactly ? ;-)
I had a problem with the "or die (odbc_errormsg());" part (gave me "Call to undefined function: odbc_errormsg()") so I replaced with a simple error display.
My code is now
<?php
define('FPDF_FONTPATH','fo
require_once('PDF_Label.ph
$odbc = mysql_connect ("localhost", "dbname", "123") or die( "Could Not Connect to Database!" );
mysql_select_db('dbname');
if(!($pdfinfo == "nada")){
$query = mysql_query(stripslashes($
$pdf2 = new PDF_Label('5160', 'mm', 1, 2);
$pdf2->Open();$pdf2->AddPa
// Print labels
while($Row=mysql_fetch_arr
}
?>
but you get it I see "Something went wrong..."
What exactly ? ;-)
The mysql_connect has other parameters than the odbc_connect so use:
$odbc = mysql_connect('host', 'dbuser', 'dbpass') or die( "Could Not Connect to Database!" .mysql_error());
mysql_select_db('dbname');
$odbc = mysql_connect('host', 'dbuser', 'dbpass') or die( "Could Not Connect to Database!" .mysql_error());
mysql_select_db('dbname');
ASKER
I have updated the script,
If I have
$query = mysql_query(stripslashes($ pdfinfo), $odbc) or die("Something went wrong....");
I once again get the message "Something went wrong...",
if I use
$query = mysql_query(stripslashes($ pdfinfo), $odbc) or die("Something went wrong...." .mysql_error());
then it displays a blank screen and does not create the PDF.
What did I miss ?...
Thanks,
If I have
$query = mysql_query(stripslashes($
I once again get the message "Something went wrong...",
if I use
$query = mysql_query(stripslashes($
then it displays a blank screen and does not create the PDF.
What did I miss ?...
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry that was not perfectly clear to me;
I have now tried adding the search statement there and have managed to have my script working perfectly, thanks a lot for that precious help !
I have now tried adding the search statement there and have managed to have my script working perfectly, thanks a lot for that precious help !
http://www.ros.co.nz/pdf/
http://www.rustyparts.com/pdf.php