使用Java将PDF转换为Postscript
我有一个基于J2EE的应用程序,我使用一个报告工具生成PDF格式的输出。我需要一个Java实用程序/工具,它可以帮助我将PDF文件转换为postscript格式,以便我可以将其打印到打印机上。。。我的应用程序将运行在各种操作系统(即Windows/Linux/AIX)上,因此独立于平台的解决方案至关重要。我的报告工具不支持PS格式的输出。请建议……< P/> < P>请考虑使用。 它将允许您以各种输出(PDF、Html、RTF)生成报告 也可以将报告导出到打印机 编辑:使用Java将PDF转换为Postscript,java,pdf,postscript,Java,Pdf,Postscript,我有一个基于J2EE的应用程序,我使用一个报告工具生成PDF格式的输出。我需要一个Java实用程序/工具,它可以帮助我将PDF文件转换为postscript格式,以便我可以将其打印到打印机上。。。我的应用程序将运行在各种操作系统(即Windows/Linux/AIX)上,因此独立于平台的解决方案至关重要。我的报告工具不支持PS格式的输出。请建议……< P/> < P>请考虑使用。 它将允许您以各种输出(PDF、Html、RTF)生成报告 也可以将报告导出到打印机 编辑: 下面是我找到的一个快速方
下面是我找到的一个快速方法,可以使用PDF导出器导出到打印机,而不是使用PDF导出器。看起来它可以完成这项工作。这方面没有简单的纯Java解决方案,但是如果转换是在服务器端完成的,我建议您使用pdf2ps转换器。即使您必须安装特定于平台的Ghostscript版本,您也应该为所有提到的平台找到一个。有几个PDF库可以打印PDF。如果打印到Postscript打印机并使用“打印到文件”选项,则最终可能会使用Postscript。另一种“基于exe”的解决方案,如Ghostscript,是Xpdf.PdfToPS
PdfToPs是一个纯命令行应用程序。在Win、Linux和Solaris上运行。
也许这段代码可以帮助:
try
{
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, psMimeType);
System.out.println ("Available PS services: " + factories.length);
if(factories.length == 0)
{
System.err.println ("No PS factories available!");
System.exit(0);
}
// Open the PDF file
PDFPrint pdfPrint = new PDFPrint ("test.pdf", null);
// Open the output file
FileOutputStream fos = new FileOutputStream("output.ps");
// Use the first service available
StreamPrintService sps = factories[0].getPrintService(fos);
DocPrintJob pj = sps.createPrintJob();
// Define paper size
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.NA_LETTER);
// Create simple doc using PDFPrint as Printable and print it
SimpleDoc doc = new SimpleDoc(pdfPrint, flavor, null);
pj.print(doc, aset);
// Close the output PS stream
fos.close();
}
catch (Throwable t)
{
t.printStackTrace();
}
使用fop-xslfo
fop test.fo-ps out.ps
test.fo:
<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simple">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:external-graphic src="my.pdf"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
这是我的纯Java解决方案。它在CentOS、SUSE和Windows7上运行良好。而且不需要安装任何软件
import java.io.File;
import java.io.FileOutputStream;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.SimpleDoc;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;
public class Printing {
public static void main(String[] args) {
try {
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, psMimeType);
System.out.println ("Available PS services: " + factories.length);
System.out.println ("Format: " + factories[0].getOutputFormat());
FileOutputStream outStream = new FileOutputStream("/path/to/your.ps");
StreamPrintService printService = factories[0].getPrintService(outStream);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.NA_LETTER);
PDDocument doc = PDDocument.load(new File("/path/to/my.pdf"));
SimpleDoc pdfDoc = new SimpleDoc(new PDFPrintable(doc, Scaling.SCALE_TO_FIT, false), flavor, null);
DocPrintJob newJob = printService.createPrintJob();
newJob.print(pdfDoc, aset);
outStream.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
依赖关系:
dependencies {
compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.8'
}
谢谢,我已经尝试过这个选项,并且能够很好地将pdf转换为postscript。我面临的唯一问题是,当我将此类文件发送到打印机时,如果pdf具有混合的方向,如纵向和横向的某些页面,则转换后的postscript文件将以纵向模式或横向模式打印。它应该在打印时自动处理方向 打印ps文件的代码段-
DocPrintJob job = service.createPrintJob();
try {
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
File psFile = new File (psFilePath);
InputStream inputStream = new FileInputStream(psFile);
Doc doc = new SimpleDoc(inputStream, DocFlavor.INPUT_STREAM.POSTSCRIPT ,null);
job.print(doc, attributes);
} catch (Exception e) {
e.printStackTrace();
}
这里有一个链接,可能对从Jasper生成PS有用:Thanx Gordon,谢谢你的帮助。。。事实上,我使用Jasper报告生成输出,但我找不到任何选项将输出导出到打印机。。。默认支持的格式不包括PSGhostscript,它是免费的,并且可以在所有平台上运行。在没有好的纯Java pdf->ps转换器的情况下,这是一种方法。