Skip to content

Instantly share code, notes, and snippets.

@Ircama
Last active June 14, 2024 18:30
Show Gist options
  • Save Ircama/bd53c77c98ecd3d7db340c0398b22d8a to your computer and use it in GitHub Desktop.
Save Ircama/bd53c77c98ecd3d7db340c0398b22d8a to your computer and use it in GitHub Desktop.
Print to Brother PTP300BT from a computer

Printing to a Brother P-Touch Cube PT-P300BT label printer from a computer

Introduction

The Brother P-touch Cube PT-P300BT labelling machine is intended to be controlled from the official Brother P-touch Design&Print 2 app for Android and iOS devices, instead of from a computer.

This Gist provides small code revision and some additional scripts to the dogtopus/Pipfile one, which in turn is forked from the excellent stecman one, in order to print text labels via scripts on computers with different O.S. or subsystems.

The scripts are used to convert text labels to appropriate images compatible with 12mm width craft tapes like TZe-131 or TZe-231. The font is fixed (TrueType ps:helvetica) and the ImageMagick convert parameters are tuned for the max allowed character size with this printer.

Two settings are empirically experimented: for character sequences (numbers and uppercase (or lowercase) letters) which do not overshoot below the baseline (e.g., different from jgpq|\$/@_()[]{}), the max size can be obtained via -pointsize 86 -splice 0x5 -border 10x10 (e.g., uppercase characters and numbers get the full size of about 9 mm with upper and lower tape border of 1,5 mm); otherwise, a slightly smaller but more compatible size (allowing overshoots) can be used via -size x82 -gravity south -splice 0x15 (corpus size of ab. 6 mm, cap height of over 7 mm). WSL has different values (see below). In all cases, -background white -bordercolor white -fill black have to be added. -style and -weight attributes can be added for italic and bold styles (while -stretch does not produce differences). The cap height of the default setting of the Brother P-touch Design&Print 2 app is about 5 mm, but can be resized via text submenu.

Other possibly compatible fonts (if installed) which could be added with the -family parameter (with Ubuntu):

  • Andale Mono
  • Courier New
  • Georgia
  • Liberation Mono
  • Liberation Sans
  • Liberation Sans Narrow
  • Liberation Serif
  • Nimbus Mono L
  • Ubuntu
  • Ubuntu Condensed

The latter (-family "Ubuntu Condensed") could be the most appropriate one for condensed text.

The scripts are simple examples which can be easily replaced by more functional apps.

Notice that the printer separates each printout by about 27 mm of unprinted tape.

Windows

Package installation on Windows

Download ImageMagick checking the legacy convert tool while installing.

python3 -m pip install --upgrade pip
python3 -m pip install pillow packbits
git clone https://gist.github.com/bd53c77c98ecd3d7db340c0398b22d8a.git printlabel # this Gist
cd printlabel

Unzip all files in "printlabel.zip" to the current directory, replacing the old ones (code modified to support RFCOMM on the Windows COM port).

Usage on Windows

Pair the printer with an RFCOMM COM port using the Windows Bluetooth panel.

Usage: printlabel "label to print" "COM Port"

./printlabel "Lorem Ipsum" COM5

Ubuntu

Package installation on Ubuntu

sudo apt-get update
sudo apt-get install -y python3 python3-dev python3-pip git imagemagick imagemagick-6.q16 libbluetooth-dev
python3 -m pip install --upgrade pip
python3 -m pip install pybluez pillow packbits
git clone https://gist.github.com/64ae743825e42f2bb8ec79cea7ad2057.git printlabel # dogtopus/Pipfile Gist
cd printlabel

cat > printlabel <<\eof
#!/bin/bash

