correct PHP headers for pdf file download
Example 2 on w3schools shows what you are trying to achieve.
<?php
header("Content-type:application/pdf");
// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");
// The PDF source is in original.pdf
readfile("original.pdf");
?>
Also remember that,
It is important to notice that header() must be called before any
actual output is sent (In PHP 4 and later, you can use output
buffering to solve this problem)
PDF file is not view in correct format in browser using PHP Header function
That's an invalid content type, you should be using application/pdf
// change this
header('Content-type:application.pdf');
// to this
header('Content-Type: application/pdf');
Force file download with php using header()
The problem was that I used ajax to post the message to the server, when I used a direct link to download the file everything worked fine.
I used this other Stackoverflow Q&A material instead, it worked great for me:
- Ajax File Download using Jquery, PHP
Cannot open PDF file downloaded using php
Instead of
header('Content-Type: application/octet-stream; charset=utf-8');
Try this
header('Content-Type: application/pdf');
Also review this:
- How to make PDF file downloadable in HTML link?
- correct PHP headers for pdf file download
PHP - What are the correct headers for downloading file?
I found the root of the problem, I hav some extra spaces after php close tag. Thank you guys.
Related Topics
Pdo::Fetchall Vs. Pdo::Fetch in a Loop
How to Pass Variable Number of Arguments to a PHP Function
Generating (Pseudo)Random Alpha-numeric Strings
MySQL Check If a Table Exists Without Throwing an Exception
How to Change Public Folder to Public_Html in Laravel 5
How to Check Whether the User Uploaded a File in PHP
How to Replace Different Newline Styles in PHP the Smartest Way
How to Prevent Multiple Form Submission on Multiple Clicks in PHP
Codeigniter Displays a Blank Page Instead of Error Messages
Reliable User Browser Detection With PHP
Disable Warnings When Loading Non-well-formed HTML by Domdocument (PHP)
Create Programmatically a Woocommerce Product Variation With New Attribute Values
How to Prevent My Site Page to Be Loaded Via 3Rd Party Site Frame of Iframe