Solved

Creating PDF files from a Mysql BD

Posted on 2004-10-02
13
463 Views
Last Modified: 2013-12-12
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.
0
Comment
Question by:Denisvt
13 Comments
 
LVL 32

Expert Comment

by:ldbkutty
ID: 12206967
0
 
LVL 3

Author Comment

by:Denisvt
ID: 12208076
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 !

0
 
LVL 48

Expert Comment

by:hernst42
ID: 12213219
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();
}
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 9

Expert Comment

by:gtkfreak
ID: 12219759
You need to install the php-odbc*.rpm file if you are using redhat or Fedora Core
0
 
LVL 3

Author Comment

by:Denisvt
ID: 12219871
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 ?


0
 
LVL 1

Expert Comment

by:Zenock
ID: 12253753
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.
0
 
LVL 3

Author Comment

by:Denisvt
ID: 12542249
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.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 12542329
The code I posted is using mysql (the code you posted was using odbc) so i transformed the odbc_function the the mysql-pendant.
0
 
LVL 3

Author Comment

by:Denisvt
ID: 12670579
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 ? ;-)
0
 
LVL 48

Expert Comment

by:hernst42
ID: 12672459
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');
0
 
LVL 3

Author Comment

by:Denisvt
ID: 12675703
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,
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 250 total points
ID: 12676098
$pdfinfo must be a vaild sql-statement
0
 
LVL 3

Author Comment

by:Denisvt
ID: 12677417
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 !
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question