1

I am trying to produce a pdf-output. I finished already the pdf-file with pdfbox, but it has now around 15 MB. This is to large for the planned purpose. So I want to reduce the file size. I tried it first with linux terminal and ghostscript:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=4 -dNOPAUSE -dBATCH -r150 -sOutputFile=output.pdf input.pdf

This works fine. But since it is a java program and should work without shell, I tested ghost4j:

Ghostscript gs = Ghostscript.getInstance();
String[] gsArgs = new String[7];
gsArgs[0] = "-sDEVICE=pdfwrite ";
gsArgs[1] = "-dCompatibilityLevel=1.4 ";
gsArgs[2] = "-dPDFSETTINGS=/screen ";
gsArgs[3] = "-dNOPAUSE ";
gsArgs[4] = "-dBATCH ";
gsArgs[5] = "-sOutputFile=qw3.pdf ";
gsArgs[6] = "input.pdf";
gs.initialize(gsArgs);
gs.exit();

But I am getting no output file. Are some of this arguments illegal?

Hope, somebody can help.

2 Answers2

3

Thanks to @KenS I got the answer:

Ghostscript gs = Ghostscript.getInstance();
String[] gsArgs = new String[8];
gsArgs[1] = "-sDEVICE=pdfwrite";
gsArgs[2] = "-dCompatibilityLevel=1.4";
gsArgs[3] = "-dNOPAUSE";
gsArgs[4] = "-dBATCH";
gsArgs[5] = "-r150";
gsArgs[6] = "-sOutputFile=qw3.pdf";
gsArgs[7] = "input.pdf";
gs.initialize(gsArgs);
gs.exit();

It seems to be important, that gsArgs[0] has to be null.

Thanks to @KenS.

0

A couple of the arguments are, at least, incorrect. CompatibilityLevel should be a number between 1.0 and 1.7, 4 is too large. Setting the resolution isn't a smart idea with pdfwrite, it will generally have no effect. There are exceptions, if you are taking a transparenct PDF file to a version of PDF so low that it doesn't support transparency for example.

You should get at the very least a 0-byte PDF file, if you don't then something is badly wrong.

If you do get a 0-byte file then at least the operation started. You should capture the back channel output and read it for any signs of errors.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • Thank you @KenS. I changed it now a little bit. But still I don't get an output. Not even a null byte file. Do you have some suggestions, how I could reduce the file size? – Björn Engel Jan 19 '17 at 14:41
  • If you aren't getting any output file at all, then Ghostscript didn't start up properly. You should still look at the back channel output to see if it said something useful, and check the error return code. No, I've no idea how to do either of those with Ghost4J because I'm not a JavaScript developer. My 'guess' would be that Ghosts4J cannot locate the Ghostscript DLL, which may be because you have the wrong bit depth Ghostscript installed (64 vs 32 for example). Have you managed to get Ghost4J working at all, ever ? – KenS Jan 19 '17 at 16:16
  • If I remove `gsArgs[3] = "-dNOPAUSE ";` a windows with the name ghostscript opens and displays the inputfile. – Björn Engel Jan 19 '17 at 16:44
  • 1
    So you have not got the device set up, and Ghostscript is using the default device, which is the display device. Oh, I see, you have set the device as args[0]. You need to set args[0] to a dummy value, and start the real values at args[1]. This is because these arguments must be the same as the ones presented to main() in a C program, and the initial argument (arg[0]) in a C program is the name of the application being started. – KenS Jan 20 '17 at 08:34