# Outputting a PDF for download after form submission

**URL:** https://community.gravityforms.com/t/outputting-a-pdf-for-download-after-form-submission/13525
**Category:** Get Help
**Tags:** custom, gform\_after\_submission, pdf
**Created:** [September 30, 2022, 1:33pm UTC](https://community.gravityforms.com/t/outputting-a-pdf-for-download-after-form-submission/13525 "2022-09-30T13:33:18Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![rivkadr](https://avatars.discourse-cdn.com/v4/letter/r/a9adbd/32.png) [@rivkadr](https://community.gravityforms.com/u/rivkadr)
#### Post date: [September 30, 2022, 1:33pm UTC](https://community.gravityforms.com/t/outputting-a-pdf-for-download-after-form-submission/13525/1 "2022-09-30T13:33:18Z")

</div>

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:

```auto
$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?

---

<div class="post-metadata">

### Author: ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)
#### Post date: [October 3, 2022, 4:55am UTC](https://community.gravityforms.com/t/outputting-a-pdf-for-download-after-form-submission/13525/2 "2022-10-03T04:55:20Z")

</div>

`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?)

> **[Gravity PDF](https://wordpress.org/plugins/gravity-forms-pdf-extended/)**
>
> Automatically generate, email and download PDF documents with Gravity Forms and Gravity PDF.

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:

> **[Gravity Forms Hook Reference | Complete Guide to Hooks | Gravity Wiz](https://gravitywiz.com/gravity-forms-hook-reference/)**
>
> The complete reference guide for Gravity Forms hooks. Includes a list of hook names and how each is used in typical scenarios.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/fef63f0599c45e0218c30d9f5d7ca8bff9b00189.png) [@system](https://community.gravityforms.com/u/system)
#### Post date: [November 2, 2022, 4:55am UTC](https://community.gravityforms.com/t/outputting-a-pdf-for-download-after-form-submission/13525/3 "2022-11-02T04:55:50Z")

</div>

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