javapostscriptghost4j

Using Ghost4j to convert PS to PDF in Desktop application java


I'm trying to convert a ps to pdf document and shows me the exception: java.io.IOException: PostScript document is not valid, the code is as follows:

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

On line document.load (new File (inputfile)); I jump the exception and am loading the document has the following format:

%!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

Solution

  • Ghost4j aborts if %%EndComments is missing in the PS file. The following not so beautiful method explains and fixes the problem.

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