Skip Navigation

[Resolved] cred_save_data hook not executing pdf

This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 3 years ago.

Assisted by: Waqar.

Author
Posts
#2207067
Untitled.png

Hi,

Im trying to set up a button that submits the edit form, prints it to PDF and attachs to an e-mail.
I know cred notifications doesnt allow attachments, so im trying to use the cred_save_data hook to do the following:

1 - Generate the pdf using the mpdf library of the current form and save it to a folder.
2 - E-mail the pdf as attachment using wp_mail function considering that it's not possible with cred

Problem is... when i press the submit form button nothing happens and i get a few messages in the console. (check attachment)

Here is the code:

[code]
function my_load_scripts() {
wp_enqueue_script ( 'mpdf', get_home_url().'/wp-content/themes/Avada-Child-Theme/vendor/autoload.php');
}
add_action('wp_enqueue_scripts', 'my_load_scripts');

add_action('cred_save_data', 'this_save_data_action',10,2);
function this_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==2552)
{
// this field exists in the form and it's not empty
if (isset($_POST['wpcf-nif']))
{
// with this block of code active, it returns the error that you can see in the screenshot when submitting the form
$mpdf = new \Mpdf\Mpdf(['tempDir' => get_home_url().'/wp-content/themes/Avada-Child-Theme/files/']);
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();
// This line was only added to see if returned any error when isolated, and apparently it doesnt return any error.
update_post_meta($post_id, 'wpcf-nif', $_POST['wpcf-nif'], true);
}
}

}
[/code]

My question is... does cred_save_data not allow to run scripts outside wordpress?

#2207617

Hi,

Thank you for contacting us and I'd be happy to assist.

I don't think using "wp_enqueue_script" would work for loading the PHP "mpdf" library. It works only for loading front-end scripts and styles.
( ref: https://developer.wordpress.org/reference/functions/wp_enqueue_script/ )

Have you tried using the "require_once" to load the "autoload.php" file, within the "this_save_data_action" function?
( ref: hidden link )

regards,
Waqar

#2207979

Hi Waqar,

thank you for your time.
It's not that really the issue, but i just changed to require_once and the error is the same.
I even tried with another script (FPDF) and the result produces the same error as the screenshot posted in the 1st post.

This is the current code of functions.php:

require_once __DIR__ . '/vendor/autoload.php';

add_action('cred_save_data', 'this_save_data_action',10,2);
function this_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2552)
    {
$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/files']);
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();
    }
 }

// If i place this code below and outside the cred_save_data hook , as soon as i refresh the page im presented with the pdf with the "hello world".

//$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/files']);
//$mpdf->WriteHTML('<h1>Hello world!</h1>');
//$mpdf->Output();

So.. it's the hook that doesn't execute the code, or executes it but with errors... how can we solve this issue?

Thanks

#2211531

Thanks for writing back.

I couldn't test with the "Mpdf" because it requires including files through "composer", but, I was able to make the "FPDF" library ( hidden link ) work.

I downloaded the "FPDF" library files and placed them in the "fpdf" folder in my active theme directory and then used this code:


add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
	// if a specific form
	if ($form_data['id']==139)
	{
		$theme_path = get_template_directory();
		require_once($theme_path.'/fpdf/fpdf.php');

		$pdf = new FPDF();
		$pdf->AddPage();
		$pdf->SetFont('Arial','B',16);
		$pdf->Cell(40,10,'Hello World!');

		$filename=$theme_path.'/fpdf/test.pdf';
		$pdf->Output($filename,'F');
	}
}

As a result, the "test.pdf" file was created in the "fpdf" folder in the theme's directory, when the form was submitted.

So this confirms that the "cred_save_data" hook itself has no problem with running the external PHP library code.

To troubleshoot the "Mpdf" libraries errors, you can either consult its author or general PHP community forums like Stack Overflow.