Blog

  • Creating PDF Using FPDF From Contact Form 7 Submission Data

    Posted at 10 Jul, 2018 in Tutorials

    We know that Contact Form 7 (CF7) Plugin is one of the most popular and widely used Plugins to build contact forms in WordPress. But it lacks few essential features like submission data storage (even though we can use some other third party Plugins to save the submission data). Today, we are going to learn how to create a PDF containing the submission data and send it as attachment.

    Step 1:

    Build the contact form with required fields using Contact Form 7 Plugin.

    Step 2:

    In the File Attachments field (Mail tab), add a new tag say [submission_pdf] which we can use to attach the submission PDF file we are going to generate dynamically.

    Step 3:

    Download FPDF package from http://www.fpdf.org/ and paste the files inside your theme (say in ‘fpdf’ folder).

    Step 4:

    Now we came to the important part – PDF generation. We need to create a file for generating the PDF. Copy the following contents and save the file as ‘tcpdf_cf7.php’ inside the new ‘fpdf’ folder.


    <?php
    class CREATE_FPDFCF7
    {
    function CREATE_FPDFCF7Fn($name,$address,$city,$postcode,$phone,$savepath)
    {
    // include the main TCPDF library
    require_once('config/tcpdf_config_alt.php');
    require_once('tcpdf.php');
    // create new pdf document
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Admin');
    $pdf->SetTitle('Contact Form 7 Submission');
    $pdf->SetSubject('Contact Form 7 Submission');
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->SetHeaderMargin(0);
    // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    // set some language-dependent strings (optional)
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
    }
    // set default font subsetting mode
    $pdf->setFontSubsetting(true);
    // add a page
    $pdf->AddPage();
    //content to print
    $html .='<h3 style="font-size:12px;text-align:center;">Contact Form 7 Submission</h3>';
    $html .='<div style="font-family:arial;font-weight:normal;font-size:8px;">Name :   '.$name.'</div>';
    $html .='<div style="font-family:arial;font-weight:normal;font-size:8px;">Address :   '.$address.'</div>';
    $html .='<div style="font-family:arial;font-weight:normal;font-size:8px;">City :    '.$city.'</div>';
    $html .='<div style="font-family:arial;font-weight:normal;font-size:8px;">Postcode :   '.$postcode.'</div>';
    $html .='<div style="font-family:arial;font-weight:normal;font-size:8px;">Phone Number :   '.$phone.'</div>';
    // print text
    $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
    $filename =rand().'_'.time().'.pdf';
    $pdf->Output($savepath.$filename,'F');
    return $filename;
    }
    }

    Feel free to adjust the field names, title and subject as needed.

    Step 5:

    Now we need to use Contact Form 7 hook to capture the submission data and generate the PDF file. Paste the following code in your theme’s ‘functions.php’.

    add_action( 'wpcf7_before_send_mail', 'create_contact_form_pdf' );
    function create_contact_form_pdf($cf7)
    {
    //check if this is the right form - replace your contact form 7 id here//
    if ($cf7->id==10)
    {
    //include pdf generation file//
    require_once('tcpdf/tcpdf_cf7.php');
    $submission = WPCF7_Submission::get_instance();
    if ($submission)
    {
    $posted_data = $submission->get_posted_data();
    $name = $posted_data['first_name']." ".$posted_data['last_name'];
    $address = $posted_data['address'];
    $city = $posted_data['city'];
    $postcode = $posted_data['postcode'];
    $phone = $posted_data['phone'];
    // create new PDF document
    $createpdf = new CREATE_FPDFCF7();
    //upload path
    $uploads = wp_upload_dir();
    define ('PDF_FILE_PATH',$uploads['basedir'].'/cf7_pdf/');
    $fname = $createpdf->CREATE_FPDFCF7Fn($name,$address,$city,$postcode,$phone,PDF_FILE_PATH);
    //set filenames
    $pdf_filename= PDF_FILE_PATH.$fname;
    //use the same tag used in contact form 7 mail tab
    $submission->add_uploaded_file('submission_pdf', $pdf_filename);
    }
    }
    }

    You need to just replace the Contact Form 7 ID and field names.

    Step 6:

    Fill and submit the form and you will receive an email with a PDF attachment containing submission data. If it doesn’t work and the Contact Form 7 loader keeps spinning, you probably made some errors in the field names – so double check it. Also the PDF will gets automatically deleted once email sent and if you need a copy of the PDF to store in the server, just copy the PDF file with another name and use that filename in the $submission->add_uploaded_file('submission_pdf', $pdf_filename); function.

    That’s it and this is a basic idea for PDF generation and you can use other PDF packages like mPDF, HTML 2 PDF, etc. or even as a seperate Plugin.

Comments are closed.

Categories

Categories

Archives

Archives