Password Protected PDF Generation in PHP and MySQL
PDF stands for the Portable Document Format. The PDF format is commonly used for saving documents and publications in a standard format that can be viewed on multiple devices. In this tutorial, We have share how to generate Password Protected PDF Generation in PHP and MySQL.TCPDF is now one of the world’s most active Open Source projects, used daily by millions of users and included in thousands of Web applications.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php 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:
1 2 3 |
<?php require_once(dirname(__FILE__)."/TCPDF/tcpdf.php"); ?> |
Create a file like GeneratePDFFile.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php function __autoload($class) { include "include/$class.php"; } $pdf = new GeneratePDFFile(); $pdf->setID(1); $blogInfo = $pdf->getBlogInfo(); ?> <?php include('templates/header.php'); ?> <div class="row"> <div class="col-lg-12"> <h2>Password Protected PDF Generation in PHP and MySQL</h2> </div> </div> <hr> <form target="_blank" action="<?php print SITE_URL;?>action.php" method="post" name="login"> <div class="row"> <div class="col-lg-6"> <div class="input-group mb-3"> <div class="input-group-append"> <span class="input-group-text" id="basic-addon2"><i class="fa fa-key"></i></span> </div> <input type="text" name="password" class="form-control" placeholder="******" value="1234"> </div> </div> <div class="col-lg-6"> <button type="submit" name="submit" class="float-right btn btn-primary"> Create PDF File with Password</button> </div> </div> </form> <hr> <div class="row"> <div class="col-lg-12"> <?php echo $blogInfo['description']; ?> </div> </div> <?php include('templates/footer.php'); ?> |