Exporting form results from Contact form 7 to PDF (fPDF)

后端 未结 5 1717
误落风尘
误落风尘 2021-01-07 05:23

I am trying to export the values that users input into Contact form 7 in WordPress, to PDF via fpdf. This is what I\'ve set up, I can generate a PDF but without the dynamica

5条回答
  •  耶瑟儿~
    2021-01-07 06:16

    the solution above by Kory works perfectly. However, it doesn't work with radio buttons. All of the radio buttons are only displaying as "Array" on the final PDF. How do I display the radio button choices properly? The code I'm using is below. Thanks!

    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;
    }
    

提交回复
热议问题