Token2Shell/MD can redirect received text to a printer instead of displaying them. You can also preview and print a PDF file over existing connection within Token2Shell/MD. Since this local printing feature is controlled by terminal control escape sequences, you can use it with any connection type (SSH, TELNET or etc.) supported in Token2Shell/MD.


Print Mode

The following printing mode is currently available:

• Apply Font and Margins

If a plain text file or content is received, it's formatted according the font and margin settings. Text lines are not wrapped and 0x0C (or U+000C) character is recognized as a form feed control character that starts a new page.

Once the content is formatted or loaded, Token2Shell/MD shows it in a preview window.

Token2Shell/MD currently supports plain text and PDF content. For unsupported content or files, you can still save it to your personal folder.

• Save To File

Instead of a preview window, Token2Shell/MD directly opens a "Save As" popup and stores the content as is.

• Direct Raw Network Printing

Token2Shell/MD opens a TCP/IP connection to the specified address and writes the content as it's received. No print preview window will be shown in this mode.

Please note that when direct file printing (ESC[2016i) is used, Token2Shell/MD sends the original file content to the printer instead of the raw encoded data it received from the server. Hence, if your server can generate actual printer control codes, you can transparently have them printed on your local printer.

For example, if your local network printer accepts PostScript and your server can also generate output in the same format, you can simply save your output as a PostScript file and send it over "direct file printing (ESC[2016i)". You can also safely transfer printer control codes with binary data such as PCL.



Print Control Escape Sequences

Printing in Token2Shell/MD is started and stopped by the following terminal control escape sequences:

<ESC>[5i Start local printing; all received text is sent to a printer.
(Part of ANSI/VT100 terminal control escape sequences)
<ESC>[2016i Start direct file printing.
(Newly introduced proprietary escape sequence for Token2Shell/MD)
Requires the content to be in data URI scheme:
data:[<mediatype>][;filename=<filename>][;base64],<data>
<ESC>[4i Stop local printing.
(Part of ANSI/VT100 terminal control escape sequences)

Direct File Printing Data Format

When "<ESC>[2016i" is used, you must format the content of your file in data URI scheme (RFC 2097).
data:[<mediatype>][;filename=<filename>][;base64],<data>

<filename> must be percent-encoded (or URL encoded). If ";filename=<filename>" is not provided, Token2Shell/MD assigns a random file name.

If ";base64" is present, <data> must be encoded in Base64; otherwise, <data> must be percent-encoded (whitespace characters are ignored).

Please note that ";charset=" parameter will be ignored even if provided. The character code page currently set for the terminal session will always be used.

Example:
data:text/plain;filename=hello.txt;base64,SGVsbG8gV29ybGQhCg==

Direct File Printing Sample Script

In order to make use of the direct file printing feature supported in Token2Shell/MD, the printing command and data must be sent from the server in data URI scheme. This can be easily accomplished by using tools and commands commonly found in Unix-like systems.

The following script demonstrates one of such possibilities. This script should work on most Unix-like systems with modern shells such as bash or tcsh.

#!/bin/sh

# Direct File Printing Sample Script for Token2Shell/MD
# http://choung.net/token2shell-md
# http://choung.net/howto/token2shell-md/local-print

[ $# -eq 0 ] && { echo "Usage: tpr filename"; exit 1; }
[ ! -f "$1" ] && { echo "ERROR: File does not exist."; exit 2; }

# Start printing
printf "\033[2016i"

# $1 holds the file path
# "file -b --mimetype $1" returns the mimetype (ex. text/plain)
printf "data:%s" "$(file -b --mime-type "$1")"

# Most Unix-like systems don't have any built-in command-line tool for percent-encoding
# "xxd" is used as a workaround. This also works for file names with multi-byte characters.

# "basename" returns only the file name (ex. path1/path2/filename -> filename)
# "xxd" converts the file name from "basename" to hex dump
# "sed" inserts "%" characters to the hex dump from "xxd"
printf ";filename=%s" "$(basename "$1" | xxd -p | sed 's/.\{2\}/%&/g')"

# "base64" outputs the content of the file in base64 encoding
printf ";base64,"
base64 "$1"

# End printing
printf "\033[4i"

After downloading, please check its content and make sure the tools and commands used in the script are supported by your server. Once you've confirmed its compatibility and made necessary changes, you should upload it to your "~/bin" folder and give it "execute" permission:

chmod 755 tpr

Once it's ready, you can print a PDF or text file over the current connection by issuing the "tpr" command (ex. tpr "/plot/lines.pdf").