Convert dynamic PHP report to PDF and then email results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phishee
Forum Newbie
Posts: 1
Joined: Thu Nov 04, 2004 12:31 pm

Convert dynamic PHP report to PDF and then email results

Post by phishee »

Hello all,

I am currently looking for a solution to my PDF creation issue. First, I can successfully create a PDF using htmldoc (Linux-based) from many different clients' MySQL data. My first question would be - Is there a way to generate the PDF's filename from a DB so that each PDF is uniquely named and move it to the appropriate "reports" folder that I desire?

Additionally, I would like to have a PHP or possibly even shell script which would run with Cron to take the newly created PDF, archive it according to the date the report was run, and email it to the client's designated email address. The email address and client info would be pulled from a MySQL db. One of the key requirements is that the script iterate through the DB so as to create/email PDF for each respective client. Obviously my goal is total automation.

I realize this is a lot to request but I have been working diligently for some time and only been able to come up with some minor solutions. Thanks for all of your participation and answers. They will be appreciated.

Brandon (phishee)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Everything you say is very possible. First check the htmldoc help files to see how you name the files how you like, and then use the name that you get from a database query. Then as you say use cron to run another script to email and store everything you want, it would be a fairly simple task of looping through the results you get from the database.

You have said exactly what you want to do, so get some flow charts drawn up to show you how everything will work and then code it. Then ask any more questions if you get stuck.
User avatar
Crom
Forum Newbie
Posts: 8
Joined: Mon Nov 08, 2004 2:50 am
Location: Ukraine
Contact:

Post by Crom »

Is there a way to generate the PDF's filename from a DB so that each PDF is uniquely named and move it to the appropriate "reports" folder that I desire?
Imho the best idea is to generate filename as timestamp. Something like "month-day-year-hours-minutes-seconds.pdf" or something. This must give you and unique filename.
I would like to have a PHP or possibly even shell script which would run with Cron to take the newly created PDF, archive it according to the date the report was run, and email it to the client's designated email address. The email address and client info would be pulled from a MySQL db. One of the key requirements is that the script iterate through the DB so as to create/email PDF for each respective client. Obviously my goal is total automation.
First you run shell command something like
shell_exec() (http://www.php.net/manual/en/function.shell-exec.php) or system() (http://www.php.net/manual/en/function.system.php), exec() (http://www.php.net/manual/en/function.exec.php). Ie

Code: Select all

shell_exec("/usr/.../htmldoc")
with parameters;
then gzip the result file (see shell_exec);
Then run query selecting emails from database and then walking thru resulting array mail it to client.

Code: Select all

$result = mysql_query("select EMAIL_FIELD from TABLE");
while ($email = mysql_fetch_array($result))
{
  //build MIME message and send mail to the address $emailї'EMAIL_FIELD']
  //your hosting provider may have already PHP MIME class set up
  //or you can make it with your own (find in google "PHP MIME mail")
}
Another way is as kettle_drum wrote: put this all in shell script - will be no need to use PHP :)
Hope this will help you. If you have any question - post it here. Good luck
Post Reply