Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display pdf in php

Tags:

php

pdf

I'm trying to display a pdf file in php, I used:

<html>
    <head>
        <style type="text/css">
            #myiframe {
                width: 600px;
                height: 100%;
            }
        </style>
    </head>
    <body>
        <div id="scroller">
            <iframe name="myiframe" id="myiframe" src="xml.pdf"></iframe>
        </div>
    </body>
</html>

it works in HTML well, but when I want to use this code into a php file, it displays nothing and only tries to download the pdf file. Can anybody help me?

like image 841
Lamiya Avatar asked Aug 04 '13 06:08

Lamiya


People also ask

Can PHP be used to display PDF?

PHP uses a standard code to display the pdf file in web browser. The process of displaying pdf involves location of the PDF file on the server and it uses various types of headers to define content composition in form of type, Disposition, Transfer-Encoding etc.

How do I display a PDF in HTML?

The easiest way to put PDF in an HTML document is using the <a> tag with its href attribute. You need to add the URL or the reference link of your PDF file to the element.

How do I show a PDF on my website?

Generally, a hyperlink is used to link a PDF document to display in the browser. An HTML anchor link is the easiest way to display a PDF file. But if you want to display a PDF document on the web page, PDF file needs to be embedded in HTML. The HTML <embed> tag is the best option to embed PDF document on the web page.


1 Answers

Try this below code

<?php
$file = 'dummy.pdf';
$filename = 'dummy.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);
?>
like image 138
M. Lak Avatar answered Oct 20 '22 07:10

M. Lak