B4J Question Printing to USB Label printer

Declan

Well-Known Member
Licensed User
Longtime User
I am using the jFX8Print Library Version 0.7
I am using a Xprinter XP-365B Thermal Label printer.
My Label size is 39mm(W) X 18mm(H)
On my Windows 10 PC, the Xprinter is set as "Default Printer"
The Printer Preferences has only 1 paper size set - 39mm(W) X 18mm(H)
The printer works great and I have tested it with the Xprinter Test software.
My B4J app has a Panel "pnlQR" which has 2 X imageviews and 2 X labels.
The imageviews display the QR Barcodes, the labels display a text.
I use:
B4X:
    Dim m As Image = MainForm.RootPane.Snapshot
    Dim cv As Canvas
    cv.Initialize("")
    cv.DrawImage(m,0,0,100,60)
to take a snapshot of pnlQR.
I must then send this to the printer with btnPrint_Click (No Dialogs):
B4X:
Private Sub btnPrint_Click
    Dim m As Image = MainForm.RootPane.Snapshot
    Dim cv As Canvas
    cv.Initialize("")
    cv.DrawImage(m,0,0,100,60)
  
    Dim P As Printer = Printer_Static.GetDefaultPrinter
'    Dim P As Printer = GetPrinter("Xprinter XP-365B")
    Dim PA As PrinterAttributes = P.GetPrinterAttributes
  
    Dim p_paper As Paper = PA.GetDefaultPaper
    Dim PJ As PrinterJob = PrinterJob_Static.CreatePrinterJob2(p)
    pl = p.CreatePageLayout(p_paper,"PORTRAIT",1,1,1,1) ' You should change orientation and margin If Neccessary
  
    PJ.GetJobSettings.SetCopies(1)
    PJ.GetJobSettings.SetPageLayout(pl)
    PJ.GetJobSettings.SetPrintQuality("HIGH")
    PJ.GetJobSettings.SetJobName("QR_PRINTER")
  
    Dim PJ As PrinterJob = PrinterJob_Static.CreatePrinterJob2(P)
    PJ.PrintPage(pnlQR)
    PJ.EndJob
  
End Sub

However, I have the following error:
B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 35 (Paper)
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4j.example.printer._createpagelayout(printer.java:80)
    at b4j.example.main._btnprint_click(main.java:143)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:234)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at com.sun.prism.j2d.print.J2DPrinter.getMediaSizeName(J2DPrinter.java:832)
    at com.sun.prism.j2d.print.J2DPrinter.printableArea(J2DPrinter.java:848)
    at javafx.print.Printer.createPageLayout(Printer.java:349)
    ... 28 more
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
If I send this to the printer with btnReprint_Click (With Dialogs):
B4X:
Private Sub btnReprint_Click
    Dim m As Image = MainForm.RootPane.Snapshot
    Dim cv As Canvas
    cv.Initialize("")
    cv.DrawImage(m,0,0,100,60)

    Dim P As Printer = Printer_Static.GetDefaultPrinter
'    Dim P As Printer = GetPrinter("Xprinter XP-365B")
    Dim PJ As PrinterJob = PrinterJob_Static.CreatePrinterJob2(P)
    PJ.ShowPageSetupDialog(Null)
    PJ.ShowPrintDialog(Null)
    PJ.PrintPage(pnlQR)
    PJ.EndJob

End Sub
I receive the following error:
B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 67 (PrinterJob)
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4j.example.printerjob._showpagesetupdialog(printerjob.java:105)
    at b4j.example.main._btnreprint_click(main.java:200)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:234)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at com.sun.prism.j2d.print.J2DPrinter.getMediaSizeName(J2DPrinter.java:832)
    at com.sun.prism.j2d.print.J2DPrinter.printableArea(J2DPrinter.java:848)
    at javafx.print.Printer.createPageLayout(Printer.java:249)
    at javafx.print.Printer.getDefaultPageLayout(Printer.java:221)
    at javafx.print.JobSettings.pageLayoutProperty(JobSettings.java:1097)
    at javafx.print.JobSettings.getPageLayout(JobSettings.java:1133)
    at com.sun.prism.j2d.print.J2DPrinterJob.syncPageLayout(J2DPrinterJob.java:615)
    at com.sun.prism.j2d.print.J2DPrinterJob.syncSettingsToAttributes(J2DPrinterJob.java:564)
    at com.sun.prism.j2d.print.J2DPrinterJob.showPageDialog(J2DPrinterJob.java:240)
    at javafx.print.PrinterJob.showPageSetupDialog(PrinterJob.java:343)
    ... 28 more
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
I have attached my app ZIP file.
 

Attachments

  • Printer.zip
    15 KB · Views: 325
Last edited:

stevel05

Expert
Licensed User
Longtime User
It looks like there may not be a default paper for the printer. what do you get if you Log(PA.GetDefaultPaper.GetObject) ?

you can also get a list of the supported papers using PrinterAttributes.GetSupportedPapers.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
what do you get if you Log(PA.GetDefaultPaper.GetObject) ?
I get the following:
B4X:
(Paper) Paper: USER size=78.7x101.6 MM
Then the Error:
B4X:
Waiting for debugger to connect...
Program started.
(Paper) Paper: USER size=78.7x101.6 MM
Error occurred on line: 54 (PrinterJob)
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4j.example.printerjob._showpagesetupdialog(printerjob.java:105)
    at b4j.example.main._btnreprint_click(main.java:207)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:234)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Bad margins
    at javafx.print.PageLayout.<init>(PageLayout.java:109)
    at javafx.print.Printer.createPageLayout(Printer.java:307)
    at javafx.print.Printer.getDefaultPageLayout(Printer.java:221)
    at javafx.print.JobSettings.pageLayoutProperty(JobSettings.java:1097)
    at javafx.print.JobSettings.getPageLayout(JobSettings.java:1133)
    at com.sun.prism.j2d.print.J2DPrinterJob.syncPageLayout(J2DPrinterJob.java:615)
    at com.sun.prism.j2d.print.J2DPrinterJob.syncSettingsToAttributes(J2DPrinterJob.java:564)
    at com.sun.prism.j2d.print.J2DPrinterJob.showPageDialog(J2DPrinterJob.java:240)
    at javafx.print.PrinterJob.showPageSetupDialog(PrinterJob.java:343)
    ... 28 more
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
This is a screenshot of the printer paper setups:
Default_Paper.png

"QR-39X18" is the paper I require.
you can also get a list of the supported papers using PrinterAttributes.GetSupportedPapers.
I get:
B4X:
(ArrayList) [Paper: 2 x 4 size=53.3x101.6 MM, Paper: USER size=78.7x101.6 MM]
My paper size "QR-39X18" does not show.

I still do not get the printer Dialogs:
B4X:
Waiting for debugger to connect...
Program started.
(Paper) Paper: USER size=78.7x101.6 MM
(ArrayList) [Paper: 2 x 4 size=53.3x101.6 MM, Paper: USER size=78.7x101.6 MM]
Error occurred on line: 54 (PrinterJob)
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at b4j.example.printerjob._showpagesetupdialog(printerjob.java:105)
    at b4j.example.main._btnreprint_click(main.java:210)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:234)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Bad margins
    at javafx.print.PageLayout.<init>(PageLayout.java:109)
    at javafx.print.Printer.createPageLayout(Printer.java:307)
    at javafx.print.Printer.getDefaultPageLayout(Printer.java:221)
    at javafx.print.JobSettings.pageLayoutProperty(JobSettings.java:1097)
    at javafx.print.JobSettings.getPageLayout(JobSettings.java:1133)
    at com.sun.prism.j2d.print.J2DPrinterJob.syncPageLayout(J2DPrinterJob.java:615)
    at com.sun.prism.j2d.print.J2DPrinterJob.syncSettingsToAttributes(J2DPrinterJob.java:564)
    at com.sun.prism.j2d.print.J2DPrinterJob.showPageDialog(J2DPrinterJob.java:240)
    at javafx.print.PrinterJob.showPageSetupDialog(PrinterJob.java:343)
    ... 28 more
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
B4X:
pl = p.CreatePageLayout(p_paper,"PORTRAIT",1,1,1,1) ' You should change orientation and margin If Neccessary

Caused by: java.lang.IllegalArgumentException: Bad margins

