Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Dynamically Adding Passwords to PDFs using PHP

Dynamically Adding Passwords to PDFs using PHP

0

In a recent project, I was asked to modify uploaded PDFs to dynamically insert a user specified password to keep files secure. To achieve this functionality, I made use of the PDFI library from Setasign, and in particular, the FpdiProtection class. This tutorial shows how this can be done.

Prerequisites

Using Composer (or by other means), you first need to install the following libraries from Setasign:

  • setasign/fpdf (v1.8)
  • setasign/fpdi-protection (v2.0)

Installing the above libraries will also install setasign/fpdi.

Creating the Password Helper

To assist with password protecting PDFs, I created a helper Class called PasswordProtectPDF, which you can find below. The protect function handles importing each page from your source PDF (specified as $file). The output function then return the new generated PDF in various formats.

Examples

Below are various examples of how the dynamically generated PDF can be handled, e.g. returning it to the browser or forcing the PDF to download to the user’s device. The following output types are available (via FPDF library):

  • I = Inline PDF
  • S = PDF as a string for you to handle manually (i.e. with manual headers to force inline rendering or download)
  • D = Forcing the PDF to download to the user’s device, using the second parameter as the filename.
  • F = Saving the PDF to the server as a file, using the second parameter as the file path and name.

Troubleshooting

The FPDF library may not be able to handle all types of files, for example, depending on the compression used in the source PDF. Wrapping the above examples in a try..catch statement will help you catch any errors in generating the PDF.

The above example (lines 4-14) shows how compression errors can be caught and handled gracefully.

For anything else, feel free to leave a comment below and I’ll try and help.

PDF, PHP, Security, Tutorial

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bash Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Geolocation Google Nexus 5 Hacking HTML5 Input Sanitization iOS JavaScript jQuery Laravel 5 Laravel 5.2 Linux Mac OS NodeJS Parse PDF PHP Plugin Portfolio Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy