Password protected PDF using Ghostscript
Asked Answered
A

4

9

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");
Annadiane answered 16/10, 2012 at 18:21 Comment(0)
F
16

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.

Fork answered 17/10, 2012 at 7:24 Comment(1)
For those who need more information on some of the items referenced in the above answer: - The PDF reference manual is published by Adobe, but I think they're fairly old now. If you search for pdf_reference17.pdf you should find one somewhere. Pages 120-124 describe the various permissions. - StackOverflow question and answer at the URL below specifically describe how to build/unpack the permissions bit field: #51116241Inadvertent
C
6

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
Concoction answered 4/5, 2018 at 10:5 Comment(0)
S
5

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

However, -sPDFPassword did.

Spheroid answered 13/5, 2018 at 15:48 Comment(0)
M
0

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.

Metamorphosis answered 20/9, 2023 at 14:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.