It looks like it can't handle margins of size 1. Try increasing it the error should disappear.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
A litle late but your XP-365B printer spec is:
high-capacity for paper, large diameter 82 mm can be put on label
And your working paper size is:
QR-39X18 (39.0 mm x 18.0 mm
Then total margin = 82 - 39 mm = 42 mm. Try to fool the printer by leaving a Left and Right Margin of say 2 mm for the right half that is not to be printed, leaving for the right side 82 mm physical width - (2 mm + 37 mm) = 39 mm label print space = 43 mm for the Right Margin to be copied. This should leave a 2mm margin on both sides of the label. When you align the label in the middle in the printer, you must use 43/2 as margin on both sides.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
When you align the label in the middle in the printer, you must use 43/2 as margin on both sides
My printer does not allow for the label to be loaded in the middle of the printer.
I am only able to load the label on the left side of the printer - this being the right side of the label in the travel direction.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
Thats fine that the the label is loaded on the left side of the printer. Then you can use my program code which suppose to prints its own nothing outside the label width. I hope it works. I'm curious.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Irrespective of how I change the margins with:
B4X:
pl = p.CreatePageLayout(p_paper,"PORTRAIT",40,0,0,0)
It does not alter the print position.
I have tried numerous permutations of the Left and Right margins.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
I was probably unclear, but a Left Margin 2 mm, a Right Margin 43 mm gives a label width of 37 mm with paper on the left side of de printer with 2 mm margin on both sizes of the label.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Not sure why you are trying to use the Windows driver.
I am using the Seagull Scientific driver, as supplied by the Printer manufacturer.
It's a label printer so has the ability to print QRCodes inbuilt. Why not just send the commands to printer to print the QR Codes? Your printer emulates TSPL which is the TSC / Kroy Printer language and that has a QRCODE command, You can find the language manual here:
@keirS
Do you possibly have an example code?
The manual is very extensive and I shudder at the learning-curve required.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
I was probably unclear, but a Left Margin 2 mm, a Right Margin 43 mm gives a label width of 37 mm with paper on the left side of de printer with 2 mm margin on both sizes of the label.
I have tried this:
B4X:
pl = p.CreatePageLayout(p_paper,"PORTRAIT",2,43,0,0)
But as I mentioned before, it does not change the print position on the printer.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
I am using the Seagull Scientific driver, as supplied by the Printer manufacturer.

@keirS
Do you possibly have an example code?
The manual is very extensive and I shudder at the learning-curve required.

Seagull drivers are of variable quality. I have had lots of experience with them and I would say they are generally rubbish. Anyway this should give you a good starting point.

B4X:
SIZE 39.00 mm,18.00 mm
DIRECTION 1,0
GAP 0 mm,0 mm
CODEPAGE 850
CLS
QRCODE 37,13,0,H,4,A,0,"QRCODE1"
QRCODE 255,13,0,H,4,A,0,"QRCODE2"
TEXT 37,172,"8",0,1,1,"QRCODE1"
TEXT 255,172,"8",0,1,1,"QRCODE2"
PRINT 1,1
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Seagull drivers are of variable quality. I have had lots of experience with them and I would say they are generally rubbish. Anyway this should give you a good starting point.

B4X:
SIZE 39.00 mm,18.00 mm
DIRECTION 1,0
GAP 0 mm,0 mm
CODEPAGE 850
CLS
QRCODE 37,13,0,H,4,A,0,"QRCODE1"
QRCODE 255,13,0,H,4,A,0,"QRCODE2"
TEXT 37,172,"8",0,1,1,"QRCODE1"
TEXT 255,172,"8",0,1,1,"QRCODE2"
PRINT 1,1
Many thanks,
Please bear with me...
Should I remove the Seagull printer driver from my PC?
Do I simply write the above code out to the printer on USB?
What exactly is the procedure :)
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Easiest way is to use the Serial port on the printer instead of the USB port and use the serial library. If you only have a USB port on the printer then it's slightly more tricky but usually doable.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Ok you have COM ports which makes it easier.

Add a new printer and select "Add a local printer or network printer with local settings"
Select "Use an existing port"
Select "USB001" from the dropdown
Select "Generic" from the list of manufacturers and "Generic / Text Only" from the list of printers
You can name the printer whatever you like
Share the printer

Next step is to redirect one of your COM ports to your label printer. Run a command prompt and use a command like this

B4X:
NET USE COM1: //MY_PC_NAME/MY_SHARED_PRINTER_NAME /persistent:yes

MY_PC_NAME = The name of your computer
MY_SHARED_PRINTER_NAME = The name of the printer share you have created.

You can use the jSerial library to write the label to the com port and that will write it to the printer.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Many, many thanks.
I send the following from the command prompt:
C:\>NET USE COM1://MSI/QR/persistent:yes
But get the following error:
System error 67 has occurred.
The network name cannot be found.

I have tried numerous web solutions to this, but all in vain.
 
Upvote 0
Top