How to show a PDF in a Browser – PHP – Chrome – Header
Requirement:
To show a pdf in a website. Also show the facebook image while sharing.
Solution: The idea is to create a index.php and put all the required facebook og:image and then redirect the page to show.php
Show.php will send the appropriate headers so that the pdf will be viewed in the respective browser.
- Folder Structure /website.com/article
- index.php
- show.php
- title.jpg
- test.pdf
- index.php
- <!DOCTYPE html>
<html lang=”en”><head>
<meta charset=”utf-8″>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<meta name=”keywords” content=””>
<meta name=”keywords” content=””>
<meta name=”description” content=””>
<meta name=”author” content=””><meta property=”og:type” content=”website” />
<meta property=”og:title” content=”xxxxx” />
<meta property=”og:site_name” content=”xxxxx” />
<meta property=”og:description” content=”xxxxx” />
<meta property=”og:image” content=”https://abcd.com/article/title.jpg”><title>test</title>
<link href=”xxxx” rel=”shortcut icon” type=”image/ico” /></head>
<script>location.href=”show.php”; </script>
- <!DOCTYPE html>
- show.php
- <?php// Store the file name into variable
$file = ‘test.pdf’;
$filename = ‘test.pdf’;// Header content type
header(‘Content-type: application/pdf’);header(‘Content-Disposition: inline; filename=”‘ . $filename . ‘”‘);
header(‘Content-Transfer-Encoding: binary’);
header(‘Accept-Ranges: bytes’);
// Read the file
@readfile($file);
?>
- <?php// Store the file name into variable