2011-04-12 1 views
0

양식을 사용하여 http://koivi.com/fill-pdf-form-fields/에서 PDF- 스크립트를 작성하고 전자 메일로 보내주십시오. Fortunally 그는 이미 "process-xfdf.php"파일을 사용하여 이것을 가능하게 만들었습니다. 그러나 나는 항상 "이 스크립트는 웹 사이트의 웹 양식과 함께 사용해야합니다."라는 오류 메시지가 나타나기 때문에이 파일을 올바르게 사용하는 방법을 찾지 못하는 것 같습니다. - 오류 메시지.HTML 양식을 PDF로 전자 메일로 보냄

내가 뭘 잘못하고 있니?

Index.php는

<?php 
/* 
KOIVI HTML Form to FDF Parser for PHP (C) 2004 Justin Koivisto 
Version 2.1 
Last Modified: 2005-04-21 

    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 
    [email protected] 
    http://koivi.com 
*/ 

    include dirname(__FILE__).'/pdf_process.php'; 
    echo '<?xml version="1.0" encoding="iso-8859-1"?>',"\n"; // because short_open_tags = On causes a parse error. 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Using HTML forms to fill in PDF fields with PHP and FDF</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
    <meta name="description" content="A PHP solution to filling a PDF file's form fields with data from a submitted HTML form." /> 
    <style type="text/css"> 
    @import url(http://koivi.com/styles.css); 
    </style> 
    <!--[if lt IE 7]><script src="/ie7/ie7-standard.js" type="text/javascript"></script><![endif]--> 

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> 
</script> 
<script type="text/javascript"> 
_uacct = "UA-207722-1"; 
urchinTracker(); 
</script> 
</head> 


<body> 
    <div id="container"> 
    <div id="intro"> 
    <h1>Using HTML forms to fill in PDF fields with PHP and FDF</h1> 
    <p>Using a simple HTML form and a small PHP function, it is possible to fill in the fields of a PDF form file for viewing without having to install any PHP libraries or modules. This is accomplished by using a pre-made PDF file with all the form fields necessary and FDF files.</p> 
    <p>By taking values provided via a GET or POST web request from an HTML form or hyperlink, we can generate an FDF file that, when opened, will provide the values to fill the PDF form fields in the original document.</p> 
    <h1>Tutorial</h1> 
    <p>I have now posted a <a href="./tutorial.php">tutorial</a> on how to use the programming found in this article for those of you who need just a little extra help implementing this.</p> 
    </div> 

<?php 
    if(isset($CREATED)){ 
?> 
    <div id="pageResults"> 
    <h1>Your Result</h1> 
    <p>You can now <a href="<?php echo str_replace(dirname(__FILE__).'/','',$fdf_file) ?>">download</a> the file you just created. When downloaded, open the FDF file with your Adobe Acrobat or Acrobat Reader program to view the original PDF document with the fields filled in with the information you posted through the form below.</p> 
    </div> 
<?php 
    } 
?> 

    <div> 
    <h1>Creating the PDF file</h1> 
    <p>First, you need to have created your <a href="test.pdf">PDF form file</a>. I do this using the form tools in Adobe Acrobat. Remember to name each field in your document, you will need those names later when you create the HTML form.</p> 
    </div> 

    <div> 
    <h1>Creating the HTML form</h1> 
    <p>Next, you need to create the form fields to fill out in an HTML form. Below is an example that fits with the above PDF file. Note that the field names in your HTML form must match the field names in your PDF file.</p> 
    <form method="post" action="process-xfdf.php"> 
    <table cellpadding="3" cellspacing="0" border="0"> 
     <tr><td>Name</td><td><input type="text" name="__NAME__" value="<?php if(isset($_POST['__NAME__'])) echo $_POST['__NAME__']; ?>" /></td></tr> 
     <tr><td>Title</td><td><input type="text" name="__TITLE__" value="<?php if(isset($_POST['__TITLE__'])) echo $_POST['__TITLE__']; ?>" /></td></tr> 
     <tr><td>Email</td><td><input type="text" name="__EMAIL__" value="<?php if(isset($_POST['__EMAIL__'])) echo $_POST['__EMAIL__']; ?>" /></td></tr> 
     <tr><td>Address</td><td><input type="text" name="__ADDRESS__" value="<?php if(isset($_POST['__ADDRESS__'])) echo $_POST['__ADDRESS__']; ?>" /></td></tr> 
     <tr><td>City</td><td><input type="text" name="__CITY__" value="<?php if(isset($_POST['__CITY__'])) echo $_POST['__CITY__']; ?>" /></td></tr> 
     <tr><td>State</td><td><input type="text" name="__STATE__" value="<?php if(isset($_POST['__STATE__'])) echo $_POST['__STATE__']; ?>" /></td></tr> 
     <tr><td>Zip</td><td><input type="text" name="__ZIP__" value="<?php if(isset($_POST['__ZIP__'])) echo $_POST['__ZIP__']; ?>" /></td></tr> 
     <tr><td>Phone</td><td><input type="text" name="__PHONE__" value="<?php if(isset($_POST['__PHONE__'])) echo $_POST['__PHONE__']; ?>" /></td></tr> 
     <tr><td>Fax</td><td><input type="text" name="__FAX__" value="<?php if(isset($_POST['__FAX__'])) echo $_POST['__FAX__']; ?>" /></td></tr> 
    </table> 
    <input type="submit" /> 
    </form> 
    </div> 

    <div> 
    <h1>Processing the POST request</h1> 
    <p>When the form is submitted, the $_POST array is passed to a function along with the location of the PDF file. The function returns the contents of an FDF file. This can them be used to write an FDF file to download an view.</p> 
    </div>  

    <div> 
    <h1>The code behind this</h1> 
    <div class="example"><?php highlight_file(dirname(__FILE__).'/createFDF.php'); ?></div> 
    <h2>Code for using XFDF formatted data</h2> 
    <div class="example"><?php highlight_file(dirname(__FILE__).'/createXFDF.php'); ?></div> 
    </div> 

    <div> 
    <h1>Code Download</h1> 
    <p>Because of the number of requests I have received for help getting this to work or copies of the code and PDF files, I have created a ZIP archive of this entire directory and am making it <a href="/fill-pdf-form-fields.zip">available for download</a> (~180K - updated 2006-09-08).</p> 
    <p>Inside this zip file you will also find a file named <code>process-xfdf.php</code> which contains sample code for sending results as an email attachment as well as sending the output directly to the browser.</p> 
    </div> 

    <div> 
    <h1>Updates</h1> 
    <p>Seriously, read these updates as they may have some additional information about what YOU are trying to accomplish</p> 
    <ul> 
    <li> 
     <p><b>2011-01-18</b> Unbelievably, I am still getting a TON of email about this article. The most often-asked question I receive is how to send the result via email attachment. For those of you that missed it, check under the "Code Download" section of the article. I added that mid 2009. I will no longer reply to emails asking for an example of how to do this since the answer is right next to where you click to download. (There are a lot of people that don't take the time to read!)</p> 
     <p>The next most frequent question is where to find the final PDF file. This process does not create a PDF file. It only creates FDF or XFDF files. However, there have been a few people nice enough to reply back to me with a solution after I explained that my code doesn't do it. While I have not tried it myself (as I haven't had the time or need to do so), <a href="http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/">PDFLabs' pdftk</a> boasts "Fill PDF Forms with FDF Data or XFDF Data and/or Flatten Forms." If anyone wants to write a how-to for this process, give me a URL and I will link over to you. Alternatively, email it to me, and I will create a page to put up with this article as well.</p> 
    </li> 
    <li> 
     <p><b>2010-02-17</b> Minor update to the createXFDF function to allow for html entities in the values posted to the script.</p> 
    </li> 
    <li> 
     <p><b>2009-07-15</b> It has been a few years since I posted this and nearly as long since I have used it. However, I still get emails about this all the time. Apparently the programming is still useful for people to store the form data. I did receive an email about a month or so ago about a rogue line of code in the createXFDF function that was left over from the createFDF version of the function. Thank you AndreasDo&ouml;ler for bringing this to my attention. I have now updated the function and download appropriately.</p> 
    </li> 
    <li> 
     <p><b>2006-09-08</b> For those of you having problems with using PDF files created from Acrobat 7 or Designer, you'll be please to hear that I have an update for you.</p> 
     <p>The problems were stemming from the way that the latest Acrobat, Acrobat Reader and Designer handle form data. Instead of using the previous FDF format, these programs are now defaulting to the newer XFDF file format. This is basically an XML-like file structure to define the values that would be filled into the fields.</p> 
    </li> 
    <li> 
     <p><b>2005-09-12</b> In the move from my last web server, I found that some of my files were corrupted, and this particular demonstrations was not working correctly. That should be all fixed up now.</p> 
     <p>Also, I did update the createFDF function a little as well. It turned out that Acrobat 7 was having problems importing data into the PDF form if there were spaces in the field definitions inside the FDF file. I have removed those spacer, and tested on a couple simple files. I will test on a better file with more options when I have a chance.</p> 
    </li> 
    <li> 
     <p> 
     <b>2005-04-21</b> - Thanks to Adrian James for submitting information about the FDF format of a multiple value selection field 
     (<code>&lt;select multiple="multiple"&gt;</code> HTML equivalent). The createFDF function has now been updated to support these fields as well. 
     </p> 
    </li> 
    </ul> 
    </div> 

<script type="text/javascript"><!-- 
google_ad_client = "pub-6879264339756189"; 
google_alternate_ad_url = "http://koivi.com/google_adsense_script.html"; 
google_ad_width = 728; 
google_ad_height = 90; 
google_ad_format = "728x90_as"; 
google_ad_type = "text_image"; 
google_ad_channel ="7653137181"; 
google_color_border = "6E6057"; 
google_color_bg = "DFE0D0"; 
google_color_link = "313040"; 
google_color_url = "0000CC"; 
google_color_text = "000000"; 
//--></script> 
<script type="text/javascript" 
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
    </div> 

<?php include_once 'site_menu.php'; ?> 

</body> 
</html> 

프로세스 xfdf.php :

<?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 
    [email protected] 
    http://koivi.com 
*/ 

    require_once 'createXFDF.php'; 
    $mailto='[email protected]'; 
    $subject='Submitted Form Data'; 
    $from='[email protected]'; 
    $pdf_file='http://www.hatsofcress.com/html2pdf/test.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='http://www.hatsofcress.com'; 

    // set $file_directory to '' to skip saving the file to server 
    $file_directory=dirname(__FILE__).'/results'; 

    $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(
     '__NAME__', 
     '__TITLE__', 
     '__EMAIL__', 
     '__ADDRESS__', 
     '__CITY__', 
     '__STATE__', 
     '__ZIP__', 
     '__PHONE__', 
     '__FAX__', 
    ); 

    /** 
    * 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,$clean); 

     // 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['__NAME__']}-{$clean['__PHONE__']}.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,'XFDF file saved to 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.'); 
    } 
?> 

당신의 사람이 나를 도와 드릴까요? 나는 필사적으로 도움이 필요하다! process-xfdf.php의 소스에서

답변

1

:

if(isset($_POST['submit'])){ 
    // [omitted] 
}else{ 
    die('This script is meant to be used in conjunction with the web forms on our website only.'); 
} 

그것은 스크립트가 POST를 통해 특정 형태의 입력을 기대하는 분명하고도 존재하는 $text_fields 배열의 모든 필드를 기대하는 그 때문에 PDF로 채울 수 있습니다.

문제 해결을 시도하지 않았으며 소스를 읽고 오류가 발생한 이유를 이해하지 못했습니다. 오류 메시지는 해당 스크립트의 마지막 코드 줄입니다.

당신은 당신 달성하기 위해 이해을 가지고 거기 설정, 상태, 오류 메시지 및 기타 행동이 있기 때문에, 위에서 아래로 전체 예제 스크립트를 읽어 필요 에가는거야 당신의 골.

+0

안녕하세요 찰스 전체 스크립트를 살펴 보았습니다. 스크립트의 해당 부분도 알아 냈습니다. 그러나 나는 "process-xfdf.php"를 어디서 어떻게 사용해야하는지 이해하지 못했다. 그리고 너는 나를 더 도와주지 않았다. 액션에서 process-xfdf.php를 타이핑하면 죽는 메시지가 나옵니다. 다르게해야 할 일은 무엇입니까? –

+0

양식 작업을 "process-xfdf.php"로 변경했습니다. 전자 메일 정보에 입력됩니다. '__NAME__', '__TITLE__', '__EMAIL__', '__ADDRESS__', '__CITY__', '__STATE__', '__ZIP__', '__PHONE__', '__FAX__'등의 예제 텍스트 필드에 $ text_fields를 지정했습니다. 그리고 여전히 오류가 발생합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 당신은 그것을 일하게 할 수 있습니까 ?? –

+0

위 필드는 앞뒤에 두 개의 밑줄이 있습니다. StackO. 그것을 굵게 번역합니다. –

-1

찰스, 당신은 오류의 원인이되는 스크립트의 정확한 부분을 찾아 내지 못합니다. 오류는 다이 스크립트가 아니라 스크립트의 다른 섹션으로 인해 발생합니다.

처음에는이 양식에 사용 된 모든 파일이 같은 디렉토리에 있는지 확인하십시오.

다이 메시지를 받고 있기 때문에 양식에 양식 동작이 올바르게 설정되어 있음을 의미합니다. 그렇지 않다면 당신은 그 죽음의 메시지를 듣지 않을 것입니다. :)

스크립트를 다시 확인하십시오. 위의 코드를 복사하여 작동시킬 수있었습니다. 이메일 설정 만 변경했는데 작동합니다.