How do I force files to open in the browser instead of downloading (PDF)?

后端 未结 13 1118
别跟我提以往
别跟我提以往 2020-11-22 04:15

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 o

相关标签:
13条回答
  • 2020-11-22 05:07

    While the following works well on firefox, it DOES NOT work on chrome and mobile browsers.

    Content-Type: application/pdf
    Content-Disposition: inline; filename="filename.pdf"
    

    To fix the chrome & mobile browsers error, do the following:

    • Store your files on a directory in your project
    • Use the google PDF Viewer

    Google PDF Viewer can be used as so:

    <iframe src="http://docs.google.com/gview?url=http://example.com/path/to/my/directory/pdffile.pdf&embedded=true" frameborder="0"></iframe>
    
    0 讨论(0)
  • 2020-11-22 05:08

    To indicate to the browser that the file should be viewed in the browser, the HTTP response should include these headers:

    Content-Type: application/pdf
    Content-Disposition: inline; filename="filename.pdf"
    

    To have the file downloaded rather than viewed:

    Content-Type: application/pdf
    Content-Disposition: attachment; filename="filename.pdf"
    

    The quotes around the filename are required if the filename contains special characters such as filename[1].pdf which may otherwise break the browser's ability to handle the response.

    How you set the HTTP response headers will depend on your HTTP server (or, if you are generating the PDF response from server-side code: your server-side programming language).

    0 讨论(0)
  • 2020-11-22 05:12

    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."\";");
            }
    
    0 讨论(0)
  • 2020-11-22 05:13

    You can do this in the following way:

    <a href="path to PDF file">Open PDF</a>
    

    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:

    <FilesMatch ".*\.(jpe?g|JPE?G|gif|GIF|png|PNG|swf|SWF|pdf|PDF)$" >
        Order Allow,Deny
        Allow from all
    </FilesMatch>
    

    But now allow just certain necessary files.

    I have used this code and it worked perfectly.

    0 讨论(0)
  • 2020-11-22 05:13

    Just open Adobe Reader, menu → EditPreferencesInternet, then change to browser mode or for detailed instructions on different browsers try Display PDF in browser | Acrobat, Acrobat Reader.

    0 讨论(0)
  • 2020-11-22 05:16

    Either use

    <embed src="file.pdf" />

    if embedding is an option or my new plugin, PIFF: https://github.com/terrasoftlabs/piff

    0 讨论(0)
提交回复
热议问题