Note that there are some explanatory texts on larger screens.

plurals
  1. POunable to print UTF8 document using Java Print Service API on windows
    text
    copied!<p>I have been trying to print a text document containing Russian letters using the Java Print Service API on windows OS, but no luck so far. The printer prints the file, but the unicode characters get garbled. </p> <p>While running the program, the VM argument <strong>-Dfile.encoding=utf8</strong> was specified</p> <h2>Following is the code</h2> <pre><code> import java.io.DataInputStream; import java.io.FileInputStream; import java.io.UnsupportedEncodingException; import javax.print.Doc; import javax.print.DocFlavor; import javax.print.DocPrintJob; import javax.print.PrintException; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.SimpleDoc; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.standard.Sides; public class PrintUtf8Doc { public static void main(String[] args) throws UnsupportedEncodingException { FileInputStream textstream=null; DataInputStream da=null; byte buff[]=new byte[1024*20]; try { textstream = new FileInputStream("D:\\test\\russian.txt"); da=new DataInputStream(textstream); da.read(buff); } catch (Exception e) { e.printStackTrace(); } if (textstream == null) { return; } // Set the document type DocFlavor myFormat = DocFlavor.BYTE_ARRAY.AUTOSENSE; // Create a Doc Doc myDoc = new SimpleDoc(buff, myFormat, null); // Build a set of attributes PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); aset.add(Sides.DUPLEX); System.out.println("Host Encoding: "+myFormat.hostEncoding); PrintService service = PrintServiceLookup.lookupDefaultPrintService(); DocFlavor flavs[]=service.getSupportedDocFlavors(); for(DocFlavor flavor: flavs){ System.out.println(flavor.toString()); } // Create a print job from one of the print services if (service!=null) { DocPrintJob printJob=service.createPrintJob(); try { printJob.print(myDoc, aset); } catch (PrintException e) { e.printStackTrace(); } } } } ------------------------------------------------------------------- The supported flavors are printed in the console Host Encoding: UTF8 image/gif; class="[B" image/gif; class="java.io.InputStream" image/gif; class="java.net.URL" image/jpeg; class="[B" image/jpeg; class="java.io.InputStream" image/jpeg; class="java.net.URL" image/png; class="[B" image/png; class="java.io.InputStream" image/png; class="java.net.URL" application/x-java-jvm-local-objectref; class="java.awt.print.Pageable" application/x-java-jvm-local-objectref; class="java.awt.print.Printable" application/octet-stream; class="[B" application/octet-stream; class="java.net.URL" application/octet-stream; class="java.io.InputStream" </code></pre> <hr> <p>I tried even changing the docFlavor to "application/octet-stream; class="java.io.InputStream" and passed the inputStream to the SimpleDoc() constructor, but still it was the same issue</p> <p>Looks like even if the platform encoding was set to UTF8, the Java Print Service on windows is not able to ode the bytes correctly.</p> <p>Is there any setting that needs to be done or corrected? Any suggestions would be of great help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload