1

在 Mac OS X 10.8.0 上尝试使用以下 Java 代码打印 JPEG 文件时,我收到错误消息:

Error: pstopdffilter/pstocupsraster failed with err number 31000

谷歌搜索带来的建议似乎暗示问题不直接出现在 Java 上,例如 http://forum.parallels.com/showthread.php?t=106337

/**
 * http://stackoverflow.com/questions/7843338/printing-a-tif-file
 * 
 * @param graphicFile
 * @throws PrintException
 * @throws IOException
 */
public void printGraphic(File graphicFile) throws PrintException,
        IOException {

    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    pras.add(new Copies(1));
    pras.add(Chromaticity.COLOR);
    pras.add(MediaSizeName.ISO_A4);
    PrintService pss[] = PrintServiceLookup.lookupPrintServices(
            DocFlavor.INPUT_STREAM.JPEG, pras);

    if (pss.length == 0)
        throw new RuntimeException("No printer services available.");

    PrintService ps = pss[0];
    System.out.println("Printing to " + ps);
    DocPrintJob job = ps.createPrintJob();
    FileInputStream fin = new FileInputStream(graphicFile);
    Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.JPEG, null);
    job.print(doc, pras);
    fin.close();
}
4

2 回答 2

2

作为一种解决方法,我建议使用 jpeg 而不是 tiff。

/**
 * http://stackoverflow.com/questions/5338423/print-a-image-with-actual-size
 * -in-java
 * 
 * @param graphicFile
 */
public void printG(File graphicFile) {
    final Image img = new ImageIcon(graphicFile.getAbsolutePath())
            .getImage();
    PrinterJob printJob = PrinterJob.getPrinterJob();
    Printable printable = new Printable() {
        public int print(Graphics graphics, PageFormat pageFormat,
                int pageIndex) throws PrinterException {
            if (pageIndex != 0) {
                return NO_SUCH_PAGE;
            }
            graphics.drawImage(img, 0, 0, img.getWidth(null),
                    img.getHeight(null), null);
            return PAGE_EXISTS;
        }
    };
    printJob.setPrintable(printable, getPageFormat());
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception prt) {
            handle(prt);
        }
    }
}
于 2012-10-19T07:20:09.257 回答
1

您说您正在“尝试打印 JPEG 文件”

但是,错误消息表明 CUPS(Mac OS X 打印子系统)尝试通过pstopdffilter(将 PostScript 转换为 PDFpstocupsraster的实用程序)或通过(将 PostScript 转换为 CUPS 光栅的实用程序)运行打印作业。

您应该首先启用LogLevel debug(编辑/private/etc/cups/cupsd.conf并重新启动 CUPS 打印服务)。

然后,在 CUPS 日志文件 (*/var/log/cups/error_log*) 中查找包含以下字符串的行:

Auto-typing file...
Request file type is
Started filter
Started backend
exited with
failed with

您的发现可能类似于以下内容:

D [...timestamp...] [Job 9] Auto-typing file...
D [...timestamp...] [Job 9] Request file type is image/jpeg.
I [...timestamp...] [Job 9] Started filter /usr/libexec/cups/filter/imagetops (PID 25690)
I [...timestamp...] [Job 9] Started filter /usr/libexec/cups/filter/pstops (PID 25691)
I [...timestamp...] [Job 9] Started filter /usr/libexec/cups/filter/pstopdffilter (PID 25694)
I [...timestamp...] [Job 9] Started backend /usr/libexec/cups/backend/2dir (PID 25695)
D [...timestamp...] PID 25690 (/usr/libexec/cups/filter/imagetops) exited with no errors.
D [...timestamp...] PID 25691 (/usr/libexec/cups/filter/pstops) exited with no errors.
E [...timestamp...] PID 25694 (/usr/libexec/cups/filter/pstopdffilter) failed with err number -31000.
D [...timestamp...] PID 25695 (/usr/libexec/cups/backend/2dir) exited with no errors.

我说“类似于以下内容”是因为我的水晶球现在正在维修中,而您没有提供有关打印环境设置的任何其他详细信息,否则将无法进一步分析您的问题。

此外,在日志文件中查找指示 PostScript 错误的行,例如:

D [...timestamp...] [Job 9] %%[ Error: ioerror; OffendingCommand: image ]%%
D [...timestamp...] [Job 9] 
D [...timestamp...] [Job 9] Stack:
D [...timestamp...] [Job 9] -dict-
D [...timestamp...] [Job 9] 
D [...timestamp...] [Job 9] %%[ Flushing: rest of job (to end-of-file) will be ignored ]%%
D [...timestamp...] [Job 9] 
D [...timestamp...] [Job 9] %%[ Warning: PostScript error. No PDF file produced. ] %%

在这种情况下,您的输入文件可能有问题,可能太大或其他...

一般来说,重要的是要知道:

  1. 打印服务的“自动键入”功能为打印作业的 MIME 类型返回了什么?
  2. 在pstopdffilter(或 pstocupsraster)发挥作用之前,过滤器链中运行了哪些过滤器(如果有) ?
于 2012-10-19T08:33:19.380 回答