First, we need to download TCPDF Library, then extract TCPDF Library
Step 1: Extract TCPDF Library
Note: Copy and Paste
Step 2: Create Database
For this tutorial, you need a MySQL database with the following table:
CREATE TABLE `blog` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`description` text NOT NULL,
`created_date` varchar(12) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `blog` (`id`, `name`, `description`, `created_date`) VALUES
(1, 'Team TechArise', 'Encryption Example: Consult the source code documentation for the SetProtection() method.', '1551562088');
ALTER TABLE `blog`
ADD PRIMARY KEY (`id`);
ALTER TABLE `blog`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
?>
Step 3: Create class and include
Syntax:
require_once(dirname(__FILE__)."/TCPDF/tcpdf.php");
?>
Create a file like GeneratePDFFile.php.
require_once(dirname(__FILE__)."/DBConnection.php");
require_once(dirname(__FILE__)."/TCPDF/tcpdf.php");
class GeneratePDFFile
{
protected $db;
private $_id;
private $_html;
private $_fileName;
private $_password;
public function setID($id) {
$this->_id = $id;
}
public function seHTML($html) {
$this->_html = $html;
}
public function setFileName($fileName) {
$this->_fileName = $fileName;
}
public function setPassword($password) {
$this->_password = $password;
}
public function __construct() {
$this->db = new DBConnection();
$this->db = $this->db->returnConnection();
}
// get Blog Info function
public function getBlogInfo() {
$query = "SELECT name, description FROM blog WHERE id = ".$this->_id;
$result = $this->db->query($query) or die($this->db->error);
$data = $result->fetch_array(MYSQLI_ASSOC);
return $data;
}
//generate PDF File
public function generatePDFFile() {
ob_start();
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetProtection(array('print', 'copy'), $this->_password, null, 0, null);
// Example with public-key
// To open the document you need to install the private key (tcpdf.p12) on the Acrobat Reader. The password is: 1234
//$pdf->SetProtection($permissions=array('print', 'copy'), $user_pass='', $owner_pass=null, $mode=1, $pubkeys=array(array('c' => '/Applications/XAMPP/htdocs/password-protected-pdf-generation-in-php/include/tcpdf.crt', 'p' => array('print'))));
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('TechArise');
$pdf->SetTitle('TechArise');
$pdf->SetSubject('TechArise');
$pdf->SetKeywords('TechArise');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 016', PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array('helvetica', '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array('helvetica', '', PDF_FONT_SIZE_DATA));
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// set font
$pdf->SetFont('times', '', 16);
// add a page
$pdf->AddPage();
// print a block of html using Write()
//$pdf->Write(0, $this->_html, '', 0, 'L', true, 0, false, false, 0);
$pdf->writeHTML($this->_html, true, false, true, false, '');
//Close and output PDF document
$pdf->Output(ROOT_UPLOAD_PATH.$this->_fileName, 'F');
return $this->_fileName;
}
}
?>
Step 4: Create a action file
Create a file named action.php
function __autoload($class) {
include "include/$class.php";
}
$pdf = new GeneratePDFFile();
$pdf->setID(1);
$blogInfo = $pdf->getBlogInfo();
if(!empty($blogInfo) && count($blogInfo)>0) {
$pdf->seHTML($blogInfo['description']);
$pdf->setFileName(time().'.pdf');
$pdf->setPassword($_POST['password']);
echo $fileName = $pdf->generatePDFFile();
header("Location:".HTTP_UPLOAD_PATH.$fileName);
}
?>
Step 5: Create html file
Create a html file named index.php
function __autoload($class) {
include "include/$class.php";
}
$pdf = new GeneratePDFFile();
$pdf->setID(1);
$blogInfo = $pdf->getBlogInfo();
?>
include('templates/header.php');
?>
Password Protected PDF Generation in PHP and MySQL
include('templates/footer.php');
?>
Demo [sociallocker] Download[/sociallocker]