Skip to content

PHP Script to Display PDF Document From MySQL Database inside IFrame Popup Window in Browser

<?php
// Database Connection 
$conn = new mysqli('hostname', 'username', 'password', 'database');
//Check for connection error
$select = "SELECT * FROM `infopdf`";
$result = $conn->query($select);
while($row = $result->fetch_object()){
  $pdf = $row->filename;
  $path = $row->directory;
  $date = $row->created_date;
}

echo '<h1>Here is the information PDF</h1>';
echo '<strong>Created Date : </strong>'.$date;
echo '<strong>File Name : </strong>'.$pdf;
?>
<br/><br/>
<iframe src="<?php echo $path.$pdf; ?>" width="90%" height="500px">
</iframe>
<?php
// Database Connection 
$conn = new mysqli('hostname', 'username', 'password', 'database');
//Check for connection error
$select = "SELECT * FROM `infopdf`";
$result = $conn->query($select);
while($row = $result->fetch_object()){
  $pdf = $row->filename;
  $path = $row->directory;
  $date = $row->created_date;
  $file = $path.$pdf;
}
// Add header to load pdf file
header('Content-type: application/pdf'); 
header('Content-Disposition: inline; filename="' .$file. '"'); 
header('Content-Transfer-Encoding: binary'); 
header('Accept-Ranges: bytes'); 
@readfile($file);  
?>