簡體   English   中英

HTML表單以PDF格式發送到電子郵件

[英]HTML form to PDF sent to e-mail

我使用來自http://koivi.com/fill-pdf-form-fields/的Form to PDF腳本,希望它可以將PDF發送到電子郵件。 幸運的是,他已經通過使用文件“ process-xfdf.php”使這成為可能。 但是,我似乎找不到正確使用此文件的方法,因為我總是得到“該腳本只能與我們網站上的Web表單一起使用。”-錯誤消息。

我究竟做錯了什么?

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
    justin.koivisto@gmail.com
    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>

process-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
    justin.koivisto@gmail.com
    http://koivi.com
*/

    require_once 'createXFDF.php';
    $mailto='kb@hatsofcress.com';
    $subject='Submitted Form Data';
    $from='webform@hatsofcress.com';
    $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的源代碼中:

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中。

同樣清楚的是,您沒有嘗試進行任何故障排除,也沒有進行任何嘗試來閱讀源代碼並了解發生錯誤的原因。 錯誤消息是該腳本中的最后一行代碼。

您將需要從上至下閱讀整個示例腳本,因為在其中必須要理解設置,條件,錯誤消息和其他行為才能實現目標。

Charles,您遠遠沒有找到導致錯誤的腳本的正確部分。 該錯誤不是由die腳本引起的,而是該腳本的不同部分引起的。

對於初學者,請確認用於此表單的所有文件都在同一目錄中。

由於您收到模具消息,因此您的表單已正確設置了表單操作。 如果不是,您將不會收到死亡消息。 :)

仔細檢查您的腳本。 通過復制上面的代碼,我可以使它正常工作。 我只更改了電子郵件設置,它可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM