Skip to content

Instantly share code, notes, and snippets.

@kevcjones-archived
Last active June 20, 2022 02:19
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kevcjones-archived/4483127 to your computer and use it in GitHub Desktop.
Save kevcjones-archived/4483127 to your computer and use it in GitHub Desktop.
Unlocking a PDF using ghostscript
required - install ghost script - e.g brew install ghostscript (takes a few minutes)
update this line as needed and run in terminal
-- New and updated thanks to comments --
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -SPDFPassword=THE_PASSWORD -sOutputFile=unencrypted.pdf -c .setpdfwrite -f 2017-06-13-11-55-56-188_14972345156188_XXXPT6345X_ITRV.pdf
-- Original --
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=unencrypted.pdf -c .setpdfwrite -f encrypted.pdf
@malozaibi
Copy link

malozaibi commented Jun 12, 2017

For Windows

gswin64 -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sFONTPATH=%windir%/fonts;xfonts;. -sPDFPassword= -dPDFSETTINGS=/prepress -dPassThroughJPEGImages=true -sOutputFile=E:outputfile.pdf E:sourcefile.pdf

Notes
  • Change gswin64 with your version name
  • Run this command in the bin folder example C:\Program Files\gs\gs9.21\bin - depends on your version
  • Use drive letters for source and output files like E:\path\outputfile.pdf - just an advice

edited from source

@gkArvindr
Copy link

gkArvindr commented Jun 29, 2017

I tried to experiment it fails

gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=unencrypted.pdf -c .setpdfwrite -f 2017-06-13-11-55-56-188_14972345156188_XXXPT6345X_ITRV.pdf
**** This file requires a password for access.
Error: /invalidfileaccess in pdf_process_Encrypt
Operand stack:

Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1951 1 3 %oparray_pop 1950 1 3 %oparray_pop 1934 1 3 %oparray_pop --nostringval-- --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push
Dictionary stack:
--dict:1178/1684(ro)(G)-- --dict:1/20(G)-- --dict:82/200(L)-- --dict:82/200(L)-- --dict:116/127(ro)(G)-- --dict:280/300(ro)(G)-- --dict:22/32(L)--
Current allocation mode is local
GPL Ghostscript 9.15: Unrecoverable error, exit code 1

@denysvega
Copy link

Hi... I know this discussion has more than 2 years...

@gkArvindr you must provide the PDF password using the -SPDFPassword parameter.

gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -SPDFPassword=THE_PASSWORD -sOutputFile=unencrypted.pdf -c .setpdfwrite -f 2017-06-13-11-55-56-188_14972345156188_XXXPT6345X_ITRV.pdf

@mustaqimM
Copy link

mustaqimM commented Dec 5, 2019

@denysvega Thank you so much for your comment. It's the only one that works

@alexsmithbr
Copy link

Exactly what @mustaqimM said. Thanks, @denysvega! Worked for me too.

@kevcjones-archived
Copy link
Author

I've updated the original gist now - cheers @denysvega

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment