Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Nov 25, 2010, 03:47 PM   #1
macrumors G4
wrldwzrd89's Avatar
Join Date: Jun 2003
Location: Solon, OH
[Java] Printing an internally generated BufferedImage object

The title says it all... I'm having trouble getting printing to work. Here's the printing code I'm using:

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
package net.worldwizard.lasertank;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.print.PrinterJob;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.JFrame;

 * @author wrldwzrd89
public class BoardPrinter {

    private BoardPrinter() {
        // Do nothing

    public static void printBoard(JFrame j) {
        try {
            Container c = j.getContentPane();
            Dimension d = c.getPreferredSize();
            BufferedImage bi = new BufferedImage(d.width, d.height,
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, "PNG", baos);
            byte[] data = baos.toByteArray();
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
            PrinterJob pj = PrinterJob.getPrinterJob();
            boolean okay = pj.printDialog(pras);
            if (okay) {
                PrintService service = pj.getPrintService();
                DocPrintJob job = service.createPrintJob();
                DocAttributeSet das = new HashDocAttributeSet();
                Doc doc = new SimpleDoc(bais, flavor, das);
                job.print(doc, pras);
        } catch (IOException ioe) {
            CommonDialogs.showErrorDialog("Printing failed!", "Print GameBoard");
        } catch (PrintException pe) {
            CommonDialogs.showErrorDialog("Printing failed!", "Print GameBoard");
        } catch (NullPointerException npe) {
            CommonDialogs.showErrorDialog("Printing failed!", "Print GameBoard");
The CommonDialogs class that's being invoked here is just a convenience class for displaying frequently used dialog boxes. Printing works on the Java end, but when it sends the data to the printer I get an error:
pstopdffilter/pstocupsraster failed with err number -31000
I'm not sure what's going on.

EDIT 2: Just tried the same printing code on my Windows 7 machine. It works there.
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, 10.8.4

Last edited by wrldwzrd89; Nov 25, 2010 at 05:44 PM. Reason: Changed the code; still does not work
wrldwzrd89 is online now   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: How do I convert an NSData object to an NSDictionary object, and vice versa? moonman239 iPhone/iPad Programming 6 May 2, 2014 01:17 PM
External HD not recognized when moved internally Doc69 Mac Pro 3 Dec 10, 2013 09:09 PM
Recording internally Tech198 Digital Audio 6 Dec 7, 2013 01:15 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
Obba 3.0.12: Java Object Handler for Spreadsheets (OpenOffice/LibreOffice/NeoOffice) mathematician Mac Applications and Mac App Store 2 Jun 10, 2012 03:45 AM

Forum Jump

All times are GMT -5. The time now is 12:02 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC