Printing with P-touch Template
Overview
This feature enables replacing the texts and printing by transferring a predefined template via the following steps.
- Create a template with P-touch Editor (*.pd3, *.pdz, *.blf) (*1)
- Transfer the template to a printer
- Print the template you specified. (*2)
(*1) Brother Print SDK doesn't support.
(*2) You can replace some texts in the template before printing.
Sample Codes
Transfer Template
iOS - Objective-C:
- (void)transfer:(NSString *)filePath { // Specify printer BRPtouchPrinter *printer = [[BRPtouchPrinter alloc] initWithPrinterName:@"QL-1110NWB" interface:CONNECTION_TYPE_WLAN]; [printer setIPAddress:@"your-printer-ip"]; // Connect, then transfer if ([printer startCommunication]) { int errorCode = [printer sendTemplate:filePath connectionType:CONNECTION_TYPE_WLAN]; if (errorCode != ERROR_NONE_) { NSLog(@"ERROR - %d", errorCode); } [printer endCommunication]; } }
iOS - Swift:
func transfer(filePath: String) { // Specify printer let printer = BRPtouchPrinter(printerName: "QL-1110NWB", interface: CONNECTION_TYPE.WLAN)! printer.setIPAddress("your-printer-ip") // Connect, then transfer if printer.startCommunication() { let errorCode = printer.sendTemplate(filePath, connectionType: CONNECTION_TYPE.WLAN) if errorCode != ERROR_NONE_ { print("ERROR - \(errorCode)") } printer.endCommunication() } }
Android:
void transfer(String filePath) { // Specify printer final Printer printer = new Printer(); PrinterInfo settings = printer.getPrinterInfo(); settings.printerModel = Model.QL_1110NWB; settings.ipAddress = "your-printer-ip"; settings.workPath = "Context.writable-dir-path"; printer.setPrinterInfo(settings); // Connect, then transfer new Thread(new Runnable() { @Override public void run() { if (printer.startCommunication()) { PrinterStatus result = printer.transfer(filepath); if (result.errorCode != ErrorCode.ERROR_NONE) { Log.d("TAG", "ERROR - " + result.errorCode); } printer.endCommunication(); } } }).start(); }
Print Template
The following sample code shows how to print a template replacing a text object with a new text. The SDK supports the following replacement API. See each API for more details.
Feature | iOS | Android |
---|---|---|
Replace text in order | replaceText: |
replaceText |
Replace text using the object index | replaceTextIndex:objectIndex: |
replaceTextIndex |
Replace text using the object name | replaceTextName:objectName: |
replaceTextName |
iOS - Objective-C:
- (void)printTemplate:(int)templateKey text:(NSString *)newText { // Specify printer BRPtouchPrinter *printer = [[BRPtouchPrinter alloc] initWithPrinterName:@"QL-1110NWB" interface:CONNECTION_TYPE_WLAN]; [printer setIPAddress:@"your-printer-ip"]; // Connect, then print if ([printer startCommunication]) { // Specify the template key and the printer encode if ([printer startPTTPrint:templateKey encoding:NSUTF8StringEncoding]) { // Replace text object with new text [printer replaceText:newText]; // Start print if ([printer flushPTTPrintWithCopies:1] == RET_FALSE) { NSLog(@"template print failed"); } } [printer endCommunication]; } }
iOS - Swift:
func printTemplate(templateKey: Int, newText: String) { // Specify printer let printer = BRPtouchPrinter(printerName: "QL-1110NWB", interface: CONNECTION_TYPE.WLAN)! printer.setIPAddress("your-printer-ip") // Connect, then print if printer.startCommunication() { // Specify the template key and the printer encode if printer.startPTTPrint(templateKey, NSUTF8StringEncoding) { // Replace text object with new text printer.replaceText(newText); // Start print if printer.flushPTTPrint(withCopies:1) == RET_FALSE { print("template print failed") } } printer.endCommunication() } }
Android:
void printTemplate(int templateKey, String newText) { // Specify Printer final Printer printer = new Printer(); PrinterInfo settings = printer.getPrinterInfo(); settings.printerModel = Model.QL_1110NWB; settings.ipAddress = "your-printer-ip"; // Connect, then print new Thread(new Runnable() { @Override public void run() { if (printer.startCommunication()) { // Specify the template key and the printer encode if (startPTTPrint(templateKey, null)) { // Replace text object with new text replaceText(newText); // Start print PrinterStatus result = printer.flushPTTPrint(); if (result.errorCode != ErrorCode.ERROR_NONE) { Log.d("TAG", "ERROR - " + result.errorCode); } } printer.endCommunication(); } } }).start(); }