Printing Image/PDF

Brother Print SDK supports printing image(s) and PDF(s) for Android/iOS devices. This page shows you sample codes to print an image file for each series.

To print PDF, you use printPDFWithURL:settings: instead of printImageWithURL:settings:.

Sample Codes

PT/QL Series

PT/QL series supports predefined labels. You need to specify one of them to print.

iOS - Objective-C:

- (void)printImage {
    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;

    BRLMQLPrintSettings *qlSettings = [[BRLMQLPrintSettings alloc] initDefaultPrintSettingsWithPrinterModel:BRLMPrinterModelQL_YOURS];
    qlSettings.labelSize = BRLMPrintSettingsLabelSizeYours;

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"YourImageFilename" withExtension:@"Extension"];

    BRLMPrintError *printError = [printerDriver printImageWithURL:url settings:qlSettings];

    if (printError.code != BRLMPrintErrorCodeNoError) {
        NSLog(@"Error - Print Image: %@", @(printError.code));
    }
    else {
        NSLog(@"Success - Print Image");
    }

    [printerDriver closeChannel];
}

iOS - Swift:

func printImage() {
    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 url = Bundle.main.url(forResource: "YourImageFilename", withExtension: "Extension"),
        let printSettings = BRLMQLPrintSettings(defaultPrintSettingsWith: .YourQLModel)
        else {
            print("Error - Image file is not found.")
            return
    }

    printSettings.labelSize = .YourLabelSize
    printSettings.autoCut = true

    let printError = printerDriver.printImage(with: url, settings: printSettings)

    if printError.code != .noError {
        print("Error - Print Image: \(printError.code)")
    }
    else {
        print("Success - Print Image")
    }
}

Android:

void printImage() {
    Channel channel = Channel.newWifiChannel("IPAddress.of.your.printer");

    PrinterDriverGenerateResult result = PrinterDriverGenerator.openChannel(channel);
    if (result.getError().getCode() != OpenChannelError.ErrorCode.NoError) {
        Log.e("", "Error - Open Channel: " + result.getError().getCode());
        return;
    }

    File dir = getExternalFilesDir(null);
    File file = new File(dir, "YourImageFilename");

    PrinterDriver printerDriver = result.getDriver();
    QLPrintSettings printSettings = new QLPrintSettings(PrinterModel.YourQLModel);

    printSettings.setLabelSize(QLPrintSettings.LabelSize.YourLabelSize);
    printSettings.setAutoCut(true);
    printSettings.setWorkPath(dir.toString());

    PrintError printError = printerDriver.printImage(file.toString(), printSettings);

    if (printError.getCode() != PrintError.ErrorCode.NoError) {
        Log.d("", "Error - Print Image: " + printError.getCode());
    }
    else {
        Log.d("", "Success - Print Image");
    }

    printerDriver.closeChannel();
}

RJ/TD Series

RJ/TD series supports custom papers. You need to specify paper information by the one of following methods.

Specify Paper Information One by One

You can specify the paper you want to use by setting each parameter one by one. This method is supported by printers in following pages.

iOS - Objective-C:

- (void)printImage {
    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;

    BRLMTDPrintSettings *tdSettings = [[BRLMTDPrintSettings alloc] initDefaultPrintSettingsWithPrinterModel:BRLMPrinterModelTD_YOURS];

    BRLMCustomPaperSizeMargins margin = BRLMCustomPaperSizeMarginsMake(0.0, 0.0, 0.0, 0.0);
    BRLMCustomPaperSize *customPaperSize = [[BRLMCustomPaperSize alloc] initRollWithTapeWidth:2.0
                                                                                      margins:margin
                                                                                 unitOfLength:BRLMCustomPaperSizeLengthUnitInch];

    if (customPaperSize != nil) {
        tdSettings.customPaperSize = customPaperSize;
    }

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"YourImageFilename" withExtension:@"Extension"];

    BRLMPrintError *printError = [printerDriver printImageWithURL:url settings:tdSettings];

    if (printError.code != BRLMPrintErrorCodeNoError) {
        NSLog(@"Error - Print Image: %@", @(printError.code));
    }
    else {
        NSLog(@"Success - Print Image");
    }

    [printerDriver closeChannel];
}

