Pigalev Pavel
Pigalev Pavel

Reputation: 1185

Password protected PDF using Ghostscript

I need to put a password protection to PDF files using ghostscript in php.

These files will be uploading to server using simple form (I don't need any help with this), but they won't have any protection at first. So I want to put password protection to them using exec function and ghostscript in it. But I couldn't find anywhere what ghostscript query should be like.

For example, I have a PDF file called File.pdf. I upload it and then I need to put protection to it and call it File_protected.pdf.

I was trying to do it like this but '.ps' file weights too much and there is no password in the final File_protected.pdf:

exec("gs -dNOPAUSE -dBATCH -sDEVICE=pswrite -sOutputFile=File.ps File.pdf");
exec("gs -dNOPAUSE -dBATCH -sPDFPassword=password -sDEVICE=pdfwrite -sOutputFile=File_protected.pdf File.ps");

Upvotes: 9

Views: 16895

Answers (4)

K J
K J

Reputation: 11739

There seems to be some confusion in how passwords are applied when handling PDF via GhostScript, two former answers say the opposite effect does not work.

Which will be true if you apply the wrong one !

To Open a protected file you need -sPDFPassword=BlahBlah this is because the passkey is essential for decryption, then you can save without that password encryption, as they are very little value and counter productive.

Encryption in effect degrades the readers accessibility by adding unnecessary delays and overheads.
The ONLY reason to add a PDF password is for your own personal privacy, where only you and nobody else knows your personal openkey. All other passwords can be easily removed by many apps including GhostScript or any other reader/writer.

To Write a PDF with a file lock (for personal privacy) you need to consider what settings you want. Those can then be added via -sOwnerPassword=myprivacy -sUserPassword=myprivacy

There is little point in adding owner settings since the user is the owner of that PDF thus user can remove or ignore their own or others user settings.

Upvotes: 0

Pablo Guerrero
Pablo Guerrero

Reputation: 1054

The switches -sOwnerPassword and -sUserPassword didn't work for me.

However, -sPDFPassword did.

Upvotes: 5

Manan Shah
Manan Shah

Reputation: 1098

The above answer is not worked for me so I am posting my answer.

Ghostscript version:

manan@manan-EliteBook-8470p ~ $ gs -v
GPL Ghostscript 9.18 (2015-10-05)
Copyright (C) 2015 Artifex Software, Inc.  All rights reserved.

The command which will work...

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dBATCH -dNOPROMPT -dNOPAUSE -dQUIET -sOwnerPassword=mypassword -sUserPassword=manan -sOutputFile=MyOutputFile.pdf MyInputFile.pdf

Upvotes: 6

KenS
KenS

Reputation: 31141

OK so firstly you don't need to convert the file to PostScript. Ghostscript is perfectly capable of taking the PDF file as an input and producing a PDF file as an output, lots of people do this for many reasons.

However, you need to be aware that if you do this, Ghostscript isn't just 'stamping' the PDF file or something, it is fully interpreting it down to marking operations and then making a completely new PDF file which incorporates those marks. But if you were satisfied by converting to PostScript and back to PDF you should find this satisfactory, its actually better than doing that 2 step conversion.

Secondly, there is no 'PDFPassword' switch for the pdfwrite device, which is why its having no effect. There are 2 switches: -sOwnerPassword and -sUserPassword. You may also want to supply the -dPermissions switch.

You should read the PDF reference manual to glean the details but in short the Owner can do anything to the file, the User is limited to the Permissions (which is a bit field). If you don't supply a User password then anyone can open the file (limited to Permissions) but you need to supply the Owner password to do anything which is not allowed by the Permissions. I suspect this is what you want to do but its up to you.

Upvotes: 16

Related Questions