0

I used this and this to check a PDF file is password protected or not! i test this methods:

exec("$shFunction withPassword.pdf",$result);

OR

shell_exec('sh ' . $shFunction . ' withPassword.pdf');

  • i set ghostscript in $shFunction
  • password.pdf and php file are in same folder

but i get error:

Failed to invoke gs 
Mehrdadam
  • 11
  • 3

1 Answers1

0

Well, Ghostscript isn't a PDF password checker, so that might be a problem.

Also the Ghostscript executable is not called ghostscript. The executable name varies depending on the platform and word size. However, I see that the error is 'failed to invoke gs' which suggests that you are at least getting the name correct.

The most likely sources of problems are:

1) Ghostscript isn't installed

2) The $PATH environment variable for the user which the process is running as does not include the folder containing Ghostscript binary gs.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • But i used another Ghostscript in this [link](https://stackoverflow.com/questions/2950246/is-it-possible-to-remove-a-password-from-a-pdf-file-using-php/#answer-2951100) and work correct. – Mehrdadam Feb 06 '18 at 17:37
  • I'm sorry I don't know what you mean by 'used another Ghostscript'. However it sounds to me like you have tagged this incorrectly, you want help with PHP not with Ghostscript. – KenS Feb 06 '18 at 19:21
  • i don't know! i using two Ghostscript for "password check" and "decrypt" PDFs. The "decrypt" Ghostscript work correctly but i test "password check" Ghostscript, but not work. i used multi methods but not worked.. – Mehrdadam Feb 06 '18 at 20:00
  • 1
    OK so I think you are saying that you can run Ghostscript to create an unencrypted PDF from a password-protected encrypted PDF (naughty!), but when you run the simple command you get an error. Well, frankly I'm not surprised. All that 'gs ' will do is render the PDF file to the default device. I'd guess that your version of Ghostscript has been built for X Windows, and so the default device is the X device, but that you don't have an X server available, so GS throws an error and exits with an error code, which the exec is noting. In effect, the command doesn't do what you think. – KenS Feb 07 '18 at 08:33
  • Thanks man for explanations . So, do you know what I should do? I want only to check PDF files that have password or not !! – Mehrdadam Feb 07 '18 at 08:45
  • 1
    Well Ghostscript won't tell you that without extra work. Its probably possible to write a PostScript program to find out, but we don't have an 'off the shelf' tool for it. I **think** that simply searching the file for the presence of /Encrypt would work. – KenS Feb 07 '18 at 13:47