Print Text mode by using Java

พอดีว่าต้องเขียน project ด้วย grails ที่ต้องทำการพิมพ์รายงานที่เป็น text mode เกิดมาก็พึ่งรู้นี่แหละว่า ไอเครื่องพิมพ์หัวเข็ม รุ่นเก่าๆ ที่มันมีปุ่มเปลี่ยน font มันคืออะไร ที่แท้มันก็คือ เอาไว้เปลี่ยน font เวลาเราจะพิมพ์เป็น text mode นั่นเอง

วิธีการพิมพ์แบบ text mode ทดลองได้โดย

1
c:>echo "test print">lpt1

โดยเครื่องพิมพ์ของเราจะต้องต่อที่พอร์ท lpt1

สำหรับ code java ก็ทำได้ง่ายโดยการเปลี่ยน OutputStream ให้เป็นเครื่องพิมพ์นั่นเอง

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.*;
public class lpt {
    public static void main (String[] argv) {
        try {
                FileOutputStream os = new FileOutputStream("LPT1");
                            //wrap stream in "friendly" PrintStream
                PrintStream ps = new PrintStream(os);
 
                            //print text here
                ps.println("Hello world!");
 
                            //form feed -- this is important
                            //Without the form feed, the text
                            //will simply sit in the print
                            //buffer until something else
                            //gets printed.
                ps.print("\f");
                            //flush buffer and close
                ps.close();
          } catch (Exception e) {
                System.out.println("Exception occurred: " + e);
          }
     }
}

ปล. Java เองก็มี Library ในการพิมพ์แบบ Text mode ที่ชื่อว่า RTextPrint ซึ่งราคา $50 USD ^^
ปล.2 บังเอินว่างานที่ทำดันเป็นทำเวปซ่ะนี่ เลยใช้ไม่ได้ เพราะต้องพิมพ์ที่เครื่องของผู้ใช้งาน ไม่ได้พิมพ์โดยเซอร์เวอร์ คงต้องลองหา การพิมพ์ Text จาก Browser โดยให้สามารถปรับ line space ได้แทน