1

When trying to print a JPEG file with the Java code below on Mac OS X 10.8.0 I get the error message:

Error: pstopdffilter/pstocupsraster failed with err number 31000

A Gooogle search brings suggestions that seem to hint that the problem is not directly to Java e.g. 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();
}
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • Does this happen with *all* JPEG jobs you print, or just with *some*? – Kurt Pfeifle Oct 19 '12 at 08:34
  • I've removed the bounty, due to signs of collusion. I recommend bringing it up on [Meta] if this is not the case, but the acceptance of a code-only answer within a minute of it being posted (11 seconds, it seems) is very suspicious. – casperOne Oct 22 '12 at 14:18
  • fully ok. I have started a meta discussion recently on the issue of posting known answers. – Wolfgang Fahl Oct 22 '12 at 14:59
  • @WolfgangFahl What it seems you are acknowledging has been done here is *not* OK at all; you should have posted the answer under the same account as posted the question. Instead you have artificially gained reputation on a second account. – Andrew Barber Oct 22 '12 at 16:31
  • no - we have replicated the true situation. I had the problem my son has solved it. – Wolfgang Fahl Oct 22 '12 at 17:47

2 Answers2

2

As a workaround I would suggest to use jpeg instead of 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);
        }
    }
}
Martin Fahl
  • 937
  • 2
  • 12
  • 28
  • 2
    Boah... posting an answer and having it upvoted, accepted (and upvoted once more) within 11 seconds must be a new record. – Kurt Pfeifle Oct 19 '12 at 08:37
  • see meta discussion about posting known solutions of known problems ... Q&A format is IMHO no good for that. But most people seem to think differently. – Wolfgang Fahl Oct 21 '12 at 15:10
  • 1
    This is odd, how did you know in 11 seconds that this was the answer? It would seem that there is some collusion. This is not permitted. Is there a relationship between you and @WolfgangFahl? It seems on the surface that there is. – casperOne Oct 22 '12 at 14:12
  • Please see meta discussion. There are a few thousand articles on problems already solved within our company and somehow the q&a format and the rules here are an obstacle to share these. – Wolfgang Fahl Oct 22 '12 at 14:21
  • @WolfgangFahl Sharing such issues and their solutions is fine. But doing so in a way that artificially boosts the reputation of multiple users from the same entity is *not* OK. You should have posted the solution *yourself*, under the same account you used to post the question. – Andrew Barber Oct 22 '12 at 16:29
1

You say you're "trying to print a JPEG file".

However, the error message suggests that CUPS (the Mac OS X print subsystem) tries to run the printjob through either pstopdffilter (a utility that converts PostScript to PDF) or through pstocupsraster (a utility that converts PostScript to CUPS-raster).

You should first enable LogLevel debug (edit /private/etc/cups/cupsd.conf and restart the CUPS print service).

Then, look in the CUPS log file (*/var/log/cups/error_log*) for the lines containing the following strings:

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

Your findings could look similar to the following:

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.

I said 'similar to the following' because my crystal ball is in repair right now, and you didn't provide any additional details about your print environment setup, without which it will not be possible to analyze your problem any further.

Also, look in the log file for lines that indicate a PostScript error, for example:

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. ] %%

In which case your input file may be faulty, may be too big or whatever...

In general, it would be important to know:

  1. What did the "auto-typing" function of the print service return for the print job's mime type?
  2. What filters have been running (if any) in the filter chain before pstopdffilter (or pstocupsraster) came into play?
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345