Online Tutorials & Training Materials | STechies.com
Register Login

How to display PDF/Word File in browser

14 Sep 2016 8:01 am || 0

In my current project I don't want to keep PDF and DOC file on server or open due to security purpose so I want a system where only login user can view or download the files. so to do this have used given below code

Display PDF file on Browser

#############################################

$file = 'myfile.pdf';
$file1 = '/file-dir/'.$file;

ob_clean();
header("Cache-Control: no-store");
header("Expires: 0");
header("Content-Type: application/pdf");
header("Cache-Control: public");
header('Content-Disposition: inline; filename="' . $file . '"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
readfile($file1);

#############################################

Download PDF file

#############################################

$file = 'myfile.pdf';
$file1 = '/file-dir/'.$file;

ob_clean();
header("Cache-Control: no-store");
header("Expires: 0");
header("Content-Type: application/pdf");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
readfile($file1);

#############################################

Display Doc file on Browser

Their is no way you can display Doc file on browser, you can only user google reader but google reader don't open password protected files, for google reader you need to give public access file to view on browser

<iframe src="http://docs.google.com/gview?url=http://www.yoursite.com/xyz.doc&amp;embedded=true"></iframe>