1
votes

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

2 Answers

3
votes

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
votes

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.