Solved

open pdf within browser via php code

Posted on 2013-06-25
9
2,282 Views
Last Modified: 2013-06-25
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

However
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));
      readfile($file);

option 2
       header("Content-type: application/pdf");
       header("Content-Disposition: inline; filename=".$file);
       readfile($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

thanks
0
Comment
Question by:joomla
  • 4
  • 4
9 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39276491
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...?
0
 

Author Comment

by:joomla
ID: 39276509
Is there a way to make it version irrelevant ?
0
 
LVL 34

Expert Comment

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

if(!file_exists($file))
{
  echo "{$file} does not exist!";
  die();
}
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 58

Expert Comment

by:Gary
ID: 39276520
What browser are you using? What version of PDF reader?
Assuming gr8gonzo is wrong and you do have the correct file path....
0
 

Author Comment

by:joomla
ID: 39276535
Hi
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

regards
0
 
LVL 58

Expert Comment

by:Gary
ID: 39276558
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
0
 

Author Comment

by:joomla
ID: 39276633
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));
      readfile($file);
0
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 39276645
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
0
 

Author Closing Comment

by:joomla
ID: 39276648
thanks for your help
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question