open pdf within browser via php code

Using PHP code I have created a pdf document and stored it as a file on our server.

I can open the pdf file within the browser if I access it via it's url
I can email it as an attachment and successfully open

I can't open the pdf in the browser window via a php call.

I get the following error "File doesn't begin with '%PDF-'

I have tried the following options   ($file is a variable)

option 1      
        header('Content-Disposition: inline; filename="'.$file.'"');
      header('Content-Type: application/pdf');
      header('Content-Length: '.filesize($file));

option 2
       header("Content-type: application/pdf");
       header("Content-Disposition: inline; filename=".$file);

option 3
       header("Content-Type: application/pdf");
       header("Cache-Control: no-cache");
       header("Accept-Ranges: none");
       header("Content-Disposition: attachment; filename=\"".$file."\"");

I have also attempted to use absolute references rather than the variable '$file' but I haven't been successful

Who is Participating?
GaryConnect With a Mentor Commented:
Microsoft Security Essentials - been using it over a year and never an infection and its free (I know what you mean about Kaspersky - and McAfee)
Also AVG - has a free version.
Depends how much you want to spend or not spend.

Yep that code works fine - but I'm on PDF 10
Tested your first option and it works as it should.
Check you are not using an old version of pdf reader.
Assuming you have nothing else in the php page...?
joomlaAuthor Commented:
Is there a way to make it version irrelevant ?
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

I'm guessing that it's probably a missing file / path issue. To confirm that, add this before the first header() in option 1:

  echo "{$file} does not exist!";
What browser are you using? What version of PDF reader?
Assuming gr8gonzo is wrong and you do have the correct file path....
joomlaAuthor Commented:
thanks gr8gonzo, but the path is correct, however I appreciate the feedback.

application:  DOMPDF
version:   PDF 1.3   (acrobat 4.x)

I should point out, it opens if sent as an attachment and or if I click on the link

1.3 - are you sure?
That's 13 years old, and probably the reason. Earlier version of PDF reader had problems with IE's ActiveX
joomlaAuthor Commented:
sorry for the slow response
using adobe acrobat professional seems to give the same result

I tried downloading/installing latest version of reader but Kasparskey locked my machine and I had to restart

Can anyone recommend a better anti-virus as I'm running out of tolerance for K.

Can you confirm that the following code block works for you ?

      header('Content-Disposition: inline; filename="'.$file.'"');
      header('Content-Type: application/pdf');
      header('Content-Length: '.filesize($file));
joomlaAuthor Commented:
thanks for your help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.