将表格结果从联系表格7导出为PDF(fPDF)

时间:2014-11-23 21:05:11

标签: php wordpress forms pdf fpdf

我正在尝试将用户输入的值输出到WordPress中的Contact form 7,通过fpdf导出为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' );

如何从联系表格7中提取内容? 现在,如果我按发送,我只会得到一个带有"名字"写在上面。我尝试了多种组合,没有任何效果。

感谢您的帮助。

编辑:我已经想出了如何打印,但似乎问题是,我没有从联系表格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就行了。

5 个答案:

答案 0 :(得分:2)

Kory的上述解决方案完美无缺。但是,它不适用于单选按钮。所有单选按钮仅在最终PDF上显示为“数组”。如何正确显示单选按钮选项?我正在使用的代码如下。谢谢!

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

答案 1 :(得分:0)

您需要从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);

答案 2 :(得分:0)

从Contact From 7版本3.9开始,您可以使用以下内容检索发布的数据,而不是使用$ cf7-> published_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);

答案 3 :(得分:0)

我需要完成同样的事情,最后将Contact Form 7的结果转换为PDF。我最终使用了一些论坛中提到的建议组合,其中包括一个。

您应该能够根据自己的目的进行调整。

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_template_directory().'/fpdf/'); // 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('Arial','B',16);
$pdf->Write(5,$name . "\n\n" . $email . "\n\n" . $subject . "\n\n" . $message);
$pdf->Output(FPDF_PATH.'test.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 .'test.pdf'); // ATTACH THE NEW PDF THAT WAS SAVED ABOVE
}
return $components;
}

答案 4 :(得分:0)

别忘了使用Child-Theme,因此在使主题保持最新状态时,functions.php中的额外代码不会消失。话虽如此,但我没有上述任何问题(归功于Kory)。

为了将/ fpdf /文件夹保留在子主题中,有一个新的WP命令:get_theme_file_path(),Kory的代码使用了该命令。

https://wordpress.stackexchange.com/questions/192773/override-get-template-directory-in-child-theme