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

12,514

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;
}
Share:
12,514
Svedr
Author by

Svedr

Updated on June 27, 2022

Comments

  • Svedr
    Svedr almost 2 years

    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 dynamically generated value from the form.

    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' );
    

    How can I pull the content from Contact form 7? Now if I press send I only get a PDF with "first name" written on it. I've tried multiple combinations, nothing works.

    Thank you for your help.

    EDIT: I have figured out how to print, but it seems like the problem is, that I am not pulling the inserted content from Contact Form 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);
    

    So $first_name doesn't work because it is empty, any ideas how i can correct this? Because if i try with $var it works.