javax print not actually printing anything - java

Desperately trying to print a document using the following code. A document is being added to the printer queue but nothing actually comes out of the printer.
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService printService[] = PrintServiceLookup.lookupPrintServices(null, null);
InputStream stream = new ByteArrayInputStream("hello world!\f".getBytes("UTF8"));
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
DocPrintJob job = service.createPrintJob();
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(stream, flavor, das);
PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(doc, pras);
pjw.waitForDone();
The print dialog I get shows the correct default printer (as below), plus the list of other available printers:
If I change the printer to "Microsoft print to PDF", I get an empty (0kb) pdf. Interestingly, if I tick "Print to File" I get a prn file with the correct contents ("hello world!FF").
What am I missing?

Related

Java send txt file to Printer

I have made a program that generates text files (.txt) and then displays them in a textArea. I want to take this one step farther and be able to print the text file. It feels like I have tried everything but always get the same result. The text file always varies in length and is normally more than one page long. Using the code below my text file prints but instead of printing multiple pages it prints each page on one/sixth of the a single sheet in landscape. I just need it to print normally. Vertically on multiple pages. I am running on a Linux environment and it is an Epson printer. Any suggestions or feedback would be greatly appreciated.
public static void tryDoc(){
try{
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(OrientationRequested.PORTRAIT);
DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if(service != null){
DocPrintJob job = service.createPrintJob();
FileInputStream input = new FileInputStream(FileName);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(input,flavor,das);
job.print(doc, pras);
}
}
catch(Exception e){
System.out.println("Failed");
}
}
What printed page looks like

Thermal Printing in java using DocFlavor

I am new to DocFlavor java .
Anyone please help to start like I have simple text file and want to print on thermal printer ,which will be suitable DocFlavor type ?
It depends...If you want to print from a txt file then AUTO_SENSE or PostScript are ussually good ideas.
DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
or
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Here is a Snippet as an example.
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
FileInputStream in = new FileInputStream(new File("C:*PATH_TO_STRING_HERE"));
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(in, flavor, null);
DocPrintJob job = service.createPrintJob();
PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(doc, pras);
I got this code when i was trying to figure out how to use my thermal receipt printer...If you want a more thourough explanation. IF you need a more in depth answer...
Then go here !
NOTE if you need to print out charactesr such as ä, ö or å then this will propably not work well as they will be obscured... I do not know how to print out special characters.
Sincerly...
//Orville
For more info on docflavors..
Go here !

print a file in java

fis = new FileInputStream(file);
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; // FILE IS .txt TYPE
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//pras.add(MediaSizeName.ISO_A4);
//pras.add(new Copies(1));
PrintService printService[] =PrintServiceLookup.lookupPrintServices(flavor, pras);
System.out.println("Print Service:"+printService);
PrintService defaultService =PrintServiceLookup.lookupDefaultPrintService();
System.out.println("Default Service:"+defaultService);
PrintService service = ServiceUI.printDialog(null, 200, 200,printService, defaultService, flavor, pras);
if (service != null)
{
System.out.println("Selected Service"+service);
DocPrintJob job = service.createPrintJob();
job.addPrintJobListener(new MyPrintJobListener());
System.out.println("JOB:"+job);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
System.out.println("Start of Print");
job.print(doc, pras);
System.out.println("End of Print");
i=1;
}
else
{
i=0;
}
I'm Working on a web application where the user wishes to print the data which is saved in file which contains details from database as per his query.I'm placing the above code in a method and when this is invoked it gives a print dialog for user where he can select the printer from list of printers attached to his machine.
If i try to print to a local printer the files are being sent to C:\WINDOWS\system32\spool folder and goes for printing.
but it does not happen in the case of a network printer
my printer on the network is Canon MP280 series Printer
i'm able to see it in list of printer,but unable to print my file
the printer on the network is share or not?
if the printer is shared and in your pc / laptop has installed.
you must choice one of list array of print service name
e.g
PrintService defaultPrintservice = printServices[0];
if the printer not install in your PC / Laptop, you must set the path of location network printer
e.g
new PrinterName("ipp:\\\\witnw21va\\ipp\\ITDepartment-HP4050", null);
i hope it solve your problem :) sorry for my english :D

Correct way to send escape codes (raw data) to printer

In the context of a bigger application, my applet needs to print some data to a Zebra or a Dymo (depending on what the user has installed) label printer.
The data i receive is in an escaped form, data that i just need to send to the printer and let it interpret it.
Searching i've found two solutions.
Method 1:
byte[] printdata;
PrintService pservice = PrintServiceLookup.lookupDefaultPrintService(); //or get the printer in some other way
DocPrintJob job = pservice.createPrintJob();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(printdata, flavor, null);
and method 2:
PrintStream printStream = new PrintStream(new FileOutputStream(“LPT1”));
printStream.print(“Hello World”);
printStream.close();
I need this to work cross-platform, with printers using the USB or the serial port.
What is the correct way to implement this behaviour?
One problem with method 2 is that i would need to find the URL of the printer in same way...
public String rawprint(String printerName, String conte) {
String res = "";
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(new PrinterName(printerName, null));
PrintService printServices[] = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet);
if (printServices.length != 1) {
return "Can't select printer :" + printerName;
}
byte[] printdata = conte.getBytes();
PrintService pservice = printServices[0];
DocPrintJob job = pservice.createPrintJob();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(printdata, flavor, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
try {
job.print(doc, aset);
} catch(Exception e){
res = e.getMessage();
}
return res;
}
Works cool in javafx
Hex printouts are trustworthy. Call String.getBytes(encoding), then use System.out.format to print each byte as a hexadecimal number.

Print gif using java on a 4x6" paper

What is the best way in Java to print a gif given as byte[] or ByteArrayInputStream on a paper with a size of 4x6 inches?
This:
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new MediaSize(4, 6, Size2DSyntax.INCH));
aset.add(new Copies(1));
PrintService[] pservices =
PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, aset);
DocPrintJob printJob = pservices[0].createPrintJob();
Doc doc = new SimpleDoc(sap.getGraphicImageBytes(), DocFlavor.INPUT_STREAM.GIF, null);
printJob.print(doc, aset);
does not work because the MediaSize is not a PrintRequestAttribute. This should be almost the same as in Package javax.print Description
I found a way to solve my problem
PageFormat format = new PageFormat();
format.setOrientation(PageFormat.REVERSE_LANDSCAPE);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.REVERSE_LANDSCAPE);
aset.add(MediaSizeName.JAPANESE_POSTCARD);
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintable(new ImagePrintable(sap.getGraphicImage()));
printerJob.defaultPage(format);
printerJob.print(aset);
The trick was using japanese postcard as media size.

Categories

Resources