wood burning stoves 2.0*
The moose likes Java in General and the fly likes Unable to print Swedish Character using DocFlavor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unable to print Swedish Character using DocFlavor" Watch "Unable to print Swedish Character using DocFlavor" New topic
Author

Unable to print Swedish Character using DocFlavor

Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13
Hi All,

i am trying to print Swedish character using DocFlavor, but i am unable to do it.

My printer only supports below Flavors.

Win32 Printer : V2PRINTER supports :
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"


Below is code snippet

psStream = new FileInputStream("C:\temp.txt");
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
try
{
DocPrintJob job = printer.createPrintJob();
job.print(myDoc, aset);
psStream.close();
}

Please let me know if anybody has face same issue and has solution, i dont want to use Graphics2d classes because of printer performance issue.

Thanks in Advance.
Ravi




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

When you say "unable", what does that actually mean? Read our FAQ entry TellTheDetails for the sort of things we need to know.

Also, I notice that you are reading data from a file and then printing it, and assuming that the printing is the problem. You might consider not jumping to that conclusion; I notice you didn't specify a charset in your code which reads from the file.
Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13
Thanks Paul, I meant to say i am unable to print Swedish Character, Swedish Character are getting printed like some garbage characters. I am reading text from the file and trying to print it, in the file Characters are displaying correctly but while printing issue is coming.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
What do you mean by garbage? If you don’t supply enough details, how is anybody supposed to help?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

This is most likely a character encoding issue. What is the character encoding of the text file that you are trying to print? (for example UTF-8, ISO-8859-1, etc.)

I've never used the Java printing API, but in the documentation of class javax.print.DocFlavor I see this:
Default and Platform Encodings

For byte print data where the doc flavor's MIME type does not include a charset parameter, the Java Print Service instance assumes the US-ASCII character set by default. This is in accordance with RFC 2046, which says the default character set is US-ASCII. Note that US-ASCII is a subset of UTF-8, so in the future this may be widened if a future RFC endorses UTF-8 as the default in a compatible manner.

Also note that this is different than the behaviour of the Java runtime when interpreting a stream of bytes as text data. That assumes the default encoding for the user's locale. Thus, when spooling a file in local encoding to a Java Print Service it is important to correctly specify the encoding. Developers working in the English locales should be particularly conscious of this, as their platform encoding corresponds to the default mime charset. By this coincidence that particular case may work without specifying the encoding of platform data.

Every instance of the Java virtual machine has a default character encoding determined during virtual-machine startup and typically depends upon the locale and charset being used by the underlying operating system. In a distributed environment there is no gurantee that two VM's share the same default encoding. Thus clients which want to stream platform encoded text data from the host platform to a Java Print Service instance must explicitly declare the charset and not rely on defaults.

The preferred form is the official IANA primary name for an encoding. Applications which stream text data should always specify the charset in the mime type, which necessitates obtaining the encoding of the host platform for data (eg files) stored in that platform's encoding. A CharSet which corresponds to this and is suitable for use in a mime-type for a DocFlavor can be obtained from DocFlavor.hostEncoding This may not always be the primary IANA name but is guaranteed to be understood by this VM. For common flavors, the pre-defined *HOST DocFlavors may be used.

See character encodings for more information on the character encodings supported on the Java platform.


Make sure you create the DocFlavor with the correct character set, for example:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13
@Campbell, for example when i am trying to print "VÄLKOMMEN" in swedish it prints "V-LKOMMEN". it replaced 'Ä' with '-'. hope this clarified.

@Jasper - Thanks you, my file encoding is ANSI, i'll try to use different encodings in DocFlavor constructor and will update result.
Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13
PFB my test code, i tried all possible encoding format but getting Invalid Flavor Exception.




i have also attched file which i am trying to print.

Please can you look into it and tell me what wrong i am doing.
Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13
PFB text i am trying to print.


PURCHASE
SEK 560,00
Personal code

GODKÄNT

INFORMATION
0025412736000226832


Cards with
0-balance are
not printed

Detta är inte ett kvitto på ert köp
Spara Kvittot
Kundens Kopia
Terms and conditions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Ravi Rai wrote:@Jasper - Thanks you, my file encoding is ANSI, i'll try to use different encodings in DocFlavor constructor and will update result.

ANSI? I don't know a character encoding named "ANSI". Maybe you mean Windows-1252? I see you've changed the code and you're using ASCII. Note that the ASCII character set does not include characters such as Ä, å and ö. So your text file is most likely not encoded in ASCII.

You need to find out what the actual character encoding of your file is.
Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13
I am using below code to generate to File, which later i am using for printing.
in this code i have used UTF-8 encoding.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

If you use UTF-8 then ofcourse you also have to use UTF-8 when creating the DocFlavor object:
Ravi Rai
Greenhorn

Joined: Apr 15, 2011
Posts: 13
I already tried this approach. m getting Invalid Flavor exception.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to print Swedish Character using DocFlavor
 
Similar Threads
Emergency javax.print question ---- who can help me?? thank in advance.
Help me, please!!
Urgent. Please Help me!!!
Emergency javax.print question ---- who can help me?? thank in advance.
Emergency javax.print question ---- who can help me?? thank in advance.