在WordPress中提交到PDF

时间:2010-11-10 17:51:11

标签: wordpress wordpress-plugin

当点击提交按钮时,是否有插件或某些内容根据用户表单数据输入创建PDF文件?

5 个答案:

答案 0 :(得分:5)

如果您想要填写表格,那么该表格的详细信息会通过电子邮件处理并发送给您,并附上详细信息的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_mail发送的电子邮件..然后显示一条感谢信息,否则它会显示并突出显示任何错误,

希望这会有所帮助..

答案 1 :(得分:1)

我发现这些插件允许通过电子邮件发送或下载帖子为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"

答案 2 :(得分:1)

有一个名为Gravity PDF的插件扩展,它扩展自Gravity Forms。它将从表单生成PDF,您可以选择下载或通过电子邮件发送。

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

答案 3 :(得分:0)

我还没有详尽地搜索WP插件,但据我所知,答案是否定的。当然,可以从头开始创建这样的插件,但托管WP的服务器需要安装适当的库才能使插件有用。

答案 4 :(得分:0)

我们为其中一位客户创建了一个自定义解决方案,因为没有现成的插件。该系统通过从Gravity Forms中提取数据来创建pdf / word文档。

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