Ghost4J PDFConverter生成0KB PDF文件

时间:2018-07-26 08:19:21

标签: java pdf ghost4j

我正在使用ghost4j将PS文件转换为PDF。我已经测试过它通过使用getPageCount()方法正确读取ps文件,并返回正确的页数。但是,正在生成的输出pdf文件的大小为0KB,其中没有内容。

这是我的转换代码

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;
}

1 个答案:

答案 0 :(得分:1)

我设法使用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;
}