Skip to content

P-touch Template を転送・印刷する

テンプレート印刷機能は次の手順で利用できます。

  1. P-touch Editor で P-touch Template(*.pd3, *.pdz, *.blf) を作成する (*1)
  2. プリンターに P-touch Template を転送する
  3. テンプレートを指定し、印刷する (*2)

(*1) Brother Print SDK は対応していません。
(*2) 印刷時にテンプレートに含まれるテキストを置換することが可能です。

サンプルコード

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();
}

テンプレート印刷

iOS - Objective-C:

- (void)printTemplate:(int)templateKey index:(NSUInteger)index text:(NSString *)newText encode:(BRLMTemplateObjectEncode)encode{
    BRLMChannel *channel = [[BRLMChannel alloc] initWithWifiIPAddress:@"IPAddress.of.your.printer"];

    BRLMPrinterDriverGenerateResult *driverGenerateResult = [BRLMPrinterDriverGenerator openChannel:channel];
    if (driverGenerateResult.error.code != BRLMOpenChannelErrorCodeNoError ||
        driverGenerateResult.driver == nil) {
        NSLog(@"%@", @(driverGenerateResult.error.code));
        return;
    }

    BRLMPrinterDriver *printerDriver = driverGenerateResult.driver;
    BRLMPTTemplatePrintSettings *ptSettings = [[BRLMPTTemplatePrintSettings alloc] initDefaultPrintSettingsWithPrinterModel:BRLMPrinterModelPT_YOURS];
    ptSettings.numCopies = 1;

    //Create replacer if you want change objects
    BRLMTemplateObjectReplacer *replacer = [[BRLMTemplateObjectReplacer alloc] initWithObjectIndex:index value:newText encode:encode];
    NSArray<BRLMTemplateObjectReplacer *> *replacers = [NSArray arrayWithObject:replacer];

    BRLMPrintError *printError = [printerDriver printTemplateWithKey:templateKey settings:ptSettings replacers:replacers];
    if (printError.code != BRLMPrintErrorCodeNoError) {
        NSLog(@"Error - Print Template: %@", @(printError.code));
    }
    else {
        NSLog(@"Success - Print Template");
    }

    [printerDriver closeChannel];
}

iOS - Swift:

    func printTemplate(templateKey: UInt, index:UInt, newText: String, eoncode:BRLMTemplateObjectEncode) {
        let channel = BRLMChannel(wifiIPAddress: "IPAddress.of.your.printer")

        let generateResult = BRLMPrinterDriverGenerator.open(channel)
        guard generateResult.error.code == BRLMOpenChannelErrorCode.noError,
        let printerDriver = generateResult.driver else {
            print("Error - Open Channel: \(generateResult.error.code)")
            return
        }
        defer {
            printerDriver.closeChannel()
        }
        guard
            let ptSettings = BRLMPTTemplatePrintSettings(defaultPrintSettingsWith: .YourPTModel)
            else {
                print("Error - fail to create ptSettings.")
                return
        }
        ptSettings.numCopies = 1;

        //Create replacer if you want change objects
        let replacers = [BRLMTemplateObjectReplacer(objectIndex: index, value: newText, encode: eoncode)]

        let printError = printerDriver.printTemplate(withKey: templateKey, settings: ptSettings, replacers: replacers)
        if printError.code != .noError {
            print("Error - Print Template: \(printError.code)")
        }
        else {
            print("Success - Print Template")
        }
    }

Android - java:

void printTemplate(int templateKey, int index, String newText, TemplateObjectReplacer.Encode encode) {
    Channel channel = Channel.newWifiChannel("IPAddress.of.your.printer");
    PrinterDriverGenerateResult generateResult = PrinterDriverGenerator.openChannel(channel);
    if (generateResult.getError().getCode() != OpenChannelError.ErrorCode.NoError) {
        Log.e("", "Error - Open Channel: " + generateResult.getError().getCode());
        return;
    }

    PrinterDriver printerDriver = generateResult.getDriver();
    PTTemplatePrintSettings ptSettings = new PTTemplatePrintSettings(PrinterModel.YourPTModel);
    ptSettings.setNumCopies(1);

    //Create replacer if you want change objects
    TemplateObjectReplacer replacer = new TemplateObjectReplacer(index, newText, encode);
    ArrayList<TemplateObjectReplacer> replacers = new ArrayList<>();
    replacers.add(replacer);

    PrintError printError = printerDriver.printTemplate(templateKey, ptSettings, replacers);
    if (printError.getCode() != PrintError.ErrorCode.NoError) {
        Log.d("", "Error - Print Template: " + printError.getCode());
    }
    else {
        Log.d("", "Success - Print Template");
    }
    printerDriver.closeChannel();
}

Android - kotlin:

fun printTemplate(templateKey: Int, index: Int, newText: String, encode: TemplateObjectReplacer.Encode) {
    val channel = Channel.newWifiChannel("IPAddress.of.your.printer")
    val generateResult = PrinterDriverGenerator.openChannel(channel)
    if (generateResult.error.code != OpenChannelError.ErrorCode.NoError) {
        Log.e("", "Error - Open Channel: " + result.getError().getCode());
        return
    }

    val printerDriver = generateResult.driver
    val ptSettings = PTTemplatePrintSettings(PrinterModel.YourPTModel)
    ptSettings.numCopies = 1

    //Create replacer if you want change objects
    var replacer = ArrayList<TemplateObjectReplacer>()
    replacer.add(TemplateObjectReplacer(index, newText, encode))

    val printError = printerDriver.printTemplate(templateKey, ptSettings, replacer)
    if(printError.code != PrintError.ErrorCode.NoError) {
        Log.d("", "Error - Print Template: " + printError.getCode());
    }
    else {
        Log.d("", "Success - Print Template");
    }
    driver.closeChannel()
}