recently the use of the printer, the beginning of the completely do not understand, now is ready, so do a summary, the article includes the background of the call printing (two ways) the front end of the JS printing, but only IE now support printing, and if you want to remotely connect the printer, Two-dimensional code generation and directly by the command of the printer to operate, it is necessary to modify the browser security configuration, the following to do a detailed introduction
The first spool print:
using the printservicelookup class in javax to print, you can call the default printer directly, or you can filter printing by using the following methods: printservicelookup. Look upMult idocprintservices (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 print output format
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;//SERVICE_FORMATTED.PRINTABLE
/ / Position the default print service
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
// Create a print job
DocPrintJob job = printService.createPrintJob();
/ / Set the print properties
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 print content
Doc doc = new SimpleDoc(textStream, flavor, das);
/ / Do not display the print dialog, print directly
Try {
Job.print(doc, pras); // Make specific print operations for each page
} catch (PrintException pe) {
pe.printStackTrace();
}
} else {
// If the print content is empty, prompting 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 spool print:
Note: The second is to use the printer's command for the third type of printing, where the JNA jar package is required, and the JDK requires 32-bit, and to register the corresponding DLL, the corresponding system will be in different Windows Register DLL, need win+r after successful registration, call and run Regsvr32.exe TSCActiveX.dll Instruction
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 pirnterName);
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");//Printer 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 instructions
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 type of printing requires a USB connection (ttp-244 Pro), if you want to generate online remotely, you need to use a third wayThe third type of JS printingWhen using JS to print the main browser restrictions, currently only Internet Explorer support, when printing, the console appears Automation server cannot create objects, please adjust the browser security in the custom level in the ActiveX related settings to enable, The code is as follows:
<script type="text/javascript">
Function printQRcode(){
Var TSCObj = new ActiveXObject("TSCActiveX.TSCLIB");//The dll used by the printer
//TSCObj.ActiveXabout(); // Printer information
TSCObj.ActiveXopenport ("TSC TTP-244 Pro"); / / printer name, can not be wrong
//TSCObj.ActiveXdownloadpcx ("D:/myeclipseproject/filesManager/trunk/doc/02-reference/03-QR code printer/01-development help/PHP calling example/TSCActiveX.DLL-PHP-Example/"," 123.PCX");
TSCObj.ActiveXsetup("70","40","4","15","0","2.5","0");//Printer Settings
//TSCObj.ActiveXsetup("label width", "tag height", "printing speed", "printing density (0-15)", "sensor category string type, 0 means using vertical spacing sensor (gap sensor) ), 1 means black mark senso, "Gap/Black mark vertical pitch (mm)", "Gap/Black mark offset distance (mm)");
//TSCObj.ActiveXformfeed();
//TSCObj.ActiveXnobackfeed();
TSCObj.ActiveXsendcommand("DIRECTION 1"); //Set the label direction, DIRECTION 1 upper left corner (x,y)={0,0}; DIRECTION 0 bottom right corner (x,y)={0,0};
TSCObj.ActiveXsendcommand ("SET TEAR ON");
TSCObj.ActiveXclearbuffer();
//TSCObj.ActiveXsendcommand ("PUTPCX 0,0,\"123.PCX\"");
/*
1, ActiveXprinterfont uses the built-in code of the machine is usually used to play English.
2, ActiveXwindowsfont can output Chinese characters, but must be the fonts that exist in the system.
TSCObj.ActiveXprinterfont ("a","b","c","d","e","f","g");
a: string, the starting point of the text X direction, expressed in dots.
b: String, the starting point of the text Y direction, expressed in dots.
c: Built-in font name, a total of 12 kinds (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 (Telecom Code) TSS24.BF2: Simplified Chinese 24*24 TSS16.BF2: Simplified Chinese 16*16 K: Korean 24*24 L: Korean 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 fonts.
parameter:
a: integer type, the starting point of the text X direction, expressed in dots.
b: integer type, the starting point of the text Y direction, expressed in dots.
c: integer type, font height, expressed in dots.
d: integer type, rotation angle, counterclockwise rotation. 0-rotation 0°, 90-rotation 90°, 180-rotation 180°, 270-rotation 270°.
e: integer type, font shape. 0: label; 1: italic; 2: bold; 3: bold italic.
f: integer type, underline, 0: no underscore; 1: underlined.
g: string type, font name. Such as: Arial, Times new Roman.
h: String type, print text content.
*/
// TSCObj.ActiveXwindowsfont (500, 200, 48, 90, 0, 0, "Arial", "\u7f16\u7801");
TSCObj.ActiveXwindowsfont (260, 60, 60, 0, 2, 0, "Arial", "XX dedicated");
//var cmd = 'QRCODE bar code X direction starting point, bar code Y direction starting point, error correction level, QR code height, A (A and M), rotation angle, M2 (divided into type 1 and type 2), S1 (s1-s8, default s7), \"1231 hello2421341325454353\"';
Var cmd = ‘QRCODE 80,80,H,7,A,0,M2,S1,\"‘+"123456789"+‘\"‘;
TSCObj.ActiveXsendcommand(cmd);
TSCObj.ActiveXwindowsfont (280, 150, 40, 0, 0, 0, "Arial", "123456789");
TSCObj.ActiveXwindowsfont (180, 260, 30, 0, 0, 0, "Arial", "XXXXXXXX");
TSCObj.ActiveXprintlabel ("1","1");//(number of copies, number of sheets per page)
TSCObj.ActiveXclearbuffer();//Clear
TSCObj.ActiveXcloseport (); / / close the print port
}
</script>
Thank you!!!
Java calls the TSC printer for printing