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.

  1. Folder Structure /website.com/article
    1. index.php
    2. show.php
    3. title.jpg
    4. test.pdf
  2. index.php
    1. <!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>

  3. show.php
    1. <?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);
      ?>