iOS - Swift:

func printImage() {
    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 url = Bundle.main.url(forResource: "YourImageFilename", withExtension: "Extension"),
        let printSettings = BRLMTDPrintSettings(defaultPrintSettingsWith: .YourTDModel)
        else {
            print("Error - Image file is not found.")
            return
    }

    // Set your paper information
    let margins = BRLMCustomPaperSizeMargins(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
    let customPaperSize = BRLMCustomPaperSize(rollWithTapeWidth: 2.0,
                                                margins: margins,
                                                unitOfLength: .inch)
    printSettings.customPaperSize = customPaperSize

    let printError = printerDriver.printImage(with: url, settings: printSettings)

    if printError.code != .noError {
        print("Error - Print Image: \(printError.code)")
    }
    else {
        print("Success - Print Image")
    }
}

Android:

void printImage() {
    Channel channel = Channel.newWifiChannel("IPAddress.of.your.printer");

    PrinterDriverGenerateResult result = PrinterDriverGenerator.openChannel(channel);
    if (result.getError().getCode() != OpenChannelError.ErrorCode.NoError) {
        Log.e("", "Error - Open Channel: " + result.getError().getCode());
        return;
    }

    File dir = getExternalFilesDir(null).toString();
    File file = new File(dir, "YourImageFilename");

    PrinterDriver printerDriver = result.getDriver();
    TDPrintSettings printSettings = new TDPrintSettings(PrinterModel.YourTDModel);

    CustomPaperSize.Margins margins = new CustomPaperSize.Margins(0.0f, 0.0f, 0.0f, 0.0f);
    CustomPaperSize customPaperSize = CustomPaperSize.newRollPaperSize(4.0, margins, CustomPaperSize.Unit.Inch);

    printSettings.setCustomPaperSize(customPaperSize);

    PrintError printError = printerDriver.printImage(file.toString(), printSettings);

    if (printError.getCode() != PrintError.ErrorCode.NoError) {
        Log.d("", "Error - Print Image: " + printError.getCode());
    }
    else {
        Log.d("", "Success - Print Image");
    }

    printerDriver.closeChannel();
}

Use Paper Infomartion File

Custom paper information can be exported as .bin file from the printer driver for each models. You can embed the file in your application so that the SDK can use it. For more information, see Configuring Your Printer for Printing on Custom-Size Paper.

iOS - Objective-C:

- (void)printImage {
    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;

    BRLMTDPrintSettings *tdSettings = [[BRLMTDPrintSettings alloc] initDefaultPrintSettingsWithPrinterModel:BRLMPrinterModelTD_YOURS];

    NSURL *customPaperFileUrl = [[NSBundle mainBundle] URLForResource:@"YourCustomPaperFile" withExtension:@"bin"];
    if (customPaperFileUrl != nil) {
        BRLMCustomPaperSize *customPaperSize = [[BRLMCustomPaperSize alloc] initWithFile:customPaperFileUrl];
        tdSettings.customPaperSize = customPaperSize;
    }

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"YourImageFilename" withExtension:@"Extension"];

    BRLMPrintError *printError = [printerDriver printImageWithURL:url settings:tdSettings];

    if (printError.code != BRLMPrintErrorCodeNoError) {
        NSLog(@"Error - Print Image: %@", @(printError.code));
    }
    else {
        NSLog(@"Success - Print Image");
    }

    [printerDriver closeChannel];
}

iOS - Swift:

func printImage() {
    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 url = Bundle.main.url(forResource: "YourImageFilename", withExtension: "Extension"),
        let printSettings = BRLMTDPrintSettings(defaultPrintSettingsWith: .YourTDModel)
        else {
            print("Error - Image file is not found.")
            return
    }

    if let customPaperFileUrl = Bundle.main.url(forResource: "YourCustomPaperFile", withExtension: "bin") {
        let customPaperSize = BRLMCustomPaperSize(file: customPaperFileUrl)
        printSettings.customPaperSize = customPaperSize
    }

    let printError = printerDriver.printImage(with: url, settings: printSettings)

    if printError.code != .noError {
        print("Error - Print Image: \(printError.code)")
    }
    else {
        print("Success - Print Image")
    }
}

Android:

void printImage() {
    Channel channel = Channel.newWifiChannel("IPAddress.of.your.printer");

    PrinterDriverGenerateResult result = PrinterDriverGenerator.openChannel(channel);
    if (result.getError().getCode() != OpenChannelError.ErrorCode.NoError) {
        Log.e("", "Error - Open Channel: " + result.getError().getCode());
        return;
    }

    File dir = getExternalFilesDir(null).toString();
    File file = new File(dir, "YourImageFilename");

    PrinterDriver printerDriver = result.getDriver();
    TDPrintSettings printSettings = new TDPrintSettings(PrinterModel.YourTDModel);

    File customPaperSizeFile = new File(dir, "YourCustomPaperFile");

    CustomPaperSize customPaperSize = CustomPaperSize.newFile(customPaperSizeFile.toString());
    printSettings.setCustomPaperSize(customPaperSize);

    PrintError printError = printerDriver.printImage(file.toString(), printSettings);

    if (printError.getCode() != PrintError.ErrorCode.NoError) {
        Log.d("", "Error - Print Image: " + printError.getCode());
    }
    else {
        Log.d("", "Success - Print Image");
    }

    printerDriver.closeChannel();
}

PJ/MW Series

PJ/MW series supports some standard papers. You need to specify one of them to print.

iOS - Objective-C:

- (void)printImage {
    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;

    BRLMPJPrintSettings *pjSettings = [[BRLMPJPrintSettings alloc] initDefaultPrintSettingsWithPrinterModel:BRLMPrinterModelPJ_YOURS];
    pjSettings.paperSize = [[BRLMPJPrintSettingsPaperSize alloc] initWithPaperSizeStandard: BRLMPJPrintSettingsPaperSizeStandardLetter];

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"YourImageFilename" withExtension:@"Extension"];

    BRLMPrintError *printError = [printerDriver printImageWithURL:url settings:pjSettings];

    if (printError.code != BRLMPrintErrorCodeNoError) {
        NSLog(@"Error - Print Image: %@", @(printError.code));
    }
    else {
        NSLog(@"Success - Print Image");
    }

    [printerDriver closeChannel];
}

iOS - Swift:

func printImage() {
    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 url = Bundle.main.url(forResource: "YourImageFilename", withExtension: "Extension"),
        let printSettings = BRLMPJPrintSettings(defaultPrintSettingsWith: .YourPJModel)
        else {
            print("Error - Image file is not found.")
            return
    }

    let pjPaperSize = BRLMPJPrintSettingsPaperSize(paperSizeStandard: .pjStandardSizeLetter)
    printSettings.paperSize = pjPaperSize

    let printError = printerDriver.printImage(with: url, settings: printSettings)

    if printError.code != .noError {
        print("Error - Print Image: \(printError.code)")
    }
    else {
        print("Success - Print Image")
    }
}

Android:

void printImage() {
    Channel channel = Channel.newWifiChannel("IPAddress.of.your.printer");

    PrinterDriverGenerateResult result = PrinterDriverGenerator.openChannel(channel);
    if (result.getError().getCode() != OpenChannelError.ErrorCode.NoError) {
        Log.e("", "Error - Open Channel: " + result.getError().getCode());
        return;
    }

    File dir = getExternalFilesDir(null);
    File file = new File(dir, "YourImageFilename");

    PrinterDriver printerDriver = result.getDriver();
    PJPrintSettings printSettings = new PJPrintSettings(PrinterModel.YourPJModel);

    printSettings.setPaperSize(PJPaperSize.newPaperSize(PJPaperSize.PaperSize.Letter));
    printSettings.setWorkPath(dir.toString());

    PrintError printError = printerDriver.printImage(file.toString(), printSettings);

    if (printError.getCode() != PrintError.ErrorCode.NoError) {
        Log.d("", "Error - Print Image: " + printError.getCode());
    }
    else {
        Log.d("", "Success - Print Image");
    }

    printerDriver.closeChannel();
}