Preface
Recently, the printer was used in the project. I didn’t understand it at first, but now I have done it, so I have summarized it. This article includes the background call printing (two ways) and the front-end js printing, but only IE now supports printing. Moreover, if you want to remotely connect the printer, the generation of QR codes and directly operate by the printer commands, you need to modify the browser’s security configuration. Here is a detailed introduction.
The first type of background printing:
Use the PrintServiceLookup class in javax to print. You can directly call the default printer, or you can use the following methods to filter and print:
PrintServiceLookup.lookupMultiDocPrintServices(flavors, attributes);
The executable code is as follows:
public static void main(String[] args) { FileInputStream textStream = null; try { textStream = new FileInputStream("Address"); } catch (FileNotFoundException e) { e.printStackTrace(); } if (textStream != null) // When the print content is not empty { // Specify the printout format DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;//SERVICE_FORMATTED.PRINTABLE // Position the default printing service PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); // Create a print job DocPrintJob job = printService.createPrintJob(); // Set the print attribute PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); // Set the paper size, you can also create a new MediaSize class to customize the size pras.add(MediaSizeName.ISO_A4); DocAttributeSet das = new HashDocAttributeSet(); // Specify the print content Doc doc = new SimpleDoc(textStream, flavor, das); // Do not display the print dialog box, print work directly try { job.print(doc, pras); // Perform specific printing operations on each page} catch (PrintException pe) { pe.printStackTrace(); } } else { // If the print content is empty, prompt the user to print will cancel JOptionPane.showConfirmDialog(null, "Sorry, Printer Job is Empty, Print Cancelled!", "Empty", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE); } } The second type of background printing:
Note: The second and third types of printing use printer commands to operate. Here you need the jna jar package, and jdk requires 32-bit, and the corresponding dll needs to be registered. For different systems, DLLs must be registered under different Windows. After successful registration, win+R is required, and Regsvr32.exe TSCActiveX.dll command is called and run.
The executable code is as follows:
public class JavaDemo { public interface TscLibDll extends Library { TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary("TSCLIB", TscLibDll.class); int about(); int openport(String pianoName); int closeport(); int sendcommand(String printerCommand); int setup(String width, String height, String speed, String density, String sensor, String vertical, String offset); int downloadpcx(String filename, String image_name); int barcode(String x, String y, String type, String height, String readable, String rotation, String narrow, String wide, String code); int printerfont(String x, String y, String fonttype, String rotation, String xmul, String ymul, String text); int clearbuffer(); int printlabel(String set, String copy); int formfeed(); int nobackfeed(); int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content); } public static void main(String[] args) { System.setProperty("jna.encoding", "GBK");// Support Chinese SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = df.format(new Date()); String qrCode = "PD102011"; TscLibDll.INSTANCE.openport("TSC TTP-244 Pro");//Print model TscLibDll.INSTANCE.setup("70","40","5","8","0","2","0"); TscLibDll.INSTANCE.clearbuffer(); String command = "QRCODE 120,90,Q,8,A,0,M1,S7,/"" + qrCode+"/""; //Print the parameters and contents of the QR code TscLibDll.INSTANCE.sendcommand(command); //Transfer command TscLibDll.INSTANCE.sendcommand("TEXT 300 70 36 0 0 0 arial"+ "Office supplies-label paper"); //TscLibDll.INSTANCE.windowsfont(300, 70, 36, 0, 0, 0, "arial", "Office supplies-label paper"); TscLibDll.INSTANCE.printlabel("1", "1"); TscLibDll.INSTANCE.closeport(); }} Note: This printing method requires a USB connection (ttp-244 pro). If you want to generate online and connect remotely, you need to use the third method.
The third type of JS printing
When printing using JS, the browser is mainly restricted. Currently, only IE browser supports it. When printing, the Automation server cannot create objects on the console. Please adjust the relevant settings in ActiveX in the custom level in the browser security to enable. The code is as follows:
<script type="text/javascript"> function printQRcode(){ var TSCObj = new ActiveXObject("TSCActiveX.TSCLIB");//dll used by the printer //TSCObj.ActiveXabout(); // Printer information TSCObj.ActiveXopenport ("TSC TTP-244 Pro");// Printer name, cannot be written incorrectly //TSCObj.ActiveXdownloadpcx ("D:/myeclipseproject/filesManager/trunk/doc/02-Reference/03-QR Code Printer/01-Development Help/PHP Call Example/TSCActiveX.DLL-PHP-Example/","123.PCX"); TSCObj.ActiveXsetup("70","40","4","15","0","2.5","0");//Print Settings//TSCObj.ActiveXsetup("Label Width","Label Height","Print Speed","Print Density (0-15)","Sensor Category String Type, 0 means using a vertical distance sensor, 1 means using a black sensor (black mark) senso)","Gap/Black mark vertical spacing (mm)","Gap/Black mark offset distance (mm)"); //TSCObj.ActiveXformfeed(); //TSCObj.ActiveXnobackfeed(); TSCObj.ActiveXsendcommand("DIRECTION 1"); //Set the tag direction, DIRECTION 1 upper left corner (x,y)={0,0}; DIRECTION 0 lower right corner (x,y)={0,0}; TSCObj.ActiveXsendcommand ("SET TEAR ON"); TSCObj.ActiveXclearbuffer(); //TSCObj.ActiveXsendcommand ("PUTPCX 0,0,/"123.PCX/""); /* 1. ActiveXprinterfont uses built-in encoding for machine, which is usually used to type in English. 2. ActiveXwindowsfont can output Chinese characters, but it must be a font that exists in the system. TSCObj.ActiveXprinterfont ("a","b","c","d","e","f","g"); a: string, the starting point in the X direction of the text, represented by a point. b: String, the starting point of the text Y direction, represented by a dot. c: Built font names, a total of 12 types (1: 8*12 dots 2: 12*20 dots 3: 16*24 dots 4: 24*32 dots 5: 32*48 dots TST24.BF2: Traditional Chinese 24*24 TST16.BF2: Traditional Chinese 16*16 TTT24.BF2: Traditional Chinese 24*24 (Letter) TSS24.BF2: Chinese 24*24 TSS16.BF2: Chinese 16*16 K: Text 24*24 L: Text 16*16 ) d: String, rotation angle e: String, X-direction magnification 1-8 f: String, Y-direction magnification 1-8 g: string, print content ActiveXwindowsfont(a,b,c,d,e,f,g,h) Description: Print text using Windows TTF font. Parameters: a: integer type, the starting point in the X direction of the text, represented by a point. b: Integer type, the starting point of the text Y direction, represented by a point. c: integer type, font height, represented by dots. d: integer type, rotation angle, counterclockwise rotation. 0-rotate 0°, 90-rotate 90°, 180-rotate 180°, 270-rotate 270°. e: integer type, font appearance. 0: Label; 1: Italic; 2: Bold; 3: Bold italic. f: integer type, underscore, 0: no underscore; 1: underscore. g: string type, font name. For example: Arial, Times new Roman. h: string type, print text content. */// TSCObj.ActiveXwindowsfont (500, 200, 48, 90, 0, "Arial", "/u7f16/u7801"); TSCObj.ActiveXwindowsfont (260, 60, 60, 0, 2, 0, "Arial", "XX-specific"); //var cmd = 'QRCODE barcode start point in X direction, barcode start point in Y direction, error correction level, QR code height, A(A and M), rotation angle, M2 (classified into type 1 and type 2), S1 (s1-s8, default s7), /"1231 Hello 2421341325454353/"'; var cmd = 'QRCODE 80,80,H,7,A,0,M2,S1,/"'+"123456789"+'/"'; TSCObj.ActiveXsendcommand(cmd); TSCObj.ActiveXwindowsfont (280, 150, 40, 0, 0, "Arial", "123456789"); TSCObj.ActiveXwindowsfont (180, 260, 30, 0, 0, 0, "Arial", "XXXXXXXXX"); TSCObj.ActiveXprintlabel ("1","1");// (number of print copies, number of prints per page) TSCObj.ActiveXclearbuffer();//Clear TSCObj.ActiveXcloseport();//Close the print port}</script>Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.