【问题标题】:Exporting form results from Contact form 7 to PDF (fPDF)将表格结果从联系表格 7 导出为 PDF (fPDF)
【发布时间】:2015-01-21 13:01:32
【问题描述】:

我正在尝试通过 fpdf 将用户输入到 WordPress 联系表 7 中的值导出为 PDF。 这是我设置的,我可以生成 PDF 但没有表单中动态生成的值。

functions.php

add_action( 'wpcf7_before_send_mail', 'save_application_form');
function save_application_form($cf7) {

/* GET EXTERNAL CLASSES */
require(TEMPLATEPATH.'/fpdf/fpdf.php');

$values = $cf7->posted_data;
echo $values['first-name'];


/* example code to generate the pdf */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','B',16);
$pdf->Write(5,'first-name');
$pdf->SetFont('Arial','B',16);


$pdf->Output(TEMPLATEPATH.'/fpdf/pdf.pdf', 'F');

/* add  the pdf as attach to the email*/
$cf7->uploaded_files = array ( 'attachedfile' =>  TEMPLATEPATH.'/fpdf/pdf.pdf' );

如何从联系表格 7 中提取内容? 现在,如果我按发送,我只会得到一个上面写有“名字”的 PDF。我尝试了多种组合,没有任何效果。

感谢您的帮助。

编辑:我已经弄清楚如何打印,但问题似乎是,我没有从联系表 7 中提取插入的内容。

$first_name = $cf7->posted_data["first-name"];
$var = "test"; 


/* example code to generate the pdf */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','B',16);
$pdf->Write(5,  "My car is " . $var . "bl");
$pdf->SetFont('Arial','B',16);

所以 $first_name 不起作用,因为它是空的,有什么想法可以纠正这个问题吗?因为如果我尝试使用 $var 它会起作用。

【问题讨论】:

  • 所有Write 都是名字,所以这就是你所得到的。
  • 但是如果我插入 $value 我什么都没有显示,你能帮我解决这个问题吗?
  • 我已经尝试了另外两种变体。第一次尝试(页面甚至没有加载)$first_name = $cf7->posted_data["first-name"]; /* example code to generate the pdf */ $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Times','B',16); $pdf->Write(echo $first_name); $pdf->SetFont('Arial','B',16); 第二次尝试,没有任何东西打印到 pdf $first_name = $cf7->posted_data["first-name"]; /* example code to generate the pdf */ $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Times','B',16); $pdf->Write($first_name); $pdf->SetFont('Arial','B',16);

标签: php wordpress forms pdf fpdf


【解决方案1】:

您需要从 POST 数据中获取 $first_name。这应该有效:

$first_name = $_POST["first-name"];

/* example code to generate the pdf */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','B',16);
$pdf->Write(5,  "My car is " . $first_name . "bl");
$pdf->SetFont('Arial','B',16);

【讨论】:

    【解决方案2】:

    从 Contact From 7 的 3.9 版开始,您可以使用以下命令检索发布的数据,而不是使用 $cf7->posted_data:

    $submission = WPCF7_Submission::get_instance();
    
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }
    

    现在您有了一个包含已发布数据的数组,您可以使用它来生成 PDF 文件:

    /* example code to generate the pdf */
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Times','B',16);
    $pdf->Write(5,  "My first name is: " . $posted_data['first-name'] );
    $pdf->SetFont('Arial','B',16);
    

    【讨论】:

      【解决方案3】:

      我需要完成同样的事情,最后将 Contact Form 7 结果转换为 PDF。我最终使用了一些论坛中提到的建议组合,包括这个。

      您应该能够根据自己的目的进行调整。

      add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
      function wpcf7_update_email_body($contact_form) {
      
      $submission = WPCF7_Submission::get_instance();
      if ( $submission ) {
      /* DEFINE CONSTANT AND GET FPDF CLASSES */
      define ('FPDF_PATH',get_template_directory().'/fpdf/'); // MAKE SURE THIS POINTS TO THE DIRECTORY IN YOUR THEME FOLDER THAT HAS FPDF.PHP 
      require(FPDF_PATH.'fpdf.php');
      
      $posted_data = $submission->get_posted_data();
      // SAVE FORM FIELD DATA AS VARIABLES 
      $name = $posted_data["your-name"];
      $email = $posted_data["your-email"];
      $subject = $posted_data["your-subject"];
      $message = $posted_data["your-message"];
      
      $pdf = new FPDF();
      $pdf->AddPage();
      $pdf->SetFont('Arial','B',16);
      $pdf->Write(5,$name . "\n\n" . $email . "\n\n" . $subject . "\n\n" . $message);
      $pdf->Output(FPDF_PATH.'test.pdf', 'F'); // OUTPUT THE NEW PDF INTO THE SAME DIRECTORY DEFINED ABOVE
      
      }
      }
      
      add_filter( 'wpcf7_mail_components', 'mycustom_wpcf7_mail_components' );
      function mycustom_wpcf7_mail_components($components){
      if (empty($components['attachments'])) {
      $components['attachments'] = array(FPDF_PATH .'test.pdf'); // ATTACH THE NEW PDF THAT WAS SAVED ABOVE
      }
      return $components;
      }
      

      【讨论】:

        【解决方案4】:

        上面的 Kory 解决方案非常有效。但是,它不适用于单选按钮。所有单选按钮仅在最终 PDF 中显示为“数组”。如何正确显示单选按钮选项?我正在使用的代码如下。谢谢!

        add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
        function wpcf7_update_email_body($contact_form) {
        
        $submission = WPCF7_Submission::get_instance();
        if ( $submission ) {
        /* DEFINE CONSTANT AND GET FPDF CLASSES */
        define ('FPDF_PATH',get_stylesheet_directory().'/fpdf17/'); // MAKE SURE THIS POINTS TO THE DIRECTORY IN YOUR THEME FOLDER THAT HAS FPDF.PHP
        require(FPDF_PATH.'fpdf.php');
        
        $posted_data = $submission->get_posted_data();
        // SAVE FORM FIELD DATA AS VARIABLES
        $name = $posted_data["your-name"];
        $name2 = $posted_data["your-name2"];
        $email = $posted_data["your-email"];
        $enhetsnr = $posted_data["number-363"];
        $radio220 = $posted_data["radio-220"];
        $radio221 = $posted_data["radio-221"];
        $radio222 = $posted_data["radio-222"];
        $radio223 = $posted_data["radio-223"];
        $radio224 = $posted_data["radio-224"];
        $radio225 = $posted_data["radio-225"];
        
        $pdf = new FPDF('P','mm','A4');
        $pdf->AddPage();
        $pdf->SetFont('Times','',16);
        $pdf->Write(5, $name . "\n\n" . $name2 . "\n\n" . $email . "\n\n" . $enhetsnr . "\n\n" . $radio220 . "\n\n" . $radio221 . "\n\n" . $radio222 . "\n\n" . $radio223 . "\n\n" . $radio224 . "\n\n" . $radio225);
        $pdf->Output(FPDF_PATH.'tillval.pdf', 'F'); // OUTPUT THE NEW PDF INTO THE SAME DIRECTORY DEFINED ABOVE
        
        }
        }
        
        add_filter( 'wpcf7_mail_components', 'mycustom_wpcf7_mail_components' );
        function mycustom_wpcf7_mail_components($components){
        if (empty($components['attachments'])) {
        $components['attachments'] = array(FPDF_PATH .'tillval.pdf'); // ATTACH THE NEW PDF THAT WAS SAVED ABOVE
        }
        return $components;
        }
        

        【讨论】:

          【解决方案5】:

          不要忘记使用子主题,这样在使主题保持最新时,functions.php 中的额外代码不会消失。话虽如此,我在上面没有任何问题(感谢 Kory)。

          为了将 /fpdf/ 文件夹保留在子主题中,有一个新的 WP 命令:get_theme_file_path(),Kory 的代码使用它。

          https://wordpress.stackexchange.com/questions/192773/override-get-template-directory-in-child-theme

          【讨论】:

            猜你喜欢
            • 2015-09-22
            • 1970-01-01
            • 2017-09-30
            • 2020-06-13
            • 2020-12-04
            • 1970-01-01
            • 2020-05-30
            • 2017-03-16
            • 1970-01-01
            相关资源
            最近更新 更多