我正在尝试导出的值,用户输入到联系人表单7在WordPress中,通过PDF格式。这是我设置的,我可以生成PDF,但是没有从表单中动态生成的值。
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' );如何从Contact form 7中提取内容?现在,如果我按下“发送”,我只能得到一个写有“名字”的PDF。我试过多种组合,都不管用。
谢谢你的帮助。
编辑:我已经知道如何打印,但问题似乎是,我没有从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,它会起作用。
发布于 2015-10-23 00:44:42
上面由Kory提出的解决方案效果很好。但是,它不能与单选按钮一起工作。所有的单选按钮只在最终的PDF中显示为"Array“。如何正确显示单选按钮选项?我使用的代码如下所示。谢谢!
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;
}发布于 2014-11-25 22:27:51
您需要从POST数据中获取$first_name。这应该是可行的:
$first_name = $_POST["first-name"];
/* example code to generate the pdf */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','B',16);
$pdf->Write(5, "My car is " . $first_name . "bl");
$pdf->SetFont('Arial','B',16);发布于 2014-11-27 03:41:15
从7的Contact 3.9版本开始,不使用$cf7-> posted _ data,您可以使用以下命令检索已发布的数据:
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}现在,您有了一个包含已发布数据的数组,您可以使用它来生成PDF文件:
/* example code to generate the pdf */
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','B',16);
$pdf->Write(5, "My first name is: " . $posted_data['first-name'] );
$pdf->SetFont('Arial','B',16);https://stackoverflow.com/questions/27094291
复制相似问题