Create a writable PDF and email as an attachement via PHP

Greg Alexander
Greg Alexander used Ask the Experts™
on
I have a writable PDF that I created with Acrobat. Is there any solution that would allow me to submit the actual PDF after the user hits submit. I realize that I could get the $_POST data, but I want to attach the PDF to an email (PHP mail()) and send the actual completed PDF.. Can this be done.. I just need a link to confirmed tutorial or something. Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
Please post a link to the writable PDF you created.  I will see if I can give you an example of how to do this.  Thanks, ~Ray
I use this software: http://www.webassist.com/dreamweaver-extensions/universal-email/

You do have to pay for it, but I think it's worth the money and does everything that I need.
Julian MatzTechnical Support
Top Expert 2005

Commented:
Yes, this is possible. I developed something like this a few months ago. It's a PDF form that you can download, fill in the details, and then press on an embedded submit button. The file is then streamed to a PHP script that attaches it to a mail message and sends it to the recipient. Let me dig up the code I used for it ...
Technical Support
Top Expert 2005
Commented:
Below is a PHP script you can use to process and mail the PDF. As far as I remember, you'll need Adobe LiveCycle Designer to create your PDF and apply the correct permissions. Let me know if you need help with that also.


<?php

error_reporting(E_ALL|E_STRICT);

$r = $_REQUEST;

$string = '';

$postdata = file_get_contents('php://input');
$name = 'PDF_form_'.date('Ymd_his').'_'.rand(1000,9999).'.pdf';
$file = "/tmp/$name"; //needs to be writable
$f = fopen( $file, 'w' );
fwrite( $f, $postdata );
fclose($f);

$finfo = finfo_open(FILEINFO_MIME); // return mime type ala mimetype extension
$info = finfo_file($finfo,$file);
finfo_close($finfo);

if ( $info !== 'application/pdf' )
{
	@unlink($file);
	die("Error: the file type you have submitted ($info) is not valid.");
}

$pdf = wordwrap(base64_encode($postdata),72,"\n",true);

$recipient = 'jane@example.com';
$cc = 'joe@example.com';
$sender = 'no-reply@example.com';
$subject = 'PDF Form';
$boundary = '==Multipart_Mix_x'.md5(rand(1000,9999)).'x';

$headers = "From: $sender\n";
$headers .= "Cc: $cc\n";
$headers .= "X-Mailer: NetLink IT Services\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'."\n\n";

$message = <<<__MESSAGE__

This is a multi-part message in MIME format.
--{$boundary}
Content-Type: text/plain; charset="ISO-8859-1"; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;

Application form attached. For support, please contact support @ netlink.ie.

--{$boundary}
Content-Type: application/pdf;
 name="{$name}"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="{$name}"

{$pdf}

--{$boundary}--

__MESSAGE__;

mail($recipient,$subject,$message,$headers);

header('Content-type: application/vnd.fdf');

?>
%FDF-1.2
%âãÏÓ
1 0 obj
<<
/FDF
	<<
	/Status(Thank you. Your details have been submitted and someone will get in touch with you regarding your application.)
	>>
>>
endobj
trailer
<</Root 1 0 R>>
%%EOF

Open in new window

Greg AlexanderLead Developer

Author

Commented:
Awesome thanks
Julian MatzTechnical Support
Top Expert 2005

Commented:
No bother, glad I could help. I forgot to metion the above script depends on Fileinfo. Fileinfo is included in PHP as of version 5.3 but can be installed as a Pecl extension in earlier versions. This really just checks the file's MIME type, so if you don't have it and don't want to install it, you should be able to create a workaround that doesn't depend on Fileinfo.
I tried this, but am getting en error:

Fatal error: Call to undefined function finfo_open() in /home/content/03/9382403/html/application.php on line 16
Julian MatzTechnical Support
Top Expert 2005

Commented:
alanpowell2007, please see my last comment. You need Fileinfo. If that's not an option, you'll need to use a different method to check the file's MIME type. If you're not using PHP version 5.3 or higher, you can get Fileinfo as a Pecl package:

$ pecl install fileinfo

OR, to make sure that's the right package name:

$ pecl search fileinfo
Thank you.  Right now, I'm just using one pdf file. I took out the whole if statement, and it seems to work. Are there any potential problems with doing this?
Julian MatzTechnical Support
Top Expert 2005

Commented:
I suppose it means that any type of file could be sent to this script, and it would get processed regardless of whether or not the file is a PDF. If you want an alterntive to Fileinfo, maybe something like this would work:

http://www.phpclasses.org/package/922-PHP-Class-for-retrieving-MIME-types-and-extensions-.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial