Link to home
Start Free TrialLog in
Avatar of Denisvt
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.
Avatar of ldbkutty
ldbkutty
Flag of India image

Avatar of Denisvt
Denisvt

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','font/');
require_once('PDF_Label.php');

$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->AddPage();
// Print labels
while($Row=odbc_fetch_array($query)){$pdf2->Add_PDF_Label(sprintf("%s\n%s\n%s\n%s, %s, %s", "", "$Row[Firstname] $Row[Lastname]", "$Row[Address1] $Row[Address2]", "$Row[City]","$Row[State]","$Row[Zip]"));}$pdf2->Output();
}

?>
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','font/');
require_once('PDF_Label.php');

$odbc = mysql_connect ('root', '') or die( "Could Not Connect to ODBC Database!" );
mysql_select_db('databasename');

if(!($pdfinfo == "nada")){
$query = mysql_query(stripslashes($pdfinfo), $odbc) or die (odbc_errormsg());

$pdf2 = new PDF_Label('5160', 'mm', 1, 2);
$pdf2->Open();$pdf2->AddPage();
// Print labels
while($Row=mysql_fetch_array($query)){$pdf2->Add_PDF_Label(sprintf("%s\n%s\n%s\n%s, %s, %s", "", "$Row[Firstname] $Row[Lastname]", "$Row[Address1] $Row[Address2]", "$Row[City]","$Row[State]","$Row[Zip]"));}$pdf2->Output();
}
You need to install the php-odbc*.rpm file if you are using redhat or Fedora Core
Avatar of Denisvt

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(sprintf("%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 ?


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.
Avatar of Denisvt

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.
The code I posted is using mysql (the code you posted was using odbc) so i transformed the odbc_function the the mysql-pendant.
Avatar of Denisvt

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','font/');
require_once('PDF_Label.php');

$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->AddPage();
// Print labels
while($Row=mysql_fetch_array($query)){$pdf2->Add_PDF_Label(sprintf("%s\n%s\n%s\n%s, %s, %s", "", "$Row[Firstname] $Row[Lastname]", "$Row[Address1] $Row[Address2]", "$Row[City]","$Row[State]","$Row[Zip]"));}$pdf2->Output();
}
?>

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');
Avatar of Denisvt

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,
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Denisvt

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 !