简体   繁体   中英

PHP: Get newly generated pdf in “save as..” popup

<form  method="POST" action="createPDF.php">
<input type="text" id="myID" value="<?php echo $_SESSION['userSession']?>" style="display:none"/>
<input type="submit" value="showPDF"/>
</form>

I am sending myID variable to createPDF.php page and generating pdf file. I am using TCPDF library for pdf files. My createPDF.php code:

$myFile=$pdf->Output('mypdf001.pdf', 'I');
header('Content-Disposition: attachment; filename=' .$myFile);

I am getting save as popup with .php file or am being redirected to createPDF.php file. How to get save as pop up with .pdf file?

Depending on what your PHP configuration allows, you could try changing your form to

<form  method="POST" action="createPDF.php/myPDF001.pdf">

If your PHP configuration allows it, this will still run the createPDF.php file, but the browsers will be more likely to see it as a PDF file.

Also ensure that the content-type header is correctly set

header("Content-type:application/pdf");

Finally (although I'm not 100% sure if this makes a difference) set the headers before outputting the PDF

Have you set the content type before the content-disposition?

I think you are missing:

header('Content-Type: application/download');
header('Content-Type: application/pdf');

So you would have:

$myFile=$pdf->Output('mypdf001.pdf', 'I');  
header('Content-Type: application/download');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' .$myFile);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM