使用Ghost4j在桌面应用程序java中将PS转换为PDF

时间:2015-03-10 15:11:04

标签: java postscript ghost4j

我正在尝试将ps转换为pdf文档并向我显示异常:     java.io.IOException:PostScript文档无效, 代码如下:

FileOutputStream fos = null;
PSDocument document = new PSDocument();
File archivoPDFTemp = null;
Inputfile String = "prueba_4151483.ps";
try {
    archivoPDFTemp = new File (outputfile + "pdf.");
    document.load (new File (inputfile));

    // Create OutputStream
    fos = new FileOutputStream (archivoPDFTemp);

    // Create converter
    PDFConverter converter = new PDFConverter();

    // Set options
    converter.setPDFSettings (PDFConverter.OPTION_PDFSETTINGS_PREPRESS);

    // Convert
    converter.convert (document, fos);
}
Catch (FileNotFoundException e) {
    archivoPDFTemp = null;
    e.printStackTrace ();
}
Catch (IOException e) {
    archivoPDFTemp = null;
    e.printStackTrace ();
}

在线     document.load(new File(inputfile)); 我跳转异常并加载文档的格式如下:

%!PS-Adobe-3.0
%%BeginProlog
/imStr 0 def /imageSrc {currentfile /ASCII85Decode filter /RunLengthDecode filter  imStr readstring pop } def
/BD {bind def} bind def
/D {def} BD
/C {curveto} BD
/L {lineto} BD
/M {moveto} BD
/R {grestore} BD
/G {gsave} BD
/N {newpath} BD
/P {closepath} BD
/EC {eoclip} BD
/WC {clip} BD
/EF {eofill} BD
/WF {fill} BD
/SG {setgray} BD
/SC {setrgbcolor} BD
/ISOF {
     dup findfont dup length 1 add dict begin {
             1 index /FID eq {pop pop} {D} ifelse
     } forall /Encoding ISOLatin1Encoding D
     currentdict end definefont
} BD
/NZ {dup 1 lt {pop 1} if} BD
/S {
     moveto 1 index stringwidth pop NZ sub
     1 index length 1 sub NZ div 0
     3 2 roll ashow newpath} BD
/FL [
    /AvantGarde-Book ISOF
    /AvantGarde-BookOblique ISOF
    /AvantGarde-Demi ISOF
    /AvantGarde-DemiOblique ISOF
    /Bookman-Demi ISOF
    /Bookman-DemiItalic ISOF
    /Bookman-Light ISOF
    /Bookman-LightItalic ISOF
    /Courier ISOF
    /Courier-Bold ISOF
    /Courier-BoldOblique ISOF
    /Courier-Oblique ISOF
    /Helvetica ISOF
    /Helvetica-Bold ISOF
    /Helvetica-BoldOblique ISOF
    /Helvetica-Narrow ISOF
    /Helvetica-Narrow-Bold ISOF
    /Helvetica-Narrow-BoldOblique ISOF
    /Helvetica-Narrow-Oblique ISOF
    /Helvetica-Oblique ISOF
    /NewCenturySchlbk-Bold ISOF
    /NewCenturySchlbk-BoldItalic ISOF
    /NewCenturySchlbk-Italic ISOF
    /NewCenturySchlbk-Roman ISOF
    /Palatino-Bold ISOF
    /Palatino-BoldItalic ISOF
    /Palatino-Italic ISOF
    /Palatino-Roman ISOF
    /Symbol findfont
    /Times-Bold ISOF
    /Times-BoldItalic ISOF
    /Times-Italic ISOF
    /Times-Roman ISOF
    /ZapfDingbats findfont
    /ZapfChancery-MediumItalic ISOF
] D
/F {
         FL exch get exch scalefont
         [1 0 0 -1 0 0] makefont setfont} BD
%%EndProlog

1 个答案:

答案 0 :(得分:2)

如果PS文件中缺少%%EndComments,Ghost4j将中止。以下不那么漂亮的方法解释并修复了问题。

public byte[] fixPS(ByteArrayOutputStream out) {
    byte[] in = out.toByteArray();

    byte[] comments = "%%EndComments\n".getBytes(StandardCharsets.US_ASCII);
    byte[] skip = "%!PS-Adobe-3.0\n".getBytes(StandardCharsets.US_ASCII);

    byte[] ret = new byte[in.length + comments.length];
    System.arraycopy(skip, 0, ret, 0, skip.length);
    System.arraycopy(comments, 0, ret, skip.length, comments.length);
    System.arraycopy(in, skip.length, ret, skip.length + comments.length, in.length - (skip.length + comments.length));
    return ret;
}