Jump to content


Photo

Form data to PDF to Email


  • Please log in to reply
14 replies to this topic

#1 darkgr33n

darkgr33n
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 01 September 2006 - 09:45 AM

Hi All

New to the forums, and pretty new to PHP actually, and I'm hoping someone may have experience of what I'm attempting to do, or at least know it can be done!

I currently have an HTML form which collects user input, and then opens an interactive PDF, filled in with the data, which the user can print.

I need to change the process slightly, and this is what I cannot get my head around

Rather than OPEN in a new window all populated with the data, I need the user to not see the PDF at all, rather they need to receive the PDF (stored on the server) filled with the data via email.

I can email an attachment ok, but can't see how the form data can be sent to a file on the server, and without the user seeing it, the PDF is filled with the form data, and then emailed to the user.

I've done a search on the forum, and can find no mention. I've done various searches online, but my knowledge makes it hard to follow some of the solutions ... and one solution I did understand would cost £1500.

Could anyone help out please ?

Thanks in advance for any help.

Paul

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 01 September 2006 - 10:30 AM

You can try an exec function to pack door that process. So lets say that you have a file called pdf.php and you are running example.php. Try this.

<?php
$some_info = mysql_real_escape_string(trim($_POST['some_info']));
$some_more_info = mysql_real_escape_string(trim($_POST['some_more_info']));
//do something else with the info ..i.e. update a database or what ever.
//now here is the exec function.
exec("path/to/pdf.php $some_info, $some_more_info > /dev/null &");
//this will run the pdf.php in the background
//continue with this script
?>

If you do not know the acutal location of the pdf.php, You can use this
$_SERVER['DOCUMENT_ROOT']
to find the real location of the file. So what I would do is just echo it in a some file like this
echo $_SERVER['DOCUMENT_ROOT']
.

Now in the pdf.php, do this.

<?php
$some_info = $_SERVER['argv'][1];
$some_more_info = $_SERVER['argv'][2];

//now you can create your pdf file and email it to the user. 
?>

Your host may not allow the exec functions. You can use
phpinfo();
to find out.


Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 darkgr33n

darkgr33n
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 01 September 2006 - 11:22 AM

Hi Tom

Thanks, I appreciate your quick reply. I'm encouraged that you haven't said it's not possible, and I've not come across exec before, so thanks for that too.

Unfortunately, I'm not 100% sure how to proceed  :-\  ...  typical noob  ::) ... or how your code fits with mine.

My current PDF code, which takes post vars, always opens in a browser window ... and i'm not really sure about fdf etc.

Here's a version of what I have that works when opening to a new browser window - it does have some uncommented code, but I've never been sure how to use it. The PDF itself is already designed, and waiting to accept my data.

Page one: PDFTEST.FORM.PHP

<html>
<head><title></title></head>
<body>

<form name="contact" method="post" action="pdftest.pdf.php">
<input type="text" name="first_name" value="" tabindex="1">
<input type="text" name="surname" value=""  tabindex="2">
<input type="hidden" name="url" value="http://localhost/sav...plication.pdf">
<input type="submit" name="submit" value="send application" tabindex="3">
</form>

</body>
</html>

Page two: PDFTEST.PDF.PHP

$url = $_POST['url'];
$values=$HTTP_POST_VARS;

$fdfdata = "%FDF-1.2\n%âãÏÓ\n";
$fdfdata .= "1 0 obj \n<< /FDF ";
$fdfdata .= "<< /Fields [\n";

//loop that adds the field names and values
foreach($values as $key=>$val)
{
$fdfdata.="<< /V ($val)/T ($key) >> ";

}


$fdfdata .= "]\n";
$fdfdata .= "/F ($url) >>";
$fdfdata .= ">>\nendobj\ntrailer\n<<\n/Root 1 0 R\n>>\n";
$fdfdata .= "%%EOF";

//uncomment if you actually want to write a FDF file
//touch ($form.".fdf");
//$FD = fopen ($form.".fdf", "w");
//fwrite($FD,$fdfdata);
//fclose ($FD);

/*** Now we display the FDF data which causes Acrobat to start  ***/

header ("Content-Type: application/vnd.fdf");
print $fdfdata;

//use this instead of print if you are writing to a file rather than just the browser
//header("Location: ".$form.".fdf");
?>

Thanks again for looking!

Paul



#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 01 September 2006 - 12:20 PM

well all you have to do is just change the pdf format to write the acutal pdf file. Like this.
I would recomend useing a database table to store all of the infomation in and using the id to name the file. Like this
I am going to assume that you have the following fields in the database and that the table name is pdf
  • pdf_id
  • url
  • vals
  • email
  • date_created

