Cookbook

Protecting DOCX, PDF, XLSX and PPTX documents

You can protect documents against unauthorized accesses through many ways: from a basic restriction that allows just reading it but not changing it to more advanced ones like encrypting with passwords the content.

phpdocx provides the necessary methods to add the kind of protection you may want for DOCX, XLSX, PPTX and PDF documents.

Basic protection

The simplest kind of protection of a DOCX lets you block a document against changes, setting it as a “read-only” file. The setMarkAsFinal method activates the restriction in any DOCX, being either documents made from scratch or templates. E.g.: to add Basic protection to a new document, execute this code:

This is not an effective protection because when Word opens the document, it shows this message: ‘MARKED AS FINAL An author has marked this document as final to discourage editing’. Unfortunately, any user can modify the content simply by pressing the button ‘Edit anyway’ that appears beside that warning. LibreOffice and OpenOffice applications do not even support this protection, so the document could be edited straightaway.

Protecting the document with a password (Premium licenses only)

A step forward in DOCX security is to run the protectDOCX method, available in the CryptoPHPDOCX class. This functionality lets you limit the documents to read-only, comment-only, revision-only or form-only modes. Access to full edition would be granted by a password.

E.g.: To protect a document as read-only (the default choice) with 'phpdocx' as password, run the following code:

If you just need to protect certain parts of a document, and not the whole of it, the method addPermProtection allows to define editable sections.

This type of protection is safer than the basic one. Nevertheless, a user with enough technical skills could break it, and applications like LibreOffice and OpenOffice don't support it.

To remove this protection you just need to extract the content of the DOCX (it's a ZIP) and edit the word/setting.xml file by deleting the w:documentProtection tag. Then, just by compressing the files and folders in a new ZIP with the DOCX extension is all that it takes to have the document ready for changes.

Protecting the content PDF files is quite a different matter. The protectPDF method allows to block the access by password with the 'print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble' and 'print-high' accesses.

This way, you can forbid the access to print with the code:

Warning

This PDF protection only works if the document is opened with a compatible reader. Other way, the protection is not applied.

Encryption (Premium licenses only)

Encrypting is the only way to achieve complete and safe protection for documents. Phpdocx allows to encrypt the following document formats: DOCX (encryptDOCX), XLSX (encryptXLSX), PPTX (encryptPPTX) and PDF (encryptPDF). Both methods allow to block access to documents, and only permit to read it if the user types the password that was used for the encryption.

LibreOffice and OpenOffice and other apps are compatible with DOCX, XLSX and PPTX files encryption. Users may read the content only if they know the password.

To encrypt a DOCX with a password, apply this code:

To encrypt a XLSX, execute:

To encrypt a PPTX, execute:

To encrypt a PDF, execute: