In a PHP project I need to Create a PDF file and redirect to another page when user clicks a Submit button.
I have managed to create the pdf file using DOMPDF. PDF creation is done in a seperate file ('PDFRecipt.php')
.
I have called that page when a user clicks a button on the main page. This is how call PDF page
header('location:PDFRecipt.php');
but the problem is when I try to redirect after calling PDF page by
header('location:Other.php');
It does not create the PDF (only redirects). I tried changing
header('location:PDFRecipt.php');
to
include_once('PDFRecipt.php');
then it does not create the PDF correctly (Corrupted PDF File)
How to create the PDF file & redirect to other page?
EDIT:
Code in PDFRecipt.php
$html='SOME HTML';
include("../../dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream('FileName.pdf');
//header('location:Other.php);
Answer to question
For this to work you would need to move the second
header
call into thePDFRecipt
file. At the moment with both of them in the one file your second call toheader
is overriding the first.Remember that headers are sent when the output is sent to the users browser, which is why you often see people calling
exit()
right after aheader('Location: http://example.org');
.So any subsequent calls to set the same header, in this case
Location
, will override the first until the headers are sent.It is also worth pointing out that you should be using full web URLs in
Location
:according to the
header
page in the manual: http://php.net/manual/en/function.header.phpUpdate from comments
So you are using the
stream()
method to send the client the PDF - you cannot combine this with aLocation:
header. This is because DOMPDF has already flushed content to screen. I had assumed that yourPDFRecipt.php
file was storing the PDF to disk somewhere.See DOMPDF source code for more details: http://code.google.com/p/dompdf/source/browse/trunk/dompdf/lib/class.pdf.php#3061