1

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

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
4

1 回答 1

3

如果%%EndCommentsPS 文件中缺少 Ghost4j,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;
}
于 2015-05-22T10:58:42.803 回答