zippy1978 / ghost4j

Java wrapper for Ghostscript C API + PS/PDF document handling API
http://www.ghost4j.org
GNU Lesser General Public License v3.0
64 stars 38 forks source link

PDFConverter Processing multiple postscript files fails around 1000 files #67

Open ronlamb opened 4 years ago

ronlamb commented 4 years ago

When trying to process convert hundreds of postscript files I get the following error sometime between 500 and 1000 files.

The code will start failing with the following error.

NFO : 27-Dec-2019 11:49:47.703: message: Temporary file ...AppData\Local\Temp\ghost4j\f8c87cf6-f505-403d-81d8-65635f12f3b0@13400 cannot be deleted java.io.IOException: Temporary file ...AppData\Local\Temp\ghost4j\f8c87cf6-f505-403d-81d8-65635f12f3b0@13400 cannot be deleted at org.ghost4j.util.DiskStore.removeFile(DiskStore.java:132) at org.ghost4j.converter.PDFConverter.run(PDFConverter.java:265) at org.ghost4j.converter.AbstractRemoteConverter.convert(AbstractRemoteConverter.java:85)

A retry will work. However after about 22K or so files all retries will fail.

I am using Java 1,8 on Windows using GhostScript 9.5 and Ghost4J 1.0.1

I tried running the same code on Linux, against both Ghostscript 9.25 and 9.5 and both won't even convert a single file.

INFO : 27-Dec-2019 16:54:00.809: Current allocation mode is local ERROR: 27-Dec-2019 16:54:00.810: GPL Ghostscript 9.25: Unrecoverable error, exit code 1 INFO : 27-Dec-2019 16:54:00.810: message: org.ghost4j.GhostscriptException: Cannot initialize Ghostscript interpreter. Error code is -100 INFO : 27-Dec-2019 16:54:00.921: Error: /undefined in readonlyName INFO : 27-Dec-2019 16:54:00.921: Operand stack: INFO : 27-Dec-2019 16:54:00.921: --nostringval-- FontInfo --dict:2/9(L)-- FullName (Times New Roman) INFO : 27-Dec-2019 16:54:00.921: Execution stack: INFO : 27-Dec-2019 16:54:00.922: %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval -- --nostringval-- --nostringval-- false 1 %stopped_push 2047 1 3 %oparray_pop 2046 1 3 %oparray_pop 2026 1 3 %oparr ay_pop 1884 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopp ed_push --nostringval-- --nostringval-- INFO : 27-Dec-2019 16:54:00.922: Dictionary stack: INFO : 27-Dec-2019 16:54:00.922: --dict:953/1684(ro)(G)-- --dict:0/20(G)-- --dict:82/200(L)-- --dict:31/60(L)-- --dict:5/20(L)-- --dict:2 /9(L)-- INFO : 27-Dec-2019 16:54:00.922: Current allocation mode is local ERROR: 27-Dec-2019 16:54:00.922: GPL Ghostscript 9.25: Unrecoverable error, exit code 1

I tried an alternative method to process the file using, to see if I get better messages, but that worked even worse public void write2() throws GhostscriptException, IOException { ArrayList args = new ArrayList(); PaperSize paperSize = PaperSize.LETTER;

    args.add("-ps2pdf");
    args.add("-dNOPAUSE");
    args.add("-dBATCH");
    args.add("-dSAFER");
    args.add("-dProcessColorModel=/DeviceRGB");
    args.add("-dPDFSETTINGS=/prepress");
    args.add("-dCompatibilityLevel=1.4");
    args.add("-dPDFX=false");
    args.add("-dDEVICEWIDTHPOINTS=" + paperSize.getWidth());
    args.add("-dDEVICEHEIGHTPOINTS=" + paperSize.getHeight());
    args.add("-sDEVICE=pdfwrite");
    args.add("-dPDFWRDEBUG");
    args.add("-sOutputFile=" + outfile.getAbsolutePath());
    args.add("-q");
    args.add("-f");
    args.add("-");

    System.out.println("XXX  sOutputFile=" + outfile.getAbsolutePath());
    Ghostscript gs = Ghostscript.getInstance();

    synchronized (gs) {
        try (
            InputStream is = new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray());
        ) {
            gs.setStdIn(is);
            String[] s = new String[args.size()];
            s = args.toArray(s);

            int i=0;
            for (String str : s) {
                System.out.println("arg[" + i + "] = " + str);
                i++;
            }

                gs.initialize(args.toArray(new String[args.size()]));
        }
    }
}

And I get a similar initialize error I get in Linux, INFO : 27-Dec-2019 11:58:40.300: DSC comment: /Header INFO : 27-Dec-2019 11:58:40.300: << /EPSF 0 /DSC_struct -dsc_data_struct- >> INFO : 27-Dec-2019 11:58:40.301: DSC comment: /Creator INFO : 27-Dec-2019 11:58:40.301: << /Creator (Designer Batch Version:15.0.23 Build Number:337) /DSC_struct -dsc_data_struct- >> INFO : 27-Dec-2019 11:58:40.301: DSC comment: /CreationDate INFO : 27-Dec-2019 11:58:40.301: << /CreationDate ((12/11/19) Time(16:46:39)) /DSC_struct -dsc_data_struct- >> INFO : 27-Dec-2019 11:58:40.301: DSC comment: /Title INFO : 27-Dec-2019 11:58:40.302: << /Title ((RunName:Report_BW StreamName:REPORT_PS)) /DSC_struct -dsc_data_struct- >> INFO : 27-Dec-2019 11:58:40.411: DSC comment: /Page INFO : 27-Dec-2019 11:58:40.412: << /PageNum 17 /DSC_struct -dsc_data_struct- >> INFO : 27-Dec-2019 11:58:40.458: DSC comment: /Page INFO : 27-Dec-2019 11:58:40.458: << /PageNum 18 /DSC_struct -dsc_data_struct- >> INFO : 27-Dec-2019 11:58:40.486: DSC comment: /Page INFO : 27-Dec-2019 11:58:40.486: << /PageNum 19 /DSC_struct -dsc_data_struct- >> INFO : 27-Dec-2019 11:58:40.513: DSC comment: /Page INFO : 27-Dec-2019 11:58:40.514: << /PageNum 20 /DSC_struct -dsc_data_struct- >> ERROR: 27-Dec-2019 11:58:40.589: Extract failed: Cannot initialize Ghostscript interpreter. Error code is -100

I can manually run gswin64c, or gs in Linux on the command line all the files no problem, but would like to automate the conversion in both Windows and Linux.

ronlamb commented 4 years ago

The code to write the file using PDFConverter is: public class PDFWriter extends FileWriter {

/**
 * PDF Converter is static - Repeatably creating and destroying the converter causes errors and
 * after about 26K files fails.
 */
private static final Logger logger = Logger.getLogger(FileWriter.class);
private static final PDFConverter converter;

static {
    logger.info("Initializing PDF Converter");
    converter = new PDFConverter();
    converter.setMaxProcessCount(0);
    converter.setPDFSettings(PDFConverter.OPTION_PDFSETTINGS_PREPRESS);
}

public PDFWriter(File outFile) throws IOException {
    super(outFile);

    os = new ByteArrayOutputStream();
}

public void write() throws IOException, ConverterException, DocumentException, GhostscriptException  {
    Document doc = new PSDocument();

    try (
        InputStream is = new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray())
    ) {
        doc.load(is);
    }

    try (FileOutputStream fos = new FileOutputStream(outfile);)
    {
        converter.convert(doc, fos);
        /* remove the instance */
    } finally { 
        try {
            Ghostscript.deleteInstance();
        } catch (GhostscriptException e) {

        }
    }
}

}