Solved

Creating PDF files from a Mysql BD

Posted on 2004-10-02
13
461 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now