<?php
$url = mysql_real_escape_string(trim($_POST['url']));
$vals=$HTTP_POST_VARS;
$email = mysql_real_escape_string(trim($_POST['email']));
$date_created = date("Y-m-d H:i");
$sql = mysql_query("INSERT INTO `pdf` (`url`, `vals`, `email` `date_created`)
        VALUES ('$url', '$vals', '$email', '$date_created')");
if (!$sql) {
   die("There was an error with updateing the database");
}
$q = mysql_query("SELECT * FROM `pdf` WHERE `email` = '$email' AND `date_created` = '$date_created'");
if (!$q) {
   die("There was another error");
}
while ($row =mysql_fetch_assoc($q)) {
     $name = $row['pdf_id'];
}
  
$fdfdata = "%FDF-1.2\n%âãÏÓ\n";
$fdfdata .= "1 0 obj \n<< /FDF ";
$fdfdata .= "<< /Fields [\n"; 

//loop that adds the field names and values
foreach($values as $key=>$val)
   {
   $fdfdata.="<< /V ($val)/T ($key) >> ";
   
   }

   
$fdfdata .= "]\n";
$fdfdata .= "/F ($url) >>";
$fdfdata .= ">>\nendobj\ntrailer\n<<\n/Root 1 0 R\n>>\n";
$fdfdata .= "%%EOF";

//uncomment if you actually want to write a FDF file
touch ($name.".fdf");
$FD = fopen ($name.".fdf", "w");
fwrite($FD,$fdfdata);
fclose ($FD);

//now email the pdf to the user.
// After the email was sent I would delete the pdf unless you do not want to
//to delete the pdf just uncomment the next line
//unlink($name . ".fdf");
?>

Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 darkgr33n

darkgr33n
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 01 September 2006 - 12:37 PM

Thanks again Tom.

I'll have another go at getting it working, but I'm not actually using a DB at all. It's basically a webform that people fill in, to get an application form emailed to them. I had that bit working, but now we want to take the info from the html form, and fill in the relevant bits on the PDF, so they just have to sign it and pop it in the post.

MySQL DB will be coming next year!

Cheers again, and I'll let you know how I get on ...

P

#6 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 01 September 2006 - 12:46 PM

I would recommend that you use a database. I personaly would rather store the infomation in  a database and then delete the pdf. Then if the user needs the pdf again I would just create it from the fields in the database.

Also, if you use a database then you will be able to see how many people are having this pdf created and will have there email on record for future use.


Good Luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#7 darkgr33n

darkgr33n
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 01 September 2006 - 01:03 PM

I agree absolutely. I've used DBs previously (although was SQLSERVER7 + ColdFusion / ACCESS + ASP), but this is a relatively new position (taking over from external web services) and unfortunately there is no DB in place yet.

This is something I will definately get on to early next year we really should be using one as standard.

Anyway, I've played with the code a little, and have created an FDF file !!!  :o  ;D

I haven't yet emailed it to myself to see how it works, but when I double click it, i assume it looks for the relevant PDF on the server, and populates it on the fly ?

Thanks so much for your help - I feel like I'm winning, which is always a good feeling to take into the weekend!!

P

#8 darkgr33n

darkgr33n
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 01 September 2006 - 03:24 PM

Thanks to Toms invaluable help, I've almost got this working perfectly!

My form can create an FDF file, and email it out to the user as an attachment, which when he opens contains the PDF and his data.  ;D

The only thing I feel that would be necessary now, would be to be able to actually somehow merge the FDF and PDF on the server, and email out the PDF.

I think many people would feel uneasy opening an FDF file - most users would not know what one is - and obviously PDF everyone has heard of, and would expect.

Is this possible  ???

Thanks

P



#9 darkgr33n

darkgr33n
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 04 September 2006 - 08:08 AM

BUMP  ;D

#10 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 September 2006 - 08:38 AM

here is a decent tutorial for createing pdf files using the php_pdf library If you are not sure if you have the php_pdf lib enabled you can use
php_info();
to see what extentsions are enabled.

Or you can search phpclasses.org for a class for createing pdfs

Good luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#11 darkgr33n

darkgr33n
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 04 September 2006 - 09:31 AM

Thanks again Tom, much appreciated  ;D

I'll read through and see how I go.

#12 darkgr33n

darkgr33n
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 04 September 2006 - 11:13 AM

I have read through the link, and unless I'm missing something  :-[, it seems to refer more to actually constructing a PDF from scratch, as opposed to just filling in an already designed interactive form with the FDF data file I now have (thanks to Tom!).

I've done some more research, and it would appear something like PDFTK may help, but if possible I'd like to keep the merging process within PHP, in order to minimise server changes (our Host may not allow it ?)

Thanks

#13 darkgr33n

darkgr33n
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 04 September 2006 - 11:34 AM

More research seems to suggest what I need is not going to happen  :(

On the adobe site, I found reference to server-side merging of FDF and PDF files, but bascially Acrobat is not licensed for server side merging, so no way can I do it. This can happen on the user's client, provided they have Acrobat; but if they have Acrobat you don't need any programming, it "just happens"

There is a reader extension available, but apparently in the region of $90k ... er, that's not going to happen!





#14 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 04 September 2006 - 04:41 PM

Why do you want fdf to be merged into a pdf. You could just create the pdf and not have to worry about fdf and save your self some bandwidth.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#15 darkgr33n

darkgr33n
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 05 September 2006 - 08:24 AM

Hi Tom

Er, to be honest, I don't know!  :-[

When I read the tutorial you pointed me to, it seemed to be about creating the PDF from scratch - setting fonts, positioning of logo's etc - whereas I already have the PDF fully designed (with InDesign) and then with Acrobat Pro, I've added some text boxes, checkboxes etc, and just need to post the HTML form variables into the PDF.

The first version of my form worked fine. It filled the PDF with the info, but then opened it into a browser window. The reason I thought I needed to go down the FDF route, was so I could take the HTML form data, fill the PDF with it, and then email it to the client, all without them seeing it.

When I managed (thanks!) to get an FDF that I could double click and open, I thought I was there.

Maybe I've gone down the wrong route.

Don't you just love noobs ...  :-\ !!

Thanks

P




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users