0

tell me please, I've already broken my whole head ... I have a java maven project, I need to change the RGB color to GRAY for the existing pdf files. Stumbled upon Ghostscript, installed it, tested it via command line, it works. Included in the pom file ghost4j. My method:

private void ColorConversion(String newFileName, String directory) throws GhostscriptException {

//get Ghostscript instance
Ghostscript gs = Ghostscript.getInstance();

//prepare Ghostscript interpreter parameters
//refer to Ghostscript documentation for parameter usage
List<String> gsArgs = new ArrayList<String>();
gsArgs.add("-sDEVICE=pdfwrite");
gsArgs.add("-sProcessColorModel=DeviceGray");
gsArgs.add("-sColorConversionStrategy=Gray");
gsArgs.add("-dEncodeColorImages=false");
gsArgs.add("-sColorConversionStrategyForImages=Gray");
gsArgs.add("-o " + directory + File.separator + "output_cmyk.pdf");
gsArgs.add(newFileName);

//execute and exit interpreter
gs.initialize(gsArgs.toArray(new String[0]));
gs.exit();

} I get the error: java.lang.UnsatisfiedLinkError: Unable to load library 'gsdll64': Native library (win32-x86-64/gsdll64.dll) not found in resource path (D:\Education\javafx-maven-color\target\classes...etc.

How can I tell where to find this library? Just adding the library to target does not exit, when doing clean in maven it deletes the folder.

I also tried to run with this command:

gsArgs.add("-I c:\\Program Files\\gs\\fonts\\;c:\\Program Files\\gs\\lib\\;C:\\Program Files\\gs\\bin\\;");

zero reaction.(

UPD: For test, i wrote this string

Runtime.getRuntime().load(this.getClass().getResource("/org/example/gsdll64.dll").toString().replace("file:/", ""));

My application on JavaFX and i use the interface Task for my class. Wrote the following code:

try {
        //execute and exit interpreter
        synchronized (gs) {
            gs.initialize(gsArgs.toArray(new String[0]));
            gs.exit();
        }
    } catch (GhostscriptException e) {
        throw e;
    } finally {
        //delete interpreter instance (safer)
        try {
            Ghostscript.deleteInstance();
        } catch (GhostscriptException e) {
            //nothing
        }
    }

But when executing the code, it throws an error:

"Invalid memory access"

log:

0 [Thread-3] INFO org.ghost4j.Ghostscript  - GPL Ghostscript 9.54.0 (2021-03-30)
2 [Thread-3] INFO org.ghost4j.Ghostscript  - Copyright (C) 2021 Artifex Software, Inc.  All rights reserved.
2 [Thread-3] INFO org.ghost4j.Ghostscript  - This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
2 [Thread-3] INFO org.ghost4j.Ghostscript  - see the file COPYING for details.
61 [Thread-3] INFO org.ghost4j.Ghostscript  - Processing pages 1 through 2.
61 [Thread-3] INFO org.ghost4j.Ghostscript  - Page 1
Shaldryn
  • 13
  • 1
  • 3
  • Does this help answer your question? [Managing DLL dependencies with Maven](https://stackoverflow.com/q/1001774/5221149) – Andreas Apr 30 '21 at 07:31
  • Got an idea to use Resources in maven like this: Runtime.getRuntime().load(this.getClass().getResource("/org/example/gsdll64.dll").toString().replace("file:/", "")); – Shaldryn Apr 30 '21 at 11:55
  • Except that will fail terribly when the application gets packaged as a jar file. – Andreas Apr 30 '21 at 21:02
  • You're right... I want to test the application for performance. And i have a problem with thread... – Shaldryn May 04 '21 at 11:32
  • By default, at least, Ghostscript isn't thread safe. There is a build option to build a threadsafe version but, not going to lie, it's had limited testing. I don't know if that's your problem, but I thought it worth mentioning. – chrisl May 06 '21 at 09:19

0 Answers0