2
votes

For an insurance company, I've created PDF forms of their various insurance applications. These will be available on their website (when it is done). Each form, being in PDF, looks exactly like the printed version of the application. The idea is that a user fills it out online, hits the (visible but unprintable) "Submit" button at the bottom of the form, and the entire completed PDF is sent to the company. That way they can print it out, and carry on with their current paper filing system.

I experimented with using a "mailto:..." URL for the Submit button, but that requires an external mail program. It also gives the attached PDF a random temp filename.

Therefore, I'd like to send the PDF to a php script, change the name, and email it to the company from there. I played with it a bit, and I can reach the destination script, but I have no idea what happens to the PDF. I expected it to show up in $_FILES, but nothing.

Saving to a database is not an option.

When preparing the PDF form, you can choose how you would like to submit: as an HTML form (as Sepodati said), as XML data, as FDF (which only sends data, and will open the original PDF when displaying the data), or as a completed PDF. For ease of use for the site owners, who are not computer savvy, I'm going to send them a completed PDF that they can print.

So, the same question remains: where does my PDF file go when I get to my destination script?

I'm submitting the PDF via a Submit button on the PDF itself. You'll have to be familiar with PDF forms to understand this. For the URL, I put the destination PHP script.

In my destination script, I tried the following PHP Code:

    reset ($_FILES); 
    while (list ($key, $val) = each ($_FILES)) { 
       echo "$key => $val<br />\n"; 
    }  

I did this for $_FILES, $_GET, $_POST, and $_REQUEST. Nothing came up, as though each array was empty. I might have done this wrong, though.

I put phpinfo() in the script, and these vars appear related to the PDF...

_SERVER["CONTENT_LENGTH"] = 37722
_SERVER["CONTENT_TYPE"] = application/pdf
_SERVER["HTTP_USER_AGENT"] = Mozilla/4.0 (compatible; Adobe Acrobat Control Version 5.00 for ActiveX)
_SERVER["REQUEST_METHOD"] = POST

The content length is approximately the size of the PDF.

Something else I didn't notice before is that upon submission, Internet Explorer displays this page (or a name like it):

C:\WINDOWS\TEMP\pa003773.html

This page contains all the output from my script.

2

2 Answers

1
votes

It's very interesting that it's supposed to send the entire PDF. I would be interested in knowing how it does that because I've searched the Adobe site for information on how to submit an entire PDF file by clicking a button on the PDF form.

The only thing I found on their site was information about the FDF and XFDF formats. Both of these formats have to be parsed in some way by your form handler script and aren't included in $_FILES the way a file is uploaded when you select the file from the filesystem.

$fptr = fopen("temp.fdf", "w"); 
fputs($fptr, $HTTP_RAW_POST_DATA); 
fclose($fptr); 
1
votes

This is a really old post so I'm sure you've gotten your answer by now but here's what worked for me in case anyone else stumbles across this in the future. BTW- this only works for if you are submitting the entire PDF with the form fields completed.

The address you would put on your submit button would be something like http://youserver.com/lib/php/[email protected]&formName=Asset%20Inventory

<?php
  ob_start();
  $file = file_get_contents("php://input"); //Gets binary PDF Data
  $time = microtime(true);

  //Names pdf file based on the time to the microsecond so nothing gets overwritten.  Obviously you can change this to whatever you want or do something like $_REQUEST['formName'] and just include the form name in your URL from your pdf submit button
  $newfile = "forms/" . $time . ".pdf"; 
  $worked = file_put_contents($newfile, $file); //Creates File
  ob_end_clean();

  //Upon completion you can either return XFDF to update a field on your form or display a PDF document.  I chose the PDF route because it worked for our situation.
  $successFile = 'success.pdf';
  $errFileFile = 'error.pdf';
  require 'PHPMailer/PHPMailerAutoload.php';

  $mail = new PHPMailer;
  $mail->isSendmail();
  $mail->setFrom('From email address', 'from name');
  $mail->addReplyTo('Reply to email', 'Reply to name');
  $mail->addAddress($_REQUEST['email']); //get email from URL- alternately you can just hardcode in an address
  $mail->addAttachment( $newfile );
  $mail->isHTML(true);
  $mail->Subject = 'New form submission';
  $mail->Body = 'A new form has been submitted.';

  if(!$mail->send()) {
    header('Content-type: application/pdf');
    readfile($errFile);
  } else {
    header('Content-type: application/pdf');
    readfile($successFile);
    //if you want to delete the file from the server after emailing: unlink($newfile);
  }
?>