Correct PHP Headers For Pdf File Download

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



Leave a reply



Submit