I want to use print product information on label printer. I have Zebra TLP 2844 This printer support EPL2 programming language. When I want to print non-ASCII character printer just print "?" instead of the correct character. What I did?
First I created a simple text file with the below content
əğüçşıö
and press CTRL+P to print. Output is ok printer print all characters normally.
But when I use EPL2 commands like below
N
A220,120,0,4,1,1,N,"əğçşıüö"
P1
My printer gives the below output
??ç??üö
How can I solve this problem? I use java print api to send command to my printer.
And this my code
PrinterJob pj = PrinterJob.createPrinterJob();
Doc doc = new SimpleDoc(sb.toString().getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
boolean result = false;
javax.print.PrintService printService = PrintServiceLookup.lookupPrintServices(null, null)[0];
try {
printService.createPrintJob().print(doc, null);
} catch (PrintException e) {
e.printStackTrace();
}
PrinterJob pj = PrinterJob.createPrinterJob();
Doc doc = new SimpleDoc(sb.toString().getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
boolean result = false;
javax.print.PrintService printService = PrintServiceLookup.lookupPrintServices(null, null)[0];
try {
printService.createPrintJob().print(doc, null);
} catch (PrintException e) {
e.printStackTrace();
}
Its not an issue with the code it's an issue with the printer. Most printers don't include those characters in their standard font set. You either need to use the character's hex to call them or, if they're not included on the font set on the printer, download a different font.
Related
I am trying to print a pdf document using java.awt.print.PrinterJob.print (PrintRequestAttributeSet attributes). Before that, I put the necessary attributes in attributes using PrinterJob.printerDialog (attributes) and they really get there, but from everything I've tried, only the change in the number of pages that will be printed has been applied. The rest of the settings are ignored. How can I get the printer to print with the settings I want?
pdfReport - byte[] which was obtained with DatatypeConverter.parseBase64Binary from
printerService - standart "Microsoft Print to PDF"
PDDocument document = PDDocument.load(pdfReport);
try {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(printer.printerService);
HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
if (job.printDialog(attributes)) {
try {
job.print(attributes);
} catch (PrinterException e) {
System.out.println(e);
}
}
} finally {
document.close();
}
P.S. : when the program is running, sun.awt.windows.WPrinterJob is used as the PrinterJob implemenjatation
I've code up and running for sending Blob-Objects to a printer using javax.print. Everything works fine, except in some cases only the first page of multipage PDF-files is printed. The file looks normal when opened in acrobat and when I take the OutputStream that is used to feed the printjob and save it to another file I get and exact copy - so the stream seems to be okay. I have no idea why the following pages are dropped in some cases. here's my code:
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
aset.add(OrientationRequested.PORTRAIT);
aset.add(Sides.ONE_SIDED);
aset.add(MediaSizeName.ISO_A4);
aset.add(new JobName("Sammelausdruck", null));
ArrayList<String> fehlerDokumente = new ArrayList<String>();
//some standard print dialog
PrintService printService = ServiceUI.printDialog(null, 50, 50,
PrintServiceLookup.lookupPrintServices(null, aset), PrintServiceLookup.lookupDefaultPrintService(),
null, aset);
if (printService != null) {
DocPrintJob docjob = printService.createPrintJob();
aset.remove(JobName.class);
aset.add(new JobName(dok.getDokbezeichnung(), null));
File file = <some PDF-file>;
try {
FileInputStream psStream = new FileInputStream(file);
if (file.exists() && !file.isDirectory()) {
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
SimpleDoc doc = new SimpleDoc(psStream, psInFormat, null);
docjob.print(doc, aset);
}
} catch (FileNotFoundException e) {
...
} catch (PrintException e) {
...
}
}
}
Any ideas? I'm even interested in debug-ideas.
thank you
Edit:
In some very rare cases the printer (a professional office-printer with a touchscreen) provides an error-message that no matching paper is found for the given page-size and the pagesize shown is slightly off Din-A4 (210,2 x 295,8 or somthing like that). In this cases, if i manually tell the printer to use Din-A4, the printjob continues. I don't know if that has a complete different cause or is just another peculiarity of the same problem. I also tried to fiddle around with the MediaPrintableArea Attribute, but that didn't help either.
I want to print directly using a line printer i.e. a dot matrix printer using its features of font tab carriage return and line feed from my JAVA program. I basically know how to print from JAVA. My problem is that in JAVA printing we first generate the graphic image of the page to be printed and then send it to the printer to be printed. But I am not asking my question on those lines. I want to directly send the text as a stream of characters to the printer with the applicable commands for the printer for carriage return, Line feed, tabs and font of the printer just as in the old days when graphic printers like the laser or the inkjet printer were not in use.
I shall be very grateful if someone could guide me on these points. Thanks in advance.
Additional Info
Some of the comments are suggesting simple method of printing from a JTextComponent. Here we do not have to go through the task of creating the graphical printable which is automatically handled by the the JTextComponent, but my question is how to print without creating a graphical printable. Which means that first I select the font to use from the available fonts in my printer say "courier" and then I sent 'A' to the printer and the printer prints 'A' in "courier", then when I sent 'B' to the printer the printer prints 'B' in "courier" and so in till I change the selected font in my printer. Now at the end on the line, I sent \n for linefeed which will advance the roller drum of my printer by one line and \r for carriage return which will bring my printer's printing head to the beginning of the line.
For clarification I do not want to use printable interface, as the print method of this interface basically is used to generate a graphic image using the graphics object that is being passed as parameter to the print method. After this the JVM sends this graphics object to the printer to be printed as an image. This is not what I want. I want to use the line-printer's features of font and other commands.
This code doesn't require any Swing related component but still it needs Graphics class of awt, but you can print a text from console there is no UI component being displayed, just tested it:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PageRanges;
public class DirectPrint implements Printable {
private PrintService[] printService;
private String text;
public DirectPrint() {
this.printService = PrinterJob.lookupPrintServices();
}
public static void main(String[] args) {
DirectPrint lt = new DirectPrint();
lt.printString("If this text gets printed, it will have worked! ;D");
}
public void printString(String input) {
this.text = input;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new PageRanges(1, 1));
aset.add(new Copies(1));
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
try {
printJob.setPrintService(getDefaultPrintService());
//index of installed printers on you system
//not sure if default-printer is always '0'
printJob.print(aset);
} catch (PrinterException err) {
System.err.println(err);
}
}
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
g2.translate(pf.getImageableX(), pf.getImageableY());
g.drawString(String.valueOf(this.text), 14, 14);
return PAGE_EXISTS;
}
}
The method getDefaultPrintService() may return null, depending on your system.
Source: CodeRanch
** EDIT **
After further clarification, using the code below, there is no Graphic object being involved.
InputStream in = null;
try {
log.debug("preparing input stream");
in = getFileTobePrinted();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// find the printing service
log.debug("fetching print service");
AttributeSet attributeSet = new HashAttributeSet();
attributeSet.add(new PrinterName("lq2170", null));
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributeSet);
// create the print job
log.debug("creating print job");
DocPrintJob job = services[0].createPrintJob();
Doc doc = new SimpleDoc(in, flavor, null);
// monitor print job events
log.debug("preparing print job monitor");
PrintJobWatcher watcher = new PrintJobWatcher(job);
// print it
log.debug("start printing");
job.print(doc, null);
// wait for the print job is done
log.debug("waiting for the printing to finish");
watcher.waitForDone();
log.debug("done !");
} finally {
if (in != null) try { in.close(); } catch(Exception e) {}
}
Found Here
Have you tried to use This? but the rtextpr jar is Demo version and later you need to pay for licensed version.
How can I print duplex in java!?
Here's my code, but it didn't work - it only works in microsoft word (so the printer can do it)
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(2));
pras.add(Sides.DUPLEX);
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
Paper temp = pf.getPaper();
temp.setImageableArea(1, 3, temp.getWidth(), temp.getHeight());
pf.setPaper(temp);
if (pj.printDialog(pras)) {
try {
pj.setPrintable(this, pf);
pj.print(pras); // Drucken
return true;
} catch (Exception PrintException) {
.....
}
I tried to give only the attributes to print, only to the dialog, etc. etc. etc.
It print's the document 2 times, but not duplex! (also tried Sides.TWO_SIDED_LONG_EDGE..)
The Problem was my printer,... it always turn the option for duplex to be disabled.
Hey everyone ,
I'm facing a real problem here while trying to print a pdf file using java print .
The problem is that when i send the file to the printer using print() method with cute pdf the file is well printed but with a real printer it couldn't be done .
`
try{
File file = new File(toprint);
InputStream is = new BufferedInputStream(new FileInputStream(file));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob job = p.createPrintJob();//p here is my printservice printer
HashPrintRequestAttributeSet printRequestSet = new HashPrintRequestAttributeSet();
HashDocAttributeSet ds=new HashDocAttributeSet();
Doc doc = new SimpleDoc(is, flavor, null);
job.print(doc, aset);
}
catch(Exception e){
System.out.println("An exception occured while printing the file "+ e);
}
`
I've tried it so many times but it doesn't work.
Any ideas?
I think your printer may not have support for pdf, in this case you will have to render it using a pdf renderer.
Look at http://java.net/projects/pdf-renderer and pageable print page.
If you find it helpful I will provide code samples.