Protect Your PostScript Files from Being Converted to PDF

If you've ever used fillable PDF forms, you've probably noticed that you can't save a copy of the form once it has been filled in. Being sneaky, you probably might try to print the form to a file (PostScript) and then use ps2pdf to convert it to a PDF. But, this doesn't work either, because ghostscript gives you an error saying the file can't be redistilled. This article shows you how to protect your own PostScript using the same technique.

The PostScript code that protects these PostScript files follows:

%ADOBeginClientInjection: DocumentSetup Start "No Re-Distill"
%% Removing the following eleven lines is illegal, subject to the Digital Copyright Act of 1998.
mark currentfile eexec
54dc5232e897cbaaa7584b7da7c23a6c59e7451851159cdbf40334cc2600
30036a856fabb196b3ddab71514d79106c969797b119ae4379c5ac9b7318
33471fc81a8e4b87bac59f7003cddaebea2a741c4e80818b4b136660994b
18a85d6b60e3c6b57cc0815fe834bc82704ac2caf0b6e228ce1b2218c8c7
67e87aef6db14cd38dda844c855b4e9c46d510cab8fdaa521d67cbb83ee1
af966cc79653b9aca2a5f91f908bbd3f06ecc0c940097ec77e210e6184dc
2f5777aacfc6907d43f1edb490a2a89c9af5b90ff126c0c3c5da9ae99f59
d47040be1c0336205bf3c6169b1b01cd78f922ec384cd0fcab955c0c20de
000000000000000000000000000000000000000000000000000000000000
cleartomark
%ADOEndClientInjection: DocumentSetup Start "No Re-Distill"

So, all you have to do to protect your own PostScript files from being converted into PDFs is to insert this code into the PostScript after the %BeginSetup or the %EndProlog lines of the PostScript. The following script does just that for the PostScript file passed to it on the command line:

#!/bin/bash

if [[ $# -ne 1 ]]; then
	echo "Usage: $0 PSFILE"
	exit 1
fi
psfile=$1

nl='
'
protect='
\%ADOBeginClientInjection: DocumentSetup Start "No Re-Distill"
\%\% Removing the following eleven lines is illegal, subject to the Digital Copyright Act of 1998.
mark currentfile eexec
54dc5232e897cbaaa7584b7da7c23a6c59e7451851159cdbf40334cc2600
30036a856fabb196b3ddab71514d79106c969797b119ae4379c5ac9b7318
33471fc81a8e4b87bac59f7003cddaebea2a741c4e80818b4b136660994b
18a85d6b60e3c6b57cc0815fe834bc82704ac2caf0b6e228ce1b2218c8c7
67e87aef6db14cd38dda844c855b4e9c46d510cab8fdaa521d67cbb83ee1
af966cc79653b9aca2a5f91f908bbd3f06ecc0c940097ec77e210e6184dc
2f5777aacfc6907d43f1edb490a2a89c9af5b90ff126c0c3c5da9ae99f59
d47040be1c0336205bf3c6169b1b01cd78f922ec384cd0fcab955c0c20de
000000000000000000000000000000000000000000000000000000000000
cleartomark
\%ADOEndClientInjection: DocumentSetup Start "No Re-Distill"
'
protect="${protect//$nl/\\n}"

if grep --silent '^%%BeginSetup' $psfile; then
	sed -e "/\%\%BeginSetup/a\\$protect" $1 
else
	sed -e "/\%\%EndProlog/a\\$protect" $1 
fi

To test it, take an unprotected PostScript file and convert it to a protected PostScript file, and then try to convert it to a PDF:

$ ps2pdf unprotected.ps
$ sh prps.sh unprotected.ps >protected.ps
$ ps2pdf protected.ps
This PostScript file was created from an encrypted PDF file.
Redistilling encrypted PDF is not permitted.
Error: /undefined in --eexec--
Operand stack:
   --nostringval--   --dict:98/200(L)--   quit
Execution stack:
   %interp_exit   .runexec2   --nostringval--   ...
Dictionary stack:
   --dict:1169/3371(ro)(G)--   --dict:0/20(G)--  ...
Current allocation mode is local
Last OS error: 2
GPL Ghostscript 8.62: Unrecoverable error, exit code 1

As you can see, you can convert unprotected.ps to PDF without a problem, but once you add the super-secret protection code to it and create protected.ps, you're no longer able to convert to PDF.

Mitch Frazier is an embedded systems programmer at Emerson Electric Co. Mitch has been a contributor to and a friend of Linux Journal since the early 2000s.

Load Disqus comments