Jump to content

Form data to PDF to Email


darkgr33n

Recommended Posts

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
Link to comment
Share on other sites

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.

[code]
<?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
?>
[/code]

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

Now in the pdf.php, do this.

[code]
<?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.
?>
[/code]

Your host may not allow the exec functions. You can use [code=php:0]phpinfo();[/code] to find out.


Hope this helps,
Tom
Link to comment
Share on other sites

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 [b]exec [/b] 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: [b]PDFTEST.FORM.PHP[/b]

<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/savings/esaver.application.pdf">
<input type="submit" name="submit" value="send application" tabindex="3">
</form>

</body>
</html>

Page two: [b]PDFTEST.PDF.PHP[/b]

$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

Link to comment
Share on other sites

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
[list][*]pdf_id
[*]url
[*]vals
[*]email
[*]date_created[/list]

[code]
<?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");
?>[/code]

Hope this helps,
Tom
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

here is a decent tutorial for createing pdf files using the [url=http://www.devarticles.com/c/a/PHP/Use-PHP-to-Create-Dynamic-pdf-Files/1/]php_pdf library[/url] If you are not sure if you have the php_pdf lib enabled you can use
[code=php:0]php_info();[/code] to see what extentsions are enabled.

Or you can search [url=http://phpclasses.org]phpclasses.org[/url] for a class for createing pdfs

Good luck,
Tom
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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!



Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.