javapdfghostscriptghost4j

Ghost4j produces no output file


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.


Solution

  • 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.