Protect your PDF files generated with TCPDF

Author - Pawandeep Kaur
26.12.2018
|
0 Comments
||

Protect your PDF files generated with TCPDF

TCPDF is a open source PHP library which is used to generate PDF documents. After downloading it, you can invoke all basic functions of this class without requiring external libraries. It supports various languages: UTF-8, Unicode, Right-To-Left, Javascript and much more.

Note: SVG images are supported in this class, but not in FPDF class.

When you want to protect your PDF file from printing, editing or modifying, tcpdf class provides built-in library for this. This library includes password protection and encryption methods. Basically the SetProtection() method is invoked to add the more security to your document.

$pdf->SetProtection($permissions = null, $userPassword = null, $ownerPassword = null, $encryptionType = null, $publicKey = null);

This method expects the following parameters:

A). Permissions Array:
In permission array, you can set various permissions i.e. Print, Copy, Modify, print-high.
For example: if you want to protect your document from being modified, then you can give “modify” value in permission array.

B). Set User Password:
This parameter value will be set when you want to set the password on opening of your PDF file. User will be able to open the pdf file by using this password. If you don’t set any password, the document will open as usual.

C). Set Owner Password:
You can set this password to edit the document permissions. If you do not set it manually, random password will be generated.

D). Encryption type:
There are four encryption modes:
0 = RSA 40 bit
1 = RSA 128 bit
2 = AES 128 bit
3 = AES 256 bit
PDF encryption works with encryption keys of 40, 128, or 256 bit. These keys are selected based on pdf version. User provides the password from which the binary encryption key is created.

E). Public Key:
You can protect your document by using public key encryption method. In this parameter you have to provide an array which contains two keys:
– c: the local path to the certificate (yourpublickey.crt).
– p: permissions as listed in first parameter.

In this method, firstly you will encrypt the document with your private key. After that users can decrypt/open your document by using your public key which you provided in this parameter.

EXAMPLE:

<?php
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, ‘Letter’, true, 'UTF-8', false);
$pdf->SetProtection(array(‘modify’,'copy',’print’), "userpassword123", "masterpassword123", 0, null);
// set document information
$pdf->SetCreator(‘Test User’);
$pdf->SetTitle('Pdf Protection Method');
$pdf->SetSubject(Pdf Protection Tutorial');
// set default header data
$pdf->SetHeaderData($pdfLogo, PDF_HEADER_TITLE.'016', PDF_HEADER_STRING);
$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);
// add a page
$pdf->AddPage();
// set text for printing
$html_content = '<table><tr><td>PDF Protection</td><td>Testing</td></tr>';
$html_content = $html_content.'<tr><td>PDF Protection</td><td></td></tr></table>';
$pdf->Write(0,html_content, '', 0, 'L', true, 0, false, false, 0);
$pdf->Output(‘pdf_name.pdf’ , ‘I’);  // send the output directly to browser
?>

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *