Outputting a PDF for download after form submission

I’m using the gform_after_submission() action hook to generate a PDF based on some of the form data, and then making it available to the user to download, using the following code:

$file = 'return_label.pdf';
file_put_contents($file, $pdf_data);

if (file_exists($file)) {
	header('Content-Description: File Transfer');
	header('Content-Type: application/octet-stream');
	header('Content-Disposition: attachment; filename="'.basename($file).'"');
	header('Expires: 0');
	header('Cache-Control: must-revalidate');
	header('Pragma: public');
	header('Content-Length: ' . filesize($file));
	readfile($file);
}

The problem with rewriting the headers is that the form never fully completes, as in, the form fields stay on the page and the user doesn’t get the confirmation text - so not a great UX. Is there some other function/action that runs after the form fully submits that I can tap into? Or other ideas on how to handle this?

gform_after_submission is too late. That happens after notifications and confirmation are displayed. You’re no longer tied to the user at that point.

Have you considered one of the free Gravity Forms to PDF solutions, rather than custom code (such as this one?)

If you would rather do this with your own code, please take a look at the Gravity Wiz hook guide to find a more appropriate hook or filter to use to generate and make the PDF available to your user:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.