if [ $# -ne 2 ]
   then echo "Usage: $(basename $0)" '"label to print" "Bluetooth MAC address"'
        exit 1
fi
echo "Press ESC to print or Control-C to break"
trap 'exit 2' 1 2 13 15
if [[ "$1" =~ [\]jgpq\|\\\$/@_(){}\[] ]]
   then convert -size x82 -gravity south -splice 0x15 -background white -bordercolor white label:"$1" -fill black label.png
        echo "standard mode"
   else convert -pointsize 86 -background white -bordercolor white label:"$1" -fill black -splice 0x5 -border 10x10 label.png # UPPERCASE BLOCK
        echo "UPPERCASE MODE (bigger font)"
fi
display label.png
python3 labelmaker.py -i label.png "$2"
rm label.png
exit 0
eof

chmod a+x printlabel

Note: Windows Subsystem for Linux (also V2) does not support Bluetooth at the moment.

Bluetooth printer connection on Ubuntu

Switch on the printer.

Connect the printer via Ubuntu Bluetooth panel (e.g., Settings, Bluetooth).

To read the MAC address:

hcitool scan

Usage on Ubuntu

Usage: printlabel "label to print" "Bluetooth MAC address"

Selecting standard font size:

./printlabel "Lorem Ipsum" "AA:BB:CC:DD:EE:FF"

Selecting bigger font with uppercase letters and numbers:

./printlabel "LOREM IPSUM" "AA:BB:CC:DD:EE:FF"

WSL (Windows Subsystem for Linux)

Package installation on WSL

On WSL the installation uses the same code for Windows, with a shell based procedure without "display", which needs an X-Windows server.

sudo apt-get update
sudo apt-get install -y python3 python3-dev python3-pip git imagemagick imagemagick-6.q16 libbluetooth-dev
python3 -m pip install --upgrade pip
python3 -m pip install pillow packbits
git clone https://gist.github.com/bd53c77c98ecd3d7db340c0398b22d8a.git printlabel # this Gist
cd printlabel
unzip printlabel.zip
rm printlabel.zip

cat > printlabel.sh <<\eof
#!/bin/bash

if [ $# -ne 2 ]
   then echo "Usage: $(basename $0)" '"label to print" /dev/ttyS_serial_port_number'
        exit 1
fi
if [[ "$1" =~ [\]jgpq\|\\\$/@_(){}\[] ]]
   then convert -size x65 -splice 0x5 -background white -bordercolor white label:"$1" -fill black label.png
        echo "standard mode"
   else convert -pointsize 86 -background white -bordercolor white label:"$1" -fill black -splice 0x17 -border 0x0 label.png # UPPERCASE BLOCK
        echo "UPPERCASE MODE (bigger font)"
fi
python3 labelmaker.py -i label.png "$2"
rm label.png
exit 0
eof

chmod a+x printlabel.sh

Usage on WSL

Pair the printer with an RFCOMM COM port using the Windows Bluetooth panel. Check the outbound RFCOMM COM port number and use it to define /dev/ttyS_serial_port_number; for instance, COM5 is /dev/ttyS5.

Usage: printlabel.sh "label to print" /dev/ttyS_serial_port_number

Selecting standard font size:

./printlabel.sh "Lorem Ipsum" /dev/ttyS5

Selecting bigger font with uppercase letters and numbers:

./printlabel.sh "LOREM IPSUM" /dev/ttyS5
@miracuruzrh
Copy link

Hello

Thank you for this code.
I have followed the instructions with executing the two pip commands, clone the repository and unzip it and then followed the rest of the instructions.

With the new Version of ImageMagic, It seems that a bit have changed. Therefore I've tried to change the path in the printlabel.cmd file to
set CONVERT="C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\convert.exe" set DISPLAY="C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\imdisplay.exe"

Unfortunately, it seems that the newer ImageMagic Version doesn't have imdisplay.exe anymore.
And it seems that it have also an issue with
import ptstatus and import serial.

this are the error messages, I get, wehn I execute:
./printlabel "Lorem Ipsum" COM5

./printlabel "Lorem Ipsum" COM5
Use big fonts with uppercase letters/digits (Y/[N])? 
Standard font
WARNING: The convert command is deprecated in IMv7, use "magick"

convert.exe: unable to open image 'Ipsum': No such file or directory @ error/blob.c/OpenBlob/3596.
convert.exe: no decode delegate for this image format `' @ error/constitute.c/ReadImage/746.
Der Befehl ""C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\imdisplay.exe"" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Traceback (most recent call last):
  File "C:\Users\simon\myPythonProject\printlabel\labelmaker.py", line 10, in <module>
    import ptstatus
  File "C:\Users\simon\myPythonProject\printlabel\ptstatus.py", line 7, in <module>
    import serial
ModuleNotFoundError: No module named 'serial'

I have tried to solve the issues but I'm not experienced enough to solve the problem.
Maybe you are willing to check and maybe update the code or instructions?

Or do I do something wrong?

Best regards

Simon

@Trans-Femme-Programmer-Socks
Copy link

Trans-Femme-Programmer-Socks commented Jun 14, 2024

Heya @miracuruzrh, I had the exact issue as you and it took me too long to figure out...

I can't tell if ImageMagick V7.1.1 has removed that feature because I straight up went out and found myself a copy of V7.1.0-16.
HOWEVER, it shouldn't really matter because I have tried it with 7.1.1-33 and it seems to be able to print. If you really want to have a preview before printing, you can simply comment out set DISPLAY="D:\misc\PT\ImageMagick-7.1.1-Q16-HDRI\imdisplay.exe", or %DISPLAY% label.png in :CONT to completely skip that step.

In any case, the first error is caused by python not coming with serial per default. Running python3 -m pip install pyserial fixes that issue.

The next thing you would figure out is the fact that, for some reason, you will only be able to print one word. This is because, even though the text is in "", batch still counts it as multiple arguments, for some odd reason.
My fix for this is changing up printlabel.cmd even more:

@echo off

set CONVERT="C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\convert.exe"
set DISPLAY="C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\imdisplay.exe"

:PROMPT
SET /P USERINPUT="Text: "

:PROMPT
SET /P COMPORT="Specify COM port: COM"

setlocal
:PROMPT
SET /P UPPERC="Use big fonts with uppercase letters/digits (Y/[N])? "
IF /I "%UPPERC%" NEQ "Y" GOTO LOWERC

:UPPERC
echo UPPERCASE MODE (bigger font)
%CONVERT% -pointsize 86 -background white -bordercolor white label:"%USERINPUT%" -fill black -splice 0x5 -border 10x10 label.png
goto CONT

:LOWERC
echo Standard font
%CONVERT% -size x82 -gravity south -splice 0x15 -background white -bordercolor white label:"%USERINPUT%" -fill black label.png
goto CONT

:CONT
REM %DISPLAY% label.png
python labelmaker.py -i label.png "COM%COMPORT%"
del label.png

Having your desired text (%USERINPUT%) and COM port (%COMPORT%) be a query inside the batch file also allows you to simply open the file without any other arguments, which makes using this mess a lot easier.

Hope all of this helps >w<

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment