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


Exporting form results from Contact form 7 to PDF (fPDF)

我正在尝试通过fpdf将用户输入到WordPress中的联系人表单7中的值导出为PDF。这是我设置的,我可以生成一个PDF,但没有从表单动态生成的值。

函数.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。我尝试了多种组合,没有任何效果。

谢谢你的帮助。

编辑:我已经弄清楚了如何打印,但问题似乎是,我没有从联系表格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它会起作用。

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

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

从 Contact From 7 的 3.9 版开始,您可以使用以下命令检索已发布的数据,而不是使用 $cf 7->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);

我需要完成同样的事情,最终将联系表格 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;
}

不要忘记使用子主题,这样您在函数中的额外代码.php在保持主题最新时不会消失。 话虽如此,我上面没有任何问题(归功于科里)。

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

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