1 Reply Latest reply: Sep 22, 2010 1:41 AM by ukmSoftDev
ukmSoftDev Level 1 Level 1 (0 points)
Hi All,

I'm hoping someone could assist me with this. After googling like crazy for a solution I thought I had to post here on the forums. I'm a new Mac user (only a few days now) so please bear with me. I will also post this on the Java Forums because I don't know if this is a problem with my code or with the Mac settings.

Setup

I've got a Java Applet running on the mac (written in Eclipse 64 bit) on a Mac with OS X 10.6.4 on. Also, Java 1.6.0_21 (both 32 bit and 64 bit) is installed on the machine. The printer installed is a ZPL Label Printer (Zebra Technologies ZTC GK420d).
If I use another application such as TextEdit to print, it works fine.

*Problem Description*

Whenever I try print from the Applet nothing comes from the printer, my code does not break or catch any exception at all but the printer queue gives the error: *pstopdffilter/pstocupsraster failed with err number -31000.*

Code

The code I use with this Java Applet works absolutely fine on Windows XP 32 bit (IE 7.0, Google Chrome and Firefox) as well as on Windows 7 64 bit (I.E 8.0, Google Chrome and Firefox) but never on the Mac. For any developers who might be able to help, the code is below:


import java.applet.Applet;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.PrintServiceAttribute;
import javax.print.attribute.standard.PrinterName;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;

public class UKMJavaPrintApplet extends Applet
{

public void init()
{
System.out.println("Initialising UK Mail Java Printing Applet...");
}

public void start()
{
System.out.println("Starting UK Mail Java Printing Applet...");
PrintLabelZPL("^XA^FO100,40^BY3^B3,,30^FD123ABC^XZ","Zebra Technologies ZTC GK420d");
}

public void stop()
{
System.out.println("Stopping UK Mail Java Printing Applet...");
}

public void destroy()
{
System.out.println("Preparing unload of UK Mail Java Printing Applet...");
}

public int GetPrinterCount()
{
PrintService[] services = null;
int serviceLength = 0;

try
{
services = PrintServiceLookup.lookupPrintServices(null, null);

if(services != null && services.length > 0)
{
serviceLength = services.length;
}
}
catch(Exception z)
{
System.out.println("Failed to get printer count: " + z.getMessage());
z.printStackTrace();
}

return serviceLength;
}

public String GetPrinterName(int index)
{

String sPrinterName = null;
String printerName = "";
PrintService[] services = null;

try
{
services = PrintServiceLookup.lookupPrintServices(null, null);

for (int k=0; k < services.length; k++)
{
PrintServiceAttribute attr = services[k].getAttribute(PrinterName.class);
sPrinterName = ((PrinterName) attr).getValue();

if(index == k)
{
printerName = sPrinterName;
break;
}
}
}
catch(Exception r)
{
System.out.println("Failed to get printer name: " + r.getMessage());
r.printStackTrace();
}

return printerName;
}

public Boolean PrintLabelZPL(String zplString, String printerName)
{

String errStr = "";
Boolean boolSuccess = false;

try
{
if(printerName != null && printerName != "")
{
PrintService psZebra = null;
String sPrinterName = null;

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));

PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

for (int i = 0; i < services.length; i++)
{

PrintServiceAttribute attr = services.getAttribute(PrinterName.class);
sPrinterName = ((PrinterName) attr).getValue();

if (printerName.equals(sPrinterName))
{
psZebra = services
;
break;
}
}

if (psZebra != null)
{

DocPrintJob job = psZebra.createPrintJob();

byte[] by = zplString.getBytes();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(by, flavor, null);
System.out.println("Printing to: " + printerName);
job.print(doc, pras);
boolSuccess = true;
System.out.println("Printing Successful.");
}
else
{
errStr = "Zebra printer not found.";
System.out.println(errStr);
boolSuccess = false;
}
}
else
{
errStr = "No printer name was provided.";
System.out.println(errStr);
boolSuccess = false;
}

}
catch (PrintException e)
{
System.out.println("Print Failed with PrintException: " + e.getMessage());
boolSuccess = false;
e.printStackTrace();
}
catch (Exception f)
{
System.out.println("Print Failed: " + f.getMessage());
boolSuccess = false;
f.printStackTrace();
}

return boolSuccess;

}

}

Does anyone have any ideas on how I can resolve this and print to the ZPL printer from my Java Applet on the Mac?
Any help would be much appreciated.
Thanks!

Mac Mini Aluminum, Mac OS X (10.6.4), 2.4 Ghz Intel Core 2 Duo. 2 GB 1067 MHz DDR3. Macintosh HD.
  • ukmSoftDev Level 1 Level 1 (0 points)
    We managed to solve our own problem. This was not directly a Java Problem, but a problem with the setup of the Zebra printer on the Mac. Printing directly to the ZPL Printer never worked and always gave the error Printing Error - pstopdffilter/pstocupsraster failed with err number -31000.

    To solve this, we had to use the CUPS (Common Unix Printing System) interface. CUPS is apparently installed standard with every Mac. Being a new Mac user I didn't even know this a week ago. We had to use CUPS to add the printer, set it to be a "RAW" printer and then add something called a Class. Now when printing from the Java Applet, we DO NOT print to the actual Zebra printer, we print to the Class.

    Here are the steps on how to accomplish all this:


    1. As an Admin user, log onto Cups using the Safari browser by entering *http://localhost:631* into the Address Bar.
    2. Select the Administration tab and click, ‘Add Printer’
    3. Select the required local printer and then ‘Continue’
    4. Enter a Name, Description, Location and uncheck the ‘Share This Printer’ checkbox, where:
    a. Name = Queue Name (e.g. Zebra_RAW)
    b. Description = Human Readable Description (e.g. Zebra1)
    c. Location = Human Readable Location (e.g. My Mac mini)
    5. Click ‘Continue’
    6. Click ‘Select Another Make/Manufacturer’ and select ‘Raw’. Click ‘Continue’
    7. Click ‘Add Printer’
    8. Check that Banners are ‘none’ and Policies are ‘stop-printer’ (Error) and ‘default’ (Operation)
    9. Click ‘Set Default Options’
    10. Under the Administration tab, click Add Class.
    11. Enter a Name, Description, Location and select the Members:
    a. Name = Queue Name (e.g. ZebraRAWClass)
    b. Description = Human Readable Description (e.g. ZebraRaw)
    c. Location = Human Readable Location (e.g. My Mac mini)
    d. Members = the Queue Name of the new raw printer
    12. The only visible printers on the list then will be your non-raw printer/s and the printer class which you just created.
    13. Now when printing from Java Code, you must use the class as the printer in order to print raw ZPL.

    So in my example code in the previous post, I would print like this: PrintLabelZPL("^XA^FO100,40^BY3^B3,,30^FD123ABC^XZ","ZebraRaw");

    And it works! I hope this can help others with the same / similar problem.