将表格结果从联系表格7导出到PDF(fPDF) [英] Exporting form results from Contact form 7 to PDF (fPDF)

查看:71
本文介绍了将表格结果从联系表格7导出到PDF(fPDF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户在WordPress的联系表单7中输入的值通过fpdf导出为PDF. 这就是我设置的内容,我可以生成PDF,但是无需从表单中动态生成值.

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

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

如何从联系表7中提取内容? 现在,如果按发送,则只会得到上面写有名字"的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.

谢谢您的帮助.

我已经弄清楚了如何打印,但是似乎问题是我没有从联系表7中提取插入的内容.

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

因此$ first_name无效,因为它为空,有什么主意我可以纠正这个问题吗?因为如果我尝试使用$ var,它会起作用.

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.

推荐答案

Kory的上述解决方案效果很好.但是,它不适用于单选按钮.所有单选按钮在最终PDF上仅显示为数组".如何正确显示单选按钮的选择?我正在使用的代码如下.谢谢!

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

这篇关于将表格结果从联系表格7导出到PDF(fPDF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