0

I am using a script to create PDF files. After creating the PDF file I want to redirect the user to anonther page.

When I run the script I only get the PDF file. The script is not redirecting me to another page.

Does someone know how I can redirect users to another page?

Here is my script:

    header("Content-type:application/pdf");
    header("Content-Disposition:attachment;filename=test.pdf");

    readfile('./temp/pdf/test.pdf');

    header("Location: http://www.example.com");
    die();

I have also tried to redirect with the meta refresh tag but the script is still not redirecting:

<meta http-equiv="refresh" content="1;url=http://www.example.com">
John
  • 904
  • 8
  • 22
  • 56
  • 1
    You can't send another header(), after data has been sent to the browser, use a meta refresh tag! – Stephanie Temple Mar 03 '19 at 03:06
  • Hi John, you can't send a new header after outputting the pdf - check this question for an answer: https://stackoverflow.com/questions/4304153/is-it-possible-to-redirect-a-page-after-a-readfile – Chad Cardiff Mar 03 '19 at 03:09
  • I have tried to redirect with: `` but the script is still not redirecting. – John Mar 03 '19 at 03:13
  • You can't set `Location:` (which causes a redirect when used with a `300-399` status code)) when setting content. That's not how HTTP works. – Dai Mar 03 '19 at 03:29
  • 2
    Doing `readfile` and then doing a redirect makes no sense. A better approach is to use a `iframe` that is hidden and a form button to target it for the download link. etc. Then you can do a download without any kind of page redirection. – ArtisticPhoenix Mar 03 '19 at 03:49
  • I always need the page redirection. The proces starts with a button click (download). After click I am updating the database. Creating the file and redirecting to the first page. – John Mar 03 '19 at 12:57
  • Any more suggestions? – John Mar 10 '19 at 12:13

1 Answers1

1

After creating the PDF file I want to redirect the user to anonther page.

This is not possible with HTTP.

Instead, you should create the file, redirect the user, and then the web-page target of the redirection serves up the PDF response through a JavaScript redirection (PDFs generally cannot run JavaScript that interacts with the browser)

Dai
  • 141,631
  • 28
  • 261
  • 374