Java Thermal Printer printing QRCode with Text - java

I am new to Java printing and wanted to to print a QRCode with a small number under it on a Thermal Printer (Brother QL-810W).
With my code I can print my QRCode, but I cannot figure out how to print a number or text, also I can't use ESC/P Commands for formatting.I tried using ByteArrays and escpos-coffe library but it won't work.
Code:
try {
DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
aset.add(MediaSizeName.INVOICE);
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService printService: services) {
if (printService.getName().equals("Brother QL-810W")) {
DocPrintJob pj = printService.createPrintJob();
FileInputStream fis = new FileInputStream("C:/Local/QRCode1.png");
Doc doc = new SimpleDoc(fis, flavor, null);
pj.print(doc, aset);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
Heres the code I tried to work with the escpos-coffe libary:
try {
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService printService: services) {
if (printService.getName().equals("Brother QL-810W")) {
PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
EscPos escpos = new EscPos(printerOutputStream);
escpos.writeLF("1235");
escpos.feed(5);
escpos.cut(EscPos.CutMode.FULL);
escpos.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
Thanks for helping me!

Related

Dual Side printing with DocPrintJob (Java)

I am having two byte[], one is for front another for back. I want to print a single card, dual sided with those 2 byte arrays. My code looks like as below:-
private void print(byte[] frontSideByte, byte[] backSideByte) {
PrintService printService = getPritnerService();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.PNG;
DocPrintJob job = printService.createPrintJob();
DocAttributeSet das = new HashDocAttributeSet();
das.add(Sides.DUPLEX);
das.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
das.add(new MediaPrintableArea(0.0f, 0, 53.98f*2, 85.6f*2, MediaPrintableArea.MM));
das.add(findOrientation(frontSideByte));
Doc doc = new SimpleDoc(frontSideByte, flavor, das);
try {
job.print(doc, null);
} catch (Exception ex) {
System.out.println(ex);
}
}

Exporting jasper report to zebra printer not printing it completely

I have the following code for printing an invoice to zt410-300dpi printer:
private static void jasperPrint56(JasperPrint jasperPrint, String printerName) throws JRException, IOException {
PrinterJob job = PrinterJob.getPrinterJob();
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet(new PrinterName(printerName, null));
PrintService[] printService = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet);
int selectedService = 0;
try {
job.setPrintService(printService[selectedService]);
} catch (Exception e) {
System.out.println(e);
}
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimplePrintServiceExporterConfiguration configuration = new SimplePrintServiceExporterConfiguration();
configuration.setPrintService(job.getPrintService());
configuration.setPrintRequestAttributeSet(printRequestAttributeSet);
configuration.setPrintServiceAttributeSet(printServiceAttributeSet);
configuration.setDisplayPageDialog(false);
configuration.setDisplayPrintDialog(false);
exporter.setConfiguration(configuration);
exporter.exportReport();
}
This doesn't print the complete report and is only printing about 30% of it.
In the jrxml file, I have defined page width and height as follows:
pageWidth="288" pageHeight="432"
Which is 4 in x 6 in after converting for 72 dpi of jasper
In the printer properties, I have set the page size as 4x6 as well.

File rendering for PDF file print

I tried below code for print the PDF file for
public static void main(String args[])
{
FileInputStream psStream = null;
try {
psStream = new FileInputStream("E://ssc exam.pdf");
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
// this step is necessary because I have several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++){
String svcName = services[i].toString();
System.out.println("service found: "+svcName);
if (svcName.contains("sfg")){
myPrinter = services[i];
System.out.println("my printer found: "+svcName);
break;
}
}
if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);
} catch (Exception pe) {
pe.printStackTrace();}
} else {
System.out.println("no printer services found");
}
}
}
but i got "PDF file not printed.128 MB of memory is required to enable direct PDF printing" error,So I decide to use PDF rendering for print the PDF.can anyone to help how to use PDFfile rendering concept in detail.

Can't print file to printer using java applet

i have created java applet class, to print text file to printer, but when i running this program is nothing happend, in my printer status is no activity...
this is my applet code (print function) :
public void testPrintDoPrivileged(){
AccessController.doPrivileged(new PrivilegedAction() {
#Override
public Object run() {
FileInputStream textStream;
try{
textStream = new FileInputStream("D:\\email_address.txt");
DocFlavor myFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(textStream, myFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
aset.add(Sides.ONE_SIDED);
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
System.out.println("Printing to default printer: "+ printService.getName());
DocPrintJob job = printService.createPrintJob();
job.print(myDoc, aset);
} catch( FileNotFoundException e1){
e1.printStackTrace(System.out);
System.err.println(e1);
} catch (PrintException ex) {
System.err.println(ex);
}
return null;
}
});
}
and then by accident, i unplug my usb printer, then i try to test print again...
in my printer(offline now) status is show a pending document.
this is the picture :
there something wrong in my code or else..?.
sorry for my english...
thank you before...
Edit :
the problem is in DocFlavor.
my code running well in linux OS...
because linux have much DocFlavor support...
i use this code to check DocFlavor:
PrintService printServices = PrintServiceLookup.lookupDefaultPrintService();
DocFlavor[] docF = printServices.getSupportedDocFlavors();
for(int i = 0; i < docF.length; i++){
System.out.println(docF[i].getMimeType());
}

Force target printer in Java

Is there a way to force the target printer in java, using HashPrintRequestAttributeSet ?
I don't want the user to be able to change the printer in the printdialog
Thanks
Had to figure this out the hard way, but for the future generations, here's some of my
code:
PrintService[] printServices;
PrintService printService;
PageFormat pageFormat;
String printerName = "Your printer name in Devices and Printers";
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(new PrinterName(printerName, null));
printServices = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet);
pageFormat = new PageFormat(); // If you want to adjust heigh and width etc. of your paper.
pageFormat = printerjob.defaultPage();
PrinterJob printerjob = PrinterJob.getPrinterJob();
printerjob.setPrintable(new Server(), pageFormat); // Server was my class's name, you use yours.
try {
printService = printServices[0];
printerjob.setPrintService(printService); // Try setting the printer you want
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Error: No printer named '" + printerName + "', using default printer.");
pageFormat = printerjob.defaultPage(); // Set the default printer instead.
} catch (PrinterException exception) {
System.err.println("Printing error: " + exception);
}
try {
printerjob.print(); // Actual print command
} catch (PrinterException exception) {
System.err.println("Printing error: " + exception);
}
My code to solve this :
String printerNameDesired = "My Printer";
PrintService[] service = PrinterJob.lookupPrintServices(); // list of printers
DocPrintJob docPrintJob = null;
int count = service.length;
for (int i = 0; i < count; i++) {
if (service[i].getName().equalsIgnoreCase(printerNameDesired)) {
docPrintJob = service[i].createPrintJob();
i = count;
}
}
PrinterJob pjob = PrinterJob.getPrinterJob();
pjob.setPrintService(docPrintJob.getPrintService());
pjob.setJobName("job");
pjob.print();
I just solved this problem by running cmd command in Java
static void changeWindowsDefaultPrinter(String printerName) {
String cmdLine = String.format("RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n \"%s\"", printerName);
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", cmdLine );
builder.redirectErrorStream(true);
Process p = null;
try { p = builder.start(); }
catch (IOException e) { e.printStackTrace(); }
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = new String();
while (true) {
try {
line = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (line == null) { break; }
System.out.println( "result " + line);
}
}
And It's Wroked for Me :D

Categories

Resources