How do i open pdf in browser instead of download in php?

Is there a way to force PDF files to open in the browser when the option "Display PDF in browser" is unchecked?

I tried using the embed tag and an iframe, but it only works when that option is checked.

What can I do?

Here filename is optional, but if provided, it will take this name for the downloaded file.

EDIT

I know this is the opposite of what the question asked. I am keeping the opposite answer for those [like me] who came searching for the opposite question [Evidence: this answer has more upvotes then downvotes]

answered Oct 1, 2014 at 10:00

AkshayAkshay

3,3023 gold badges38 silver badges73 bronze badges

7

I had the same issue and most of the above answers should resolve your issue. Unfortunately, even if i was receiving the content-type & content-disposition headers in the response but still my pdf was being downloaded rather than viewed. After brainstorming and trying for many hours.

The Culprit was firefox, well in a way it was me. Nervous Laughter

By default, when you open a pdf file in firefox, it will provide you with a popup to either save the pdf file or to open it directly and there is also a check box which says do this action automatically from now on and guess who selected it.

Due to this mistake, my pdf was being downloaded rather than viewed, even if had all the required headers in response. This is a simple mistake but cost me a good amount of time.

To resolve this, just go to settings and search for applications and change pdf setting to whatever you need.

answered Jun 7, 2021 at 5:15

Ashfaq nisarAshfaq nisar

2,16910 silver badges19 bronze badges

1

This is for ASP.NET MVC

In your cshtml page:

@Model.Name

Your browser does not support viewing PDFs, click on the link above to download the document.

In your controller:

public ActionResult Download[Guid id]
    {
        if [id == Guid.Empty]
            return null;

        var model = GetModel[id];

        return File[model.FilePath, "application/pdf", model.FileName];
    }

public FileStreamResult View[Guid id]
    {
        if [id == Guid.Empty]
            return null;

        var model = GetModel[id];

        FileStream fs = new FileStream[model.FilePath, FileMode.Open, FileAccess.Read];

        return File[fs, "application/pdf"];
    }

answered Nov 29, 2016 at 11:05

Open PDF

If the PDF file is inside some folder and that folder doesn't have permission to access files in that folder directly then you have to bypass some file access restrictions using .htaccess file setting by this way:


    Order Allow,Deny
    Allow from all

But now allow just certain necessary files.

I have used this code and it worked perfectly.

answered Nov 9, 2012 at 10:33

1

Open downloads.php from rootfile.

Then go to line 186 and change it to the following:

        if[preg_match["/\.jpg|\.gif|\.png|\.jpeg/i", $name]]{
            $mime = getimagesize[$download_location];
            if[!empty[$mime]] {
                header["Content-Type: {$mime['mime']}"];
            }
        }
        elseif[preg_match["/\.pdf/i", $name]]{
            header["Content-Type: application/force-download"];
            header["Content-type: application/pdf"];
            header["Content-Disposition: inline; filename=\"".$name."\";"];
        }

        else{
            header["Content-Type: application/force-download"];
            header["Content-type: application/octet-stream"];
            header["Content-Disposition: attachment; filename=\"".$name."\";"];
        }

Cleb

23.5k18 gold badges105 silver badges143 bronze badges

answered Nov 12, 2014 at 14:29

2

Here is another method of forcing a file to view in the browser in PHP:

$extension = pathinfo[$file_name, PATHINFO_EXTENSION];
$url = 'uploads/'.$file_name;
        echo ''
                .header['Content-Type: application/'.$extension].'
' .header['Content-Disposition: inline; filename="'.$file_name.'"'].'
' .'' .' ' .'' . '';

answered Feb 25, 2015 at 11:15

user28864user28864

3,2931 gold badge24 silver badges19 bronze badges

1

If you link to a .PDF it will open in the browser.
If the box is unchecked it should link to a .zip to force the download.

If a .zip is not an option, then use headers in PHP to force the download

header['Content-Type: application/force-download']; 
header['Content-Description: File Transfer']; 

answered Jun 9, 2011 at 13:56

Kirk StrobeckKirk Strobeck

17.3k18 gold badges74 silver badges110 bronze badges

3

How do I force a PDF to open in browser instead of download?

At the bottom, click Show advanced settings. Under “Privacy”, click Content settings. Under “PDF Documents," check the box next to "Open PDF files in the default PDF viewer application.” [Uncheck this box if you want PDFs to open automatically when you click them.] Show activity on this post.

How do I make a file open in browser instead of downloading?

To make certain file types OPEN on your computer, instead of Chrome Downloading... You have to download the file type once, then right after that download, look at the status bar at the bottom of the browser. Click the arrow next to that file and choose "always open files of this type". DONE.

How do I open PDF directly in browser?

Opening a PDF document in Chrome is simple, whether you use Mac or Windows..
Right click on your PDF document..
Click on 'Open with'..
Select Chrome or your browser of choice. You can also open PDFs in any other browser you use regularly..

How do I get PDF to open in browser HTML?

Click "Enable" to set Chrome PDF Viewer as the default PDF viewer on Google Chrome. Step 3: Right click on your document. Navigate to the "Open With" option and choose "Chrome PDF Viewer" from the drop-down menu. You can also drag a PDF document directly into the browser, and it will open.

Chủ Đề