使用 Java 从 PDF 转换为 Postscript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2072835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert from PDF to Postscript using Java
提问by Aniket Bharambe
I am having a J2EE based application, where I am using a reporting tool to generate outputs in PDF format. I need a utility/ tool in Java which can help me convert my PDF file to postscript format so that I can print it to a printer... My application will be running on a variety of OS (i.e. Windows / Linux / AIX) hence a platform independant solution is essential. My reporting tool does not support outputs in PS format. Please advice...
我有一个基于 J2EE 的应用程序,我在其中使用报告工具生成 PDF 格式的输出。我需要一个 Java 实用程序/工具,它可以帮助我将我的 PDF 文件转换为 postscript 格式,以便我可以将其打印到打印机......因此我的应用程序将在各种操作系统(即 Windows/Linux/AIX)上运行独立于平台的解决方案是必不可少的。我的报告工具不支持 PS 格式的输出。请指教...
回答by jarnbjo
There are AFAIK no fool proof pure Java solutions for this, but if the conversion is to be done server-side, I would recommend you to use Ghostscript'spdf2ps converter. Even if you have to install a platform specific version of Ghostscript, you should find one for all the mentioned platforms.
AFAIK 没有针对此问题的万无一失的纯 Java 解决方案,但如果要在服务器端完成转换,我建议您使用Ghostscript 的pdf2ps 转换器。即使您必须安装特定于平台的 Ghostscript 版本,您也应该为所有提到的平台找到一个。
回答by Dustin Sun
Here is my pure Java solution. It works well on CentOS, SUSE, and Windows 7. And no installation of any software is needed.
这是我的纯 Java 解决方案。它在 CentOS、SUSE 和 Windows 7 上运行良好。无需安装任何软件。
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();
}
}
}
Dependency:
依赖:
dependencies {
compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.8'
}
回答by Puneet Chachra
Thanks, I have tried this option and very well able to convert pdf to postscript. The only problem I am facing is, when I am sending such file to printer and if the pdf is having mixed orientation like some pages in portrait and some are in landscape, then converted postscript file is getting printed either in portrait mode or in landscape mode. It should handle orientation automatically while printing.
谢谢,我已经尝试过这个选项,并且能够很好地将 pdf 转换为 postscript。我面临的唯一问题是,当我将此类文件发送到打印机时,如果 pdf 具有混合方向,例如某些页面为纵向而某些页面为横向,则转换后的 postscript 文件将以纵向模式或横向模式打印. 它应该在打印时自动处理方向。
Code snippet for print ps file -
打印 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();
}
回答by mark stephens
There are several PDF libraries which can print PDFs. If you print to a Postscript Printer and use the print to file option, you could end up with Postscript.
有几个可以打印 PDF 的 PDF 库。如果您打印到 Postscript 打印机并使用打印到文件选项,您最终可能会使用 Postscript。
回答by Fabrizio Accatino
Another solution "exe based", like Ghostscript, is Xpdf.PdfToPS
PdfToPs is a pure commandline application. Runs on Win, Linux and Solaris.
http://www.foolabs.com/xpdf/
另一种“基于exe”的解决方案,如Ghostscript,是Xpdf.PdfToPS
PdfToPs 是一个纯命令行应用程序。在 Win、Linux 和 Solaris 上运行。
http://www.foolabs.com/xpdf/
回答by simon
Using fop xslfo http://xmlgraphics.apache.org/fop/fop-pdf-images.html
使用 fop xslfo http://xmlgraphics.apache.org/fop/fop-pdf-images.html
fop test.fo -ps out.ps
fop test.fo -ps out.ps
test.fo:
测试.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>
回答by Juraj ?uti?
Maybe this code can help:
也许此代码可以帮助:
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();
}
回答by Gordon
Maybe consider using jasper reports.
也许可以考虑使用jasper 报告。
It will allow you to generate a report in a variety outputs (PDF, Html, RTF).
它将允许您以各种输出(PDF、Html、RTF)生成报告。
You can also export the report to a printer.
您还可以将报告导出到打印机。
Edit:
编辑:
Here is a quick exampleI found to export to a printer using JRPrintServiceExporteras opposed using a PDF exporter. It looks like it will do the job.
这是我发现使用JRPrintServiceExporter而不是使用 PDF 导出器导出到打印机的快速示例。看起来它会完成这项工作。

