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.
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!
You will need get the $first_name from the POST data. This should work:
Since version 3.9 of Contact From 7, instead of using $cf7->posted_data, you can retrieve the posted data with:
Now you have an array with the posted data which you can use to generate the PDF file:
I needed to accomplish the same thing and finally got the Contact Form 7 results to be converted to a PDF. I ended up using a combination of suggestions mentioned in a few forums, this one included.
You should be able to adapt this to your own purposes.
Don’t forget to use a Child-Theme so your extra code in functions.php doesn’t disappear when keeping the theme up to date. Having said that I had no issues above (credit to Kory).
In order to keep the /fpdf/ folder in the child-theme there is a new WP command:
get_theme_file_path()
, which the code from Kory uses.https://wordpress.stackexchange.com/questions/192773/override-get-template-directory-in-child-theme