简体   繁体   中英

Using a custom java.awt.print.Paper with Brother QL label printers

I have a problem with my Brother QL-720NW label printer and java. As i figured out thru this comment , using java.awt.print.Paper does not work. The provided workaround is incomplete and I am relatively new to java printing.

There is a part missing in the code workaround.

Printable p = ...; // Create a printable object

Now, I tried to create a printable object and my code looks like this:

    public static void main(String[] args) throws FileNotFoundException{

        // Lookup printer by name 
PrintService[] foundPrinters = PrintServiceLookup.lookupPrintServices(null, new HashAttributeSet(new PrinterName("Brother QL-720NW", null)));
PrintService printer = foundPrinters[0];

// Lookup custom paper by name
MediaSizeName paper = null;
for (Media m : (Media[])printer.getSupportedAttributeValues(Media.class, null, null))
{
    if (m instanceof MediaSizeName)
    {
        MediaSizeName media = ((MediaSizeName)m);
        if ("Barcode".equals(media.toString())) paper = media;
    }
}

// Create a new printable object
InputStream input = new FileInputStream("C:/Temp/Barcodes/test.png");
Doc doc = new SimpleDoc(input, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

// Set custom paper as request attribute
PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
attrs.add(paper);

// Create a new print job and print it
DocPrintJob job = printer.createPrintJob();
try
{
    job.print(doc, attrs);
}
catch (PrintException ex)
{
    ex.printStackTrace();
}

    }

When I try to run the code, I am getting the error:

Exception in thread "main" java.lang.IllegalArgumentException: data is not of declared type

which seems to be related with this line:

Doc doc = new SimpleDoc(input, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

Searching stackoverflow got me to this comment . After trying to change DocFlavor.SERVICE_FORMATTED.PRINTABLE with DocFlavor.INPUT_STREAM.AUTOSENSE , i got the following error:

Exception in thread "main" java.lang.NullPointerException

which seems to be related to attrs.add(paper); .

I am running out of ideas what to do next and my knowledge in this matter is limited.

Can you please help me out? :)

EDIT

I updated my code with the suggestions from Arvind Kumar Avinash, but I still get an NullPointerException error. My code looks like this now:

    public static void main(String[] args) throws FileNotFoundException{

    // Lookup printer by name 
    PrintService[] foundPrinters = PrintServiceLookup.lookupPrintServices(null, new HashAttributeSet(new PrinterName("Brother QL-720NW", null)));
    PrintService printer = foundPrinters[0];

    // Lookup custom paper by namezusammenhang
    MediaSizeName paper = null;
    if(printer != null) {
        for (Media m : (Media[])printer.getSupportedAttributeValues(Media.class, null, null)) {
            if (m instanceof MediaSizeName) {
                MediaSizeName media = ((MediaSizeName)m);
                if ( media!=null && "Barcode".equals(media.toString())) 
                    paper = media;
            }
        }
    }

    // Create a new printable object
    InputStream input = new FileInputStream("C:/Temp/Barcodes/test.png");
    Doc doc = new SimpleDoc(input, DocFlavor.INPUT_STREAM.PNG, null);

    // Set custom paper as request attribute
    PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
    attrs.add(paper);

    // Create a new print job and print it
    DocPrintJob job = null;

    if (printer != null){
        job = printer.createPrintJob();
        try{
            if(job != null){
                job.print(doc, attrs);
            }

        }catch (PrintException ex){
            ex.printStackTrace();
        }
    }

}

The error / console output looks like this:

Exception in thread "main" java.lang.NullPointerException at java.desktop/javax.print.attribute.HashAttributeSet.add(HashAttributeSet.java:275) at easypackaging.TEST.main(TEST.java:45)

I have a problem with my Brother QL-720NW label printer and java. As i figured out thru this comment , using java.awt.print.Paper does not work. The provided workaround is incomplete and I am relatively new to java printing.

There is a part missing in the code workaround.

Printable p = ...; // Create a printable object

Now, I tried to create a printable object and my code looks like this:

    public static void main(String[] args) throws FileNotFoundException{

        // Lookup printer by name 
PrintService[] foundPrinters = PrintServiceLookup.lookupPrintServices(null, new HashAttributeSet(new PrinterName("Brother QL-720NW", null)));
PrintService printer = foundPrinters[0];

// Lookup custom paper by name
MediaSizeName paper = null;
for (Media m : (Media[])printer.getSupportedAttributeValues(Media.class, null, null))
{
    if (m instanceof MediaSizeName)
    {
        MediaSizeName media = ((MediaSizeName)m);
        if ("Barcode".equals(media.toString())) paper = media;
    }
}

// Create a new printable object
InputStream input = new FileInputStream("C:/Temp/Barcodes/test.png");
Doc doc = new SimpleDoc(input, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

// Set custom paper as request attribute
PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
attrs.add(paper);

// Create a new print job and print it
DocPrintJob job = printer.createPrintJob();
try
{
    job.print(doc, attrs);
}
catch (PrintException ex)
{
    ex.printStackTrace();
}

    }

When I try to run the code, I am getting the error:

Exception in thread "main" java.lang.IllegalArgumentException: data is not of declared type

which seems to be related with this line:

Doc doc = new SimpleDoc(input, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

Searching stackoverflow got me to this comment . After trying to change DocFlavor.SERVICE_FORMATTED.PRINTABLE with DocFlavor.INPUT_STREAM.AUTOSENSE , i got the following error:

Exception in thread "main" java.lang.NullPointerException

which seems to be related to attrs.add(paper); .

I am running out of ideas what to do next and my knowledge in this matter is limited.

Can you please help me out? :)

EDIT

I updated my code with the suggestions from Arvind Kumar Avinash, but I still get an NullPointerException error. My code looks like this now:

    public static void main(String[] args) throws FileNotFoundException{

    // Lookup printer by name 
    PrintService[] foundPrinters = PrintServiceLookup.lookupPrintServices(null, new HashAttributeSet(new PrinterName("Brother QL-720NW", null)));
    PrintService printer = foundPrinters[0];

    // Lookup custom paper by namezusammenhang
    MediaSizeName paper = null;
    if(printer != null) {
        for (Media m : (Media[])printer.getSupportedAttributeValues(Media.class, null, null)) {
            if (m instanceof MediaSizeName) {
                MediaSizeName media = ((MediaSizeName)m);
                if ( media!=null && "Barcode".equals(media.toString())) 
                    paper = media;
            }
        }
    }

    // Create a new printable object
    InputStream input = new FileInputStream("C:/Temp/Barcodes/test.png");
    Doc doc = new SimpleDoc(input, DocFlavor.INPUT_STREAM.PNG, null);

    // Set custom paper as request attribute
    PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
    attrs.add(paper);

    // Create a new print job and print it
    DocPrintJob job = null;

    if (printer != null){
        job = printer.createPrintJob();
        try{
            if(job != null){
                job.print(doc, attrs);
            }

        }catch (PrintException ex){
            ex.printStackTrace();
        }
    }

}

The error / console output looks like this:

Exception in thread "main" java.lang.NullPointerException at java.desktop/javax.print.attribute.HashAttributeSet.add(HashAttributeSet.java:275) at easypackaging.TEST.main(TEST.java:45)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM