[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.5

PHP PDF Help Please

Asked by pertrai1 in PHP and Databases

Tags: php, pdf

Hello, I am new to learning how to put php together and now I am trying to use it to put data into a pdf. All is working but one problem. The script I am using is only allowing input from text fields. My html form has text fields, checkboxes, and text areas. I have tried to figure this out but I am at a loss. Could someone look at the code below and help me to understand how to let this use checkboxes and texareas. What it does it output form data to an already created pdf form.

I appreciate all help on this matter as I am stumped when it comes to the preg_match which I believe might be the problem. Thank you.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
<?php
/*
KOIVI HTML Form to XFDF Parser for PHP (C) 2004 Justin Koivisto
Version 1.0 - customized
Last Modified: 2006-08-25
 
    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or (at
    your option) any later version.
 
    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
 
    You should have received a copy of the GNU Lesser General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 
    Full license agreement notice can be found in the LICENSE file contained
    within this distribution package.
 
    Justin Koivisto
    justin.koivisto@gmail.com
    http://koivi.com
*/
 
    require_once 'createXFDF.php';
    $mailto='me@myemail.com';
    $subject='sample PDF form';
    $from='site@site.net';
    $pdf_file='http://www.mysite.com/sample.pdf';
    
    // set $redirect to '' to just output the fdf data to the browser, or give a URL for a
    // thank-you type of page
    $redirect='';
    
    // set $file_directory to '' to skip saving the file to server
    $file_directory=dirname(__FILE__).'/applicants';
    
    $message=<<<EndMessage
The XFDF creation script on your website has received data. Save the
attachment in this message to your hard disk. Open the PDF file and
use the import form data option under the File menu. Some setups allow
you to simply double-click the .xfdf file.
 
-- 
createXFDF PHP script by Justin Koivisto
http://koivi.com/fill-pdf-form-fields/
EndMessage;
 
    /**
     * fields accepted by this script
     */
    $text_fields=array(
    	'applicant',
    	'address',
    	'city',
    	'state',
    	'zip',
    	't_days',
    	't_even',
		'fax',
		'email',
		'full_mortality',
    );
 
    /**
     * ony text fields are checked in this script as no other type was provided in the html form
     * -justin 8/25
     */
    
    if(isset($_POST['submit'])){
		unset($_POST['submit']); // don't need to process the submit button
    	
		foreach($_POST as $k=>$v){
			if(in_array($k,$text_fields)){
				// this field was an "accepted" field
				// verify contents (these are single lines, so no \r or \n)
				if(preg_match('`[\r\n]+`',$v)){
					$clean[$k]=''; // bad data - send empty
				}else{
					$clean[$k]=$v;
				}
			}else{
				// bad field option, we do nothing with this
				unset($_POST[$k]);
			}
		}
		
        // get the XFDF file contents
        $xfdf=createXFDF($pdf_file,$text_fields);
 
        // this seems to be the most popular request, so we will mail the xfdf to an account
        
        // this is the file attachment's name - you may want to customize this to fit your needs.
	    $fileattname = "{$clean['applicant']}.xfdf";
	    $fileatttype = "application/vnd.adobe.xfdf"; 
	    $data=chunk_split(base64_encode($xfdf));
	    $mime_boundary = '==Multipart_Boundary_x'.md5(time()).'x'; 
	    $headers = "From: $from\n".
	    	"MIME-Version: 1.0\n".
	        "Content-Type: multipart/mixed;\n".
	        " boundary=\"{$mime_boundary}\"";
	    $message = "This is a multi-part message in MIME format.\n\n".
	        "--{$mime_boundary}\n".
	        "Content-Type: text/plain; charset=\"iso-8859-1\"\n".
	        "Content-Transfer-Encoding: 7bit\n\n".
	        $message."\n\n".
	        "--{$mime_boundary}\n".
	        "Content-Type: {$fileatttype};\n".
	        " name=\"{$fileattname}\"\n".
	        "Content-Disposition: attachment;\n".
	        " filename=\"{$fileattname}\"\n".
	        "Content-Transfer-Encoding: base64\n\n".
	        $data."\n\n".
	        "--{$mime_boundary}--\n";
        if(!mail($mailto,$subject,$message,$headers)){
        	// mail failed!
        	mail(
        		$mailto,
        		'ERROR in '.__FILE__,
        		'Unable to send xfdf file via attachment. Data follows:'."\n----- Begin -----\n$xfdf\n----- End -----\n"
    		);
        }
        
		$file_name=time().'-'.$fileattname;
    	if(strlen($file_directory) && file_exists($file_directory) & is_writeable($file_directory)){
    		// if we have defined a writable directory on the server, write the results there as well
    		$target=$file_directory.'/'.$file_name;
    		if($fp=fopen($target,'w')){
    			fwrite($fp,$xfdf,strlen($xfdf));
    			fclose($fp);
    			
    			// mail notification of file creation
    			mail($mailto,'Livestock Mortality Applicant Saved on Server',$target);
    		}else{
    			// can't open the file for writing...
    			mail($mailto,'XFDF file cannot be saved',"File cannot be written to server: $target\n");
    		}
    	}else if(strlen($file_directory)){
    		// can't use this directory - exists on server? writeable by web server process?
    		mail($mailto,'XFDF file directory cannot be used',"File cannot be written to server directory: $file_directory\n");
    	}
    	
    	if(strlen($redirect)){
    		// success - redirect if a redirect url is provided
    		header('Location: '.$redirect);
    		exit;
    	}else{
			// if no redirect, we want to just send the data to the browser
			
            // send the XFDF headers for the browser
            header('Content-type: application/vnd.adobe.xfdf');
            header('Content-Disposition: attachment; filename="'.$file_name.'"');
            echo $xfdf;
    	}
    }else{
    	die('This script is meant to be used in conjunction with the web forms on our website only.');
    }
?>
[+][-]03/11/09 06:58 PM, ID: 23864132Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/12/09 03:47 AM, ID: 23866693Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/14/09 11:40 PM, ID: 23890015Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: PHP and Databases
Tags: php, pdf
Sign Up Now!
Solution Provided By: icarey
Participating Experts: 2
Solution Grade: A
 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625