gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:53:52 27 4
gpt4 key购买 nike

我正在尝试将 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(新文件(输入文件));我跳过异常并加载具有以下格式的文档:

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

最佳答案

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

关于java - 在桌面应用程序java中使用Ghost4j将PS转换为PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28967326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com