1

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;
}
KenS
  • 30,202
  • 3
  • 34
  • 51
Arpan
  • 596
  • 2
  • 10
  • 29
  • Please show your `example.ps` file (preferably a stripped down example) so that we can reproduce your problem. What error message did you get? Instead of `System.out.println("ERROR: " + e.getMessage());` you should do `e.printStackTrace();` do get more detailed error information. – Thomas Fritsch Jul 26 '18 at 11:27

1 Answers1

1

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;
}
Arpan
  • 596
  • 2
  • 10
  • 29