Printing on different printers using java generate different results - java

I'm here to ask you a solution to this problem:
I have this code:
public void print(String ip, int port, String printService, byte[] message, String jobName) throws PrinterException{
PrintService service = getPrintService(printService);
try {
Doc doc = new SimpleDoc(message, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
DocPrintJob job = service.createPrintJob();
PrintRequestAttributeSet arset = new HashPrintRequestAttributeSet();
arset.add(convertMediaSize(mediaSize));
arset.add(Finishings.STAPLE);
arset.add(MultipleDocumentHandling.SEPARATE_DOCUMENTS_COLLATED_COPIES);
arset.add(convertPageSides(pageSides));
arset.add(new Copies(copies==0?1:copies));
job.print(doc, arset);
} catch (Exception e) {
throw new PrinterException(e);
}
}
the message is a byte array generated from a PDF.
When I execute this code printing on a Canon iR-ADV 400/500 PLC5e (setted as default printer) output is ok.
When I execute this code printing on a Canon iR-ADV c5235/5240 PCL5c, output is lots of pages full of strange chars:
strange chars
Both printers are network printers in the same network, if I try to print on both printers using word or Notepad or Acrobat Reader... output is ok.
I'm trying to print from Windows, not from Linux.
"message" is generated in this way (in my test)
File file = new File(inputFile);
byte[] b = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(b);
wp.print(null, 0, printService, b, null);
Any ideas?
thanks

Related

java prints only the first page of multipage pdf

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.

Generating Print .txt file directly using DocPrintJob for DotMatrix Printer

I am printing a .txt file using DocPrintJob. This works fine.
The file will be printed from a Dot Matrix Printer. Now, the issue which I am facing is -:
As per my requirement, the paper needs to set back automatically to
perforated position after printing one record.
But while printing directly from the application, the paper is not automatically setting back to perforated position.
But while giving print of file manually by opening in notepad, printing happens properly with automatic paper adjustment i.e. the paper sets back to the perforated position for next printing
I tried using line feeds and carriage returns at the end of each page. But it didn't work out.
Is there any way out to set the adjustment of printer through code?
Here is my sample code -:
private static void print(String fileName) throws FileNotFoundException, PrintException {
FileInputStream textStream;
textStream = new FileInputStream(fileName);
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc mydoc = new SimpleDoc(textStream, flavor, null);
// Set the printer Name
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(new PrinterName(service.getName(), null));
// Set the paper size and orientation
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
printRequestAttributeSet.add(new MediaPrintableArea(0, 0, 4, 9, MediaPrintableArea.INCH));
printRequestAttributeSet.add(OrientationRequested.LANDSCAPE);
InputStream is = null;
try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
DocPrintJob job = service.createPrintJob();
PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(mydoc, printRequestAttributeSet);
pjw.waitForDone();
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

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.

Printing a .TIF file

I am able to print a .GIF, .JPG or .PNG successfully using the following code snippet but it doesn't work for .TIF file. Also I can't get the color even after adding the chromaticity.color attribute.
public class PrintImage {
static public void main(String args[]) throws Exception {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
pras.add(chromaticity.color);
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
if (pss.length == 0)
throw new RuntimeException("No printer services available.");
PrintService ps = pss[0];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
String fileName = "C:/labels/2.tif"
FileInputStream fin = new FileInputStream(fileName);
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
job.print(doc, pras);
fin.close();
}
How do I support .TIF for printing?
Use Java Advanced Imaging API for TIFF. JAI can handle multipage TIFF files, JPEG in TIFF and a few compression schemes. If you still have trouble printing, with the API you could convert your TIFF file to PNG.

How to Convert from String into PDF in Java

Currently I am using this code but its throwing PrintJobFlavorException. This is my code help me out fixing this one:
public class PJUtil {
public static void main(String[] args) throws Exception {
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
Writer output = null;
String text = "printing in pdfPrinting in Java ";
File file = new File("C:\\CMPSup_AL_.PDF");
output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.close();
InputStream is = new BufferedInputStream(new FileInputStream(file));
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
is.close();
}
}
and exception is
Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Win32PrintJob.java:327)
at Collections.PrinterJobUtil.main(PrinterJobUtil.java:89)
your printer may not support text based representation. Have a look at this article java printing, specially page 5.
As other have pointed out, you can't just create a file called PDF and print it. If you need to generate PDF then you might take a look at itext.
Just to give you another option for creating PDF files. Try using Apache's PDFBox and take a look at the cookbook. The HelloWorld example shows you how to create a simple PDF document like the one you were trying to create in your sample code.
Also take a look on Jasper Reports http://community.jaspersoft.com/project/jasperreports-library
Change DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF to *DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE*.
E Pavan Varma

Categories

Resources