代码之家  ›  专栏  ›  技术社区  ›  serhio

在wordpress中提交到pdf

  •  0
  • serhio  · 技术社区  · 14 年前

    当用户点击提交按钮时,是否有一个插件或其他东西从用户输入的表单数据创建pdf文件?

    5 回复  |  直到 14 年前
        1
  •  5
  •   Marty    14 年前

    如果你想填写一份表格,然后处理表格的详细信息,并通过电子邮件发送给你,附带一份pdf格式的详细信息附件,几周前我也有类似的事情,我找不到任何可以按照我想要的方式工作的东西,所以……

    我设置表单,然后使用自定义页面模板将其分配给页面,然后使用php和 html2pdf class 我创建了我的pdf,并通过电子邮件作为附件发送…

    这是我用的密码。 已为此页缩小(请记住清理用户输入)。

    <?php
    /*
    Template Name: FORMTOPDF
    */
    ?>
    <?php get_header(); ?>
    <style>
    /* STYLES FOR ERROR PLACEMENT */
    label {
      width: 80px;
      text-align: right;
      float: left;
    }
    .formerror {
      border: 1px solid red;
      background-color : #FFCCCC;
      width: auto;
      padding: 5px 0;
      padding-left:10px;
    }
    .errortext {
      font: bold smaller sans-serif;
    }
    </style>
    <?php 
    // CREATE AN ARRAY FOR OUR ERRORS
    $arrErrors = array();
    // Check for FORM SUBMISSION
    // using hidden form field
    if(isset($_POST['action']) && ($_POST['action']=='send')) 
    { 
    /* ================= START FORM DATA ========================= */
        $name  = trim($_POST['name']); 
        if ($name=='') $arrErrors['name'] = 'Please provide your name.';
    
        $email = trim($_POST['email']);
        if ($email=='') $arrErrors['Email'] = 'Please provide your Email Address.';
    
        $comments = trim($_POST['your-comments']);
        if ($comments=='') $arrErrors['Comments'] = 'Please add your Comments.';
    /* ================= END FORM DATA ========================= */ 
    
        if (count($arrErrors) == 0) {
            // Process form here
        /* ================= START PDF CREATION ========================= */
        $strContent = "<p>Submission from ".$name."</p>";
        $strContent.= "<p><strong>Name</strong>:".$name."</p>";
        $strContent.= "<p><strong>Email </strong>: ".$email."</p>";
        $strContent.= "<p><strong>Comments</strong> : <br />".$comments."</p>";
        /* ================= END PDF CREATION ========================= */
        // Include our HTML to PDF creator
        // FROM THEME DIRECTORY?
        require(TEMPLATEPATH.'/html2pdf/html2fpdf.php');
        $pdf=new HTML2FPDF();
        $pdf->AddPage();
        // folder location of HTML file
        $fileLocation  = "wp-content/uploads/";
        // Call to the file name from the URL
        $fileName = "Form_Submission_From_".$name;
        // add the location 'wp-content/uploads/' to the fileToOpen
        $fileToOpen  = $fileLocation;
        // Then add the actual file name // form_submission.pdf
        // output should look like 'wp-content/uploads/form_submission_from_(name).pdf'
        $fileToOpen .= $fileName.".pdf";
        // Open the file with read access
        $fp = fopen($fileToOpen,"r");
        //$strContent = fread($fp, filesize($fileToOpen));
        // Close of the page
        fclose($fp);
        // Create new PDF document from the Content
        $pdf->WriteHTML($strContent);
        // create our PDF in the wp uploads folder
        $pdf->Output("wp-content/uploads/" .$fileName. ".pdf");
        /* ================= END PDF ========================= */
    
        /* ================= START EMAIL ========================= */
        $headers= "From: YourWebsite <info@yourwebsite.co.uk>\r\n\\";
        $emailSubject = "Submission from " . $name;
        $emailAdmin = "admin@yourwebsite.co.uk"; 
        $emailMessage = "Submission from ".$yourcompanyname."\n\n";
        $emailMessage.= "Company Name: ".$yourcompanyname."\n";
        $emailMessage.= "Email : ".$email."\n";
        $emailMessage.= "Comments : \n".$comments."\n\n";
        $attachments = array(WP_CONTENT_DIR ."/uploads/".$fileName.".pdf", $target_path);
        wp_mail($emailAdmin, $emailSubject, $emailMessage, $headers, $attachments);
    
        // Delete our PDF from the server after email Sent
        // uncomment this to delete after email sent?
        //unlink($fileToOpen);
        /* ================= END EMAIL ========================= */
    
        // show thank you message if successful
        $strGood = '<div class="formerror" style="background:#FFC;">
        <h2>Thank You</h2>
        <p>Thank you for contacting us.</p>
        </div>';
    
    
        }else{
    
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
         $strError = '<div class="formerror"><p><img style="margin-left:10px;" src="'.get_option('home').'/wp-content/themes/mytheme/media/images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><strong>Please check the following and try again:</strong></p><ul style="margin-left:50px;">';
    
         // Get each error and add it to the error string
         // as a list item.
         foreach ($arrErrors as $error) {
             $strError .= "<li style='list-style-type:circle;'><em>$error</em></li>";
         }
         $strError .= '</ul></div><br />';
        } 
    }// NOT BEEN SUBMITTED
    // show regular page with form
    ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <h2>
    <?php the_title(); ?>
    </h2>
    <?php 
    // show errors if there is any
    echo $strError; 
    ?>
    <?php 
    // show thank you if successful
    echo $strGood; 
    ?>
    <?php the_content('<p>Read the rest of this page &raquo;</p>'); ?>
    <form method="post" action="<?php bloginfo('url');?>/your-form-page/" enctype="multipart/form-data">
    <input type="hidden" value="send" name="action">
    <p <?php if (!empty($arrErrors['name'])) echo ' class="formerror"'; ?>>Your Name:
    <span class="errortext" style="color:#F00;">(required)</span><br>
    The rest of the form below here ------ >
    </form>
    <?php endwhile; endif; ?>
    <?php get_footer(); ?>
    

    也就是说,一旦用户填写了表单(我的表单有比这个多得多的字段,另外还有一个上传字段,用于附加文件)。

    但提交的表单会检查所需字段,如果成功,它将从$strcontent变量创建一个pdf文件,然后将此附加到要使用来自wordpress的wp_邮件发送的电子邮件。然后显示感谢信息,否则将显示并突出显示任何错误,

    希望这有帮助…

        2
  •  1
  •   kevtrout    14 年前

    我找到了这些插件,可以通过电子邮件发送或下载pdf格式的帖子。

    http://wordpress.org/extend/plugins/tags/create-pdf

    如果您是灵活的,似乎可以从用户提交的表单中以编程方式创建帖子,然后创建该帖子的pdf。从表单创建的帖子可以很容易地分配到一个特定的类别,该类别不会显示在网站上。

    要以编程方式创建、更新和删除帖子,请参阅wordpress函数引用,特别是:

    wp_insert_post wp update post wp delete post

    快速 google search 展示了许多使用php创建pdf的方法。有些难,有些不那么难。 I found this class that might get you started: "FPDF"

        3
  •  1
  •   zeta    9 年前

    有一个插件扩展名为gravity pdf,它从gravity表单扩展而来。它将从表单生成pdf,您可以选择下载或通过电子邮件发送。

    来源 https://wordpress.org/plugins/gravity-forms-pdf-extended/

        4
  •  0
  •   Brian Driscoll    14 年前

    我还没有对wp插件做过详尽的搜索,但从我所知答案是否定的。当然,可以从头开始创建这样一个插件,但是承载wp的服务器需要安装适当的库,以便插件有用。

        5
  •  0
  •   Domain    10 年前

    我们为我们的一个客户创建了一个自定义解决方案来完成这项工作,因为没有任何现成的可用插件。这个系统通过从重力表单中获取数据来创建pdf/word文档。

    您可以在这里查看解决方案的详细信息。 Gravity Forms to PDF/Word Document Auto-Fill Soluion

    推荐文章