mPDF Manual – mPDF functions

SetProtection()

(mPDF >= 1.0)

SetProtection – Encrypts and sets the PDF document permissions

Description

void SetProtection ( array $permissions [, string $user_password [, string $owner_password [, integer $length ]]])

Encrypts and sets the PDF document permissions for the PDF file, together with user and owner passwords.

Parameters

$permissions

This parameter is an array which specifies the permissions granted to the end-user.

A blank array should be passed to deny all permissions to the user. The latter 4 permissions were added in mPDF >=5.3 

Using any of these last 4 permissions require 128-bit encryption and will force this mode, regardless of any value set for $length.

Values (case-sensitive)

An array() including any, all or none of the following. The values included are those permissions allowed:

  • 'copy'
  • 'print'
  • 'modify'
  • 'annot-forms'
  • 'fill-forms'
  • 'extract'
  • 'assemble'
  • 'print-highres'
$user_password

Specify a password required for a user to open the PDF file.

BLANK or omitted - No password is required to open the PDF document.

$owner_password

Specify a password which will allow full access and permissions to the PDF file.

If omitted, or you pass in null, a random password is generated by mPDF

$length

Specify the bit-length used for encryption. Two values are possible, 40 and 128. The 4 latter $permissions (see above) require 128-bit encryption, and setting any of these will automatically set $length as 128, overriding any value specified.

Default: 40 - use 40-bit encryption

Values

  • 40
  • 128

Changelog

Version Description
2.5 CJK files can be encrypted
3.2 Empty (blank array) $permissions array correctly handled.
5.3 Additional $permissions added, and $length parameter added enabling 128-bit encryption

Examples

Example #1

<?php
$mpdf = new \Mpdf\Mpdf();

// Encrypt the file and grant no permissions to the user to copy, print etc.
// The user will be able to open the file as no password is specified
// Owner cannot access full rights because no owner_password was set
$mpdf->SetProtection(array());

$mpdf->WriteHTML('Hello World');
$mpdf->Output('filename.pdf');

Example #2

<?php

// Encrypt the file and grant no permissions to the user
// The user will need to use "UserPassword" to open the file
// Owner has full rights using the password "MyPassword"
$mpdf->SetProtection(array(), 'UserPassword', 'MyPassword');

// Encrypt the file and grant permissions to the user to copy and print
// No password is required to open the document
// Owner has full rights using the password "MyPassword"
$mpdf->SetProtection(array('copy','print'), '', 'MyPassword');

Fork me on GitHub