Skip to main content

Create Password Protected PDF with PHP & MySQL

Portable Document Format (PDF) is the most popular file format which is used for creating documents. In web applications, we also need to implement functionality create PDF documents. Sometimes there also requirement to create password protected PDF documents to make it secure.

So you’re thinking about creating password protected PDF documents then you’re at right place. In this tutorial you will learn how to create password protected PDF documents using PHP and MySQL. We will use FPDF PHP library to create password protected PDF.

We will cover this tutorial in easy steps with live demo to create password protect PDF to display employee details by entering password.

Also, read:

So let’s start implementing password protected pdf with PHP and MySQL. Before we begin, take a look on files structure for this example.

  • index.php
  • pdf.php

Step1: Create MySQL Database Table

As we will cover this tutorial with live example to create password protected PDF with PHP and MySQL. So we will create MySQL database table developers to display details into password protected PDF.

CREATE TABLE `developers` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `skills` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `gender` varchar(255) NOT NULL,
  `designation` varchar(255) NOT NULL,
  `age` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

We will also insert few records into developers table to display.

INSERT INTO `developers` (`id`, `name`, `skills`, `address`, `gender`, `designation`, `age`) VALUES
(1, 'Smith', 'Java', 'Newyork', 'Male', 'Software Engineer', 34),
(2, 'David', 'PHP', 'London', 'Male', 'Web Developer', 28),
(3, 'Rhodes', 'jQuery', 'New Jersy', 'Male', 'Web Developer', 30),
(4, 'Jay', 'PHP', 'Delhi, India', 'Male', 'Web Developer', 30);

Step2: Display Records from MySQL Database

In index.php file, we will display developers records from MySQL database table developers with link to view developer details in PDF format.

<?php
include_once("inc/db_connect.php");
$sqlQuery = "SELECT id, name, gender, age, skills, designation FROM developers LIMIT 5";
$resultSet = mysqli_query($conn, $sqlQuery) or die("database error:". mysqli_error($conn));
?>
<table id="editableTable" class="table table-bordered">
	<thead>
		<tr>
			<th>Id</th>
			<th>Name</th>
			<th>Gender</th>
			<th>Age</th>
			<th>Skills</th>
			<th>Designation</th>
			<th> </th>					
		</tr>
	</thead>
	<tbody>
		<?php while( $developer = mysqli_fetch_assoc($resultSet) ) { ?>
		   <tr>
		   <td><?php echo $developer['id']; ?></td>
		   <td><?php echo $developer['name']; ?></td>
		   <td><?php echo $developer['gender']; ?></td>
		   <td><?php echo $developer['age']; ?></td>
		   <td><?php echo $developer['skills']; ?></td>
		   <td><?php echo $developer['designation']; ?></td>
		   <td><a href="pdf.php?id=<?php echo $developer ['id']; ?>" target="_blank">PDF</a></td>
		   </tr>
		<?php } ?>
	</tbody>
</table>

Step2: Create Password Protected PDF

In pdf.php file, we will implement functionality to create password protect PDF using FPDF library. We will also use FPDF_Protection.php library to make PDF password protected. Here in this example, we will set first two character from developer name to set as password to view details in PDF.

require('lib/FPDF_Protection.php');
$pdf = new FPDF_Protection();
$pdf->SetProtection(array('print'), $password); 

Here is complete code to create dynamic password protected PDF with PHP and MySQL.

<?php
if(!empty($_GET['id']) && $_GET['id']) {
	include_once("../db_connect.php");
	$sql = "SELECT id, name, gender, age, skills, designation FROM developers WHERE id='".$_GET['id']."'";
	$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
	$developerDetails = mysqli_fetch_assoc($resultset);
	$password = strtolower(substr($developerDetails['name'], 0, 2));
	require('lib/FPDF_Protection.php');
	$pdf = new FPDF_Protection();
	$pdf->SetProtection(array('print'), $password); 
	$pdf->AddPage();
	$pdf->SetFont('Arial', 'B', 18);
	$pdf->Cell(100, 12, 'Employee Details', 0);
	$pdf->Ln();
	$pdf->SetFont('Arial', 'B', 12);
	while ($column = mysqli_fetch_field($resultset)){
		$pdf->Cell(32,12, ucfirst($column->name), 1);
	}	
	$pdf->SetFont('Arial', '', 12);
	$pdf->Ln();
	foreach($developerDetails as $developer) {
		$pdf->Cell(32,12, ucfirst($developer), 1);
	}	
	$pdf->Output();
}
?>

You may also like:

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo Download