anastaciocintra / escpos-coffee

Java library for ESC/POS printer
https://anastaciocintra.github.io/escpos-coffee
MIT License
283 stars 74 forks source link

ESc/POS arabic printing not working #44

Closed rajesh-shanthi closed 4 years ago

rajesh-shanthi commented 4 years ago

Hi,

I am trying to print arabic language and i get ????? as printed for the arabic text. The following is my code in java. public void Sample(String printerName) {

    // get the printer service by name passed on command line...
    // this call is slow, try to use it only once and reuse the PrintService
    // variable.
    PrintService printService = PrinterOutputStream.getPrintServiceByName(printerName);
    EscPos escpos;
    try {
        System.setProperty("file.encoding", "UTF-8");
        escpos = new EscPos(new PrinterOutputStream(printService));

        Style title = new Style().setFontSize(Style.FontSize._2, Style.FontSize._4)

                .setJustification(EscPosConst.Justification.Center);

        escpos.setCharacterCodeTable(CharacterCodeTable.WCP1256_Arabic);
        System.out.println(escpos.getDefaultCharsetName());
        String rawString = "الصفحة الرئيسية";
        escpos.write(rawString.getBytes("UTF-8"), 0, rawString.length());

        escpos.feed(5);
        // escpos.cut(EscPos.CutMode.FULL);

        escpos.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

Can you please help me, what is the problem for junk characters getting printed. I just opened a texpad and pasted some arabic content and saved file in utf-8 encoding and fired printout to my pos printer , it is prining the arabic characters

Regards, Rajesh.

JulioCesarQ commented 3 years ago

necesito ayuda estoy tratando de imprimir una imagen pero me salen otros caracteres

blackangiliq commented 2 years ago

u can use this

you can check this

this repo use 3 laiblary

secreenshot to convert widget to image and image library to convert it to uint8 and pos_print to print it ass u love to show

https://github.com/blackangiliq/flutter_pos_printer_spport_arabic

Amm1r-IT commented 2 years ago

Hi

Thanks for your effort and work. Really appreciated!

I copied the code you provided, but It did not work for me. The printer does not print anything. Then, I did what u said about figuring out the width and height in the ImageHelper class as it might cause an issue then it worked :)

So, I only added width and height

ImageHelper helper = new ImageHelper (384,58);

I am just sharing my experience as it could help others

So, thanks again

good try this:

import com.github.anastaciocintra.escpos.EscPos;
import com.github.anastaciocintra.escpos.image.*;
import image.ImageHelper;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.CharConversionException;
import java.io.IOException;

import static java.awt.image.BufferedImage.TYPE_INT_RGB;

public class CoffeeBitmap2 {

    public void exec(EscPos escpos) throws IOException {

        final int FontSize = 30;
//        final String arabicText = "الصفحة الرئيسية";
        final String arabicText = "تجربة تجربة";
        // 1 - create one buffered image with width and height
        BufferedImage image = new BufferedImage(576, 150, TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();

        // change background and foregroud colors
        g.setColor(Color.white);
        g.fillRect(0, 0, g.getDeviceConfiguration().getBounds().width, g.getDeviceConfiguration().getBounds().height);
        g.setColor(Color.BLACK);

        // choose your font
        //OBS: not all fonts work well with Arabic language
        // read more on https://docs.oracle.com/javase/tutorial/2d/text/advanced.html
        //
        // choose the font... below you have some combinations
        Font fontMonoSpacePlan = new Font (Font.MONOSPACED, Font.PLAIN, FontSize);
        Font fontMonoSpaceBold = new Font (Font.MONOSPACED, Font.BOLD, FontSize);
        Font fontMonoSpaceBoldItalic = new Font (Font.MONOSPACED, Font.ITALIC|Font.BOLD, FontSize);
        Font fontSerifPlan = new Font (Font.SERIF, Font.PLAIN, FontSize);
        Font fontSerifBold = new Font (Font.SERIF, Font.BOLD, FontSize);
        Font fontSansSerif = new Font (Font.SANS_SERIF, Font.PLAIN, FontSize);
        Font fontSansSerifBold = new Font (Font.SANS_SERIF, Font.BOLD, FontSize);
        // doc about canDisplayUpTo
        /**
         * Indicates whether or not this <code>Font</code> can display a
         * specified <code>String</code>.  For strings with Unicode encoding,
         * it is important to know if a particular font can display the
         * string. This method returns an offset into the <code>String</code>
         * <code>str</code> which is the first character this
         * <code>Font</code> cannot display without using the missing glyph
         * code. If the <code>Font</code> can display all characters, -1 is
         * returned.
         * @param str a <code>String</code> object
         * @return an offset into <code>str</code> that points
         *          to the first character in <code>str</code> that this
         *          <code>Font</code> cannot display; or <code>-1</code> if
         *          this <code>Font</code> can display all characters in
         *          <code>str</code>.
         * @since 1.2
         */

        // ..
        // you can test the font
        if(fontMonoSpaceBold.canDisplayUpTo(arabicText) != -1){
            throw new CharConversionException("the font doesn't work with these glyphs");
        }

        // set the font to be used
        g.setFont(fontMonoSpaceBold);

        // write your text
        g.drawString("hello Arabian Character", 1, 90);

        // other line ...
        g.drawString(arabicText, 1, 140);

        // send the graphic to the escpos printer...
        **ImageHelper helper = new ImageHelper();**
        Bitonal algorithm = new BitonalThreshold();
//        GraphicsImageWrapper imageWrapper = new GraphicsImageWrapper();
        RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper();
        helper.write(escpos, new CoffeeImageImpl(image),imageWrapper,algorithm);

        escpos.feed(5).cut(EscPos.CutMode.FULL);
        escpos.close();

    }

    public void printCodeTable(EscPos escpos, int codeTable, int codMin, int codMax) throws IOException {
        escpos.setCharacterCodeTable(EscPos.CharacterCodeTable.CP437_USA_Standard_Europe);
        escpos.writeLF(String.format("character code table for code [%d], " +
                "\nbetween %d and %d", codeTable,codMin, codMax));
        escpos.setPrinterCharacterTable(codeTable);
        for(int code = codMin; code <= codMax; code++){
            escpos.write(code);
            escpos.write("  ");

        }
        escpos.writeLF("");
        escpos.feed(5).cut(EscPos.CutMode.FULL);

    }
}
Hany1Gadallah commented 1 year ago

good try this:

import com.github.anastaciocintra.escpos.EscPos;
import com.github.anastaciocintra.escpos.image.*;
import image.ImageHelper;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.CharConversionException;
import java.io.IOException;

import static java.awt.image.BufferedImage.TYPE_INT_RGB;

public class CoffeeBitmap2 {

    public void exec(EscPos escpos) throws IOException {

        final int FontSize = 30;
//        final String arabicText = "الصفحة الرئيسية";
        final String arabicText = "تجربة تجربة";
        // 1 - create one buffered image with width and height
        BufferedImage image = new BufferedImage(576, 150, TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();

        // change background and foregroud colors
        g.setColor(Color.white);
        g.fillRect(0, 0, g.getDeviceConfiguration().getBounds().width, g.getDeviceConfiguration().getBounds().height);
        g.setColor(Color.BLACK);

        // choose your font
        //OBS: not all fonts work well with Arabic language
        // read more on https://docs.oracle.com/javase/tutorial/2d/text/advanced.html
        //
        // choose the font... below you have some combinations
        Font fontMonoSpacePlan = new Font (Font.MONOSPACED, Font.PLAIN, FontSize);
        Font fontMonoSpaceBold = new Font (Font.MONOSPACED, Font.BOLD, FontSize);
        Font fontMonoSpaceBoldItalic = new Font (Font.MONOSPACED, Font.ITALIC|Font.BOLD, FontSize);
        Font fontSerifPlan = new Font (Font.SERIF, Font.PLAIN, FontSize);
        Font fontSerifBold = new Font (Font.SERIF, Font.BOLD, FontSize);
        Font fontSansSerif = new Font (Font.SANS_SERIF, Font.PLAIN, FontSize);
        Font fontSansSerifBold = new Font (Font.SANS_SERIF, Font.BOLD, FontSize);
        // doc about canDisplayUpTo
        /**
         * Indicates whether or not this <code>Font</code> can display a
         * specified <code>String</code>.  For strings with Unicode encoding,
         * it is important to know if a particular font can display the
         * string. This method returns an offset into the <code>String</code>
         * <code>str</code> which is the first character this
         * <code>Font</code> cannot display without using the missing glyph
         * code. If the <code>Font</code> can display all characters, -1 is
         * returned.
         * @param str a <code>String</code> object
         * @return an offset into <code>str</code> that points
         *          to the first character in <code>str</code> that this
         *          <code>Font</code> cannot display; or <code>-1</code> if
         *          this <code>Font</code> can display all characters in
         *          <code>str</code>.
         * @since 1.2
         */

        // ..
        // you can test the font
        if(fontMonoSpaceBold.canDisplayUpTo(arabicText) != -1){
            throw new CharConversionException("the font doesn't work with these glyphs");
        }

        // set the font to be used
        g.setFont(fontMonoSpaceBold);

        // write your text
        g.drawString("hello Arabian Character", 1, 90);

        // other line ...
        g.drawString(arabicText, 1, 140);

        // send the graphic to the escpos printer...
        ImageHelper helper = new ImageHelper();
        Bitonal algorithm = new BitonalThreshold();
//        GraphicsImageWrapper imageWrapper = new GraphicsImageWrapper();
        RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper();
        helper.write(escpos, new CoffeeImageImpl(image),imageWrapper,algorithm);

        escpos.feed(5).cut(EscPos.CutMode.FULL);
        escpos.close();

    }

    public void printCodeTable(EscPos escpos, int codeTable, int codMin, int codMax) throws IOException {
        escpos.setCharacterCodeTable(EscPos.CharacterCodeTable.CP437_USA_Standard_Europe);
        escpos.writeLF(String.format("character code table for code [%d], " +
                "\nbetween %d and %d", codeTable,codMin, codMax));
        escpos.setPrinterCharacterTable(codeTable);
        for(int code = codMin; code <= codMax; code++){
            escpos.write(code);
            escpos.write("  ");

        }
        escpos.writeLF("");
        escpos.feed(5).cut(EscPos.CutMode.FULL);

    }
}

Please could you tell me how i can get this fonts for windows in otf or ttf extension

anastaciocintra commented 1 year ago

@Hany1Gadallah, take a look at Font.createFont(...) https://docs.oracle.com/javase/8/docs/api/java/awt/Font.html

Hany1Gadallah commented 1 year ago

@Hany1Gadallah, take a look at Font.createFont(...) https://docs.oracle.com/javase/8/docs/api/java/awt/Font.html

I taked a look but i cant see a method to create this fonts in otf/ttf extention could you create this fonts in ttf or otf please i want to use your fonts in windows .. thank you for your reply and interesting