javapdfghost4j

Ghost4J PDFConverter generating 0KB PDF File


I'm using ghost4j to convert PS file to PDF. I have tested it's reading the ps file correctly by using the getPageCount() method it's returning the correct page count. However the output pdf file that's generating is 0KB in size and have not content in it.

Here is my conversion code

public static File converPsToPdf(File psFile){
    FileOutputStream fos = null;
    File tempFile=null;
    try{
        //load PostScript document
        PSDocument document = new PSDocument();
        document.load(psFile);
        //create OutputStream
        //tempFile = File.createTempFile("temp", ".pdf", null);
        tempFile=new File("C:/Users/DOS20/Desktop/rendition.pdf");
        fos = new FileOutputStream(tempFile);
        //create converter
        PDFConverter converter = new PDFConverter();
        //set options
        converter.setPDFSettings(PDFConverter.OPTION_PDFSETTINGS_PREPRESS);
        //convert
        converter.convert(document, fos);
    } catch (Exception e) {
        System.out.println("ERROR: " + e.getMessage());
    } finally{
        IOUtils.closeQuietly(fos);
    }
    return tempFile;
}

Solution

  • I manage to solve the issue by using org.ghost4j.Ghostscript

    public static File convertPSToPdf(String psFile){
        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=C:/Users/DOS20/Desktop/rendition.pdf";
        gsArgs[7] = psFile;
        try {
            gs.initialize(gsArgs);
            gs.exit();
            return new File("C:/Users/DOS20/Desktop/rendition.pdf");
        } catch (GhostscriptException e) {
            System.out.println(e.toString());
        }
        return null;
    }