Contact Form 7 Results to PDF

Contact Form 7 Results to PDF

Create a pdf attachment of my CF7 form's post data.

Steps

  1. Download FPDF from www.fpdf.org/
  2. Unzip the download contents and move the fold via ftp to your website theme folder.
  3.  Insert the code below into functions.php

Thanks to WP Member korynorthrop

// ATTACH PDF TO EMAIL VIA FPDF
add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
function wpcf7_update_email_body($contact_form) {

$submission = WPCF7_Submission::get_instance();
if ( $submission ) 
$id = $contact_form->id();
if ($id==691) // THIS IS THE ID OF THE CONTACT FORM USED
{
/* DEFINE CONSTANT AND GET FPDF CLASSES */
define ('FPDF_PATH',get_template_directory().'/fpdf18/'); // 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('courier','B',16);
$pdf->Write(5,$name . "\n\n" . $email . "\n\n" . $subject . "\n\n" . $message);
$pdf->Output(FPDF_PATH.'message.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 .'message.pdf'); // ATTACH THE NEW PDF THAT WAS SAVED ABOVE
}
return $components;
}
// END FPDF CODE