Java-使用Ghostscript将字符串转换为post脚本文件的代码
我以前从未使用postscript。。。我需要更换一个工具:Java-使用Ghostscript将字符串转换为post脚本文件的代码,java,ghostscript,Java,Ghostscript,我以前从未使用postscript。。。我需要更换一个工具: 将字符串转换为postscript 基于postscript文件生成pdf文件(完成) 我的问题是:我不知道如何实现第一步。顺便说一句,我最好采用与步骤(2)类似的方法 我想知道是否可以替换参数,但是如何替换?你能帮助我吗 第(2)项的代码如下: public byte[] convertPostScriptToPDF() throws IOException { //get Ghostscript inst
public byte[] convertPostScriptToPDF() throws IOException {
//get Ghostscript instance
Ghostscript gs = Ghostscript.getInstance();
File file= new File (this.getClass().getResource( "/resources/employer_report_last_page2.ps").getFile());//(Config.EMP_REPORT.REPORT_LAST_PAGE_STORE_PATH);
File pdfGenerated = File.createTempFile("output", "pdf");
System.out.println("Path for temp file -> " + pdfGenerated.getAbsolutePath());
//prepare Ghostscript interpreter parameters
//refer to Ghostscript documentation for parameter usage
String[] gsArgs = new String[10];
gsArgs[0] = "-ps2pdf";
gsArgs[1] = "-dNOPAUSE";
gsArgs[2] = "-dBATCH";
gsArgs[3] = "-dSAFER";
gsArgs[4] = "-sDEVICE=pdfwrite";
// gsArgs[5] = "-sOutputFile=output2.pdf";//output file name
gsArgs[5] = "-sOutputFile=" + pdfGenerated.getAbsolutePath();
// gsArgs[5] = "-sOutputFile=" + file.getAbsolutePath();
gsArgs[6] = "-c";
gsArgs[7] = ".setpdfwrite";
gsArgs[8] = "-f";
// gsArgs[9] = "input.ps";//input file name
gsArgs[9] = file.getAbsolutePath();//input file name
//execute and exit interpreter
try {
gs.initialize(gsArgs);
gs.exit();
} catch (GhostscriptException e) {
System.out.println("ERROR: " + e.getMessage());
}
FileInputStream fis = new FileInputStream(pdfGenerated);
return IOUtils.toByteArray(fis);
}
提前谢谢你 我没有解决办法,所以我意识到我可以尝试一种解决方法 我更改了应用程序逻辑。最后,它会将所有内容转换为pdf格式,而不是:
- 将字符串转换为ps
- 添加其他ps内容
- 将ps转换为字符串
- 将字符串转换为pdf
- 将ps转换为pdf
- 使用PDFMergerUtility将两者合并