How to display PDF file in web page from Database in PHP

Theory of Computation
In this article, you will learn how to display the PDF file on a webpage using PHP.

PDF is one of the most trusted document format. PHP provides simple functionality to embed PDF files. It has a very simple implementation. Here, we have mentioned two ways to display PDF in a web page. It demands on how you want to show. If you want to include PDF as a sub part of a web page, then display them in an iframe as we mentioned in method1. If you want to make the PDF as a whole web content, then use the header() function as we mentioned in method2.
Suppose we have a 'infopdf' TABLE in the Database as follows. You can use your existing database or copy and paste this in MySQL.
CREATE TABLE IF NOT EXISTS `infopdf` (
  `fileid` int(11) NOT NULL AUTO_INCREMENT,
  `filename` varchar(150) NOT NULL,
  `directory` varchar(150) NOT NULL,
  `created_date` date NOT NULL,
  PRIMARY KEY (`fileid`)
)
INSERT INTO `infopdf` (`fileid`, `filename`, `directory`, `created_date`) VALUES
(1, 'etp.pdf', '/document/', '2019-07-12');
Now, create a PHP file 'index.php' and write the database connection code at the top. For this, we are using Object Oriented PHP MySQLi connection code. Then, fetch the pdf file name and directory using select statement. To embed the PDF in a web page, we use the iframe element.
<?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>

Here is the PDF file displayed on the webpage that is fetched from the MySQL database table using the PHP code:
In the above method, we have embedded PDF in an iframe. This is basically used when we have to show a pdf in addition to the web contents. But if you want to display PDF as the whole content of webpage, then it's better to use header() function. In this below example, we have added header() function and set the file 'Content-type', 'Content-Disposition', 'Content-Transfer-Encoding'. We have used PHP predefined readfile() function to read the file and added error control operator (@) in front of it. When this operator pretended to an expression, any error or warning might be generated by this expression will be ignored.
<?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);  
?>
Here is the PDF file displayed on the webpage that is fetched from the MySQL database table using the PHP code:

Jan 3
Stateful vs Stateless
A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

Dec 29
Best programming language to learn in 2021
In this article, we have mentioned the analyzed results of the best programming language for 2021...

In this article, we have mentioned the analyzed results of the best programming language for 2021...

Dec 20
How is Python best for mobile app development?
Python has a set of useful Libraries and Packages that minimize the use of code...

Python has a set of useful Libraries and Packages that minimize the use of code...

July 18
Learn all about Emoji
In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

Jan 10
Data Science Recruitment of Freshers
In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...






eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.