How to send low-level escape sequences to a printer on OS X?
Asked Answered
S

3

5

I have a string of printer escape sequences (ESC/P) that I need to send to a printer (either USB or network) on Mac OS X. How can I do that? Is using CUPS directly the best way? Is there a "higher level" way?

And before you ask: I really do need to send escape sequences and can't simply use the high-level printing system.

Edit: These are some projects and resources that provide similar functionality in other languages like Java:

Slaw answered 18/7, 2012 at 12:10 Comment(2)
How do you know you need escape sequence? Are you working around a bug in the Epson driver?Flavor
More or less. It's a client requirement and unfortunately I can't go into any more details.Slaw
D
3

I would imagine that the best way to do this would be to just use CUPS/lp:

echo "ctrl_char" | cat file_to_print - | lp [...flags...]
Delectate answered 9/8, 2012 at 3:30 Comment(2)
Thank you, but unfortunately it's not that easy: we don't need to simply print files. The escape sequences the we want to send to the printer include all the (image) data that needs to be printed.Slaw
Also, I'd like to use a Cocoa/Foundation/C API without calling an external CLI command. If possible :)Slaw
D
3

You can send the control characters in C using unix system calls:

char escp_seq[BYTES_FOR_SEQUENCE];

// ...initialize the ESC/P sequence.

int printer_fd = open("/dev/lp0", O_WRONLY);
ssize_t bytes_written = write(printer_fd, escp_seq, sizeof(escp_seq));
close(printer_fd);

As for the printable data itself, you can use Core Printing.

Using this library, you can use a either

OSStatus PMPrinterPrintWithProvider (
   PMPrinter printer,
   PMPrintSettings settings,
   PMPageFormat format,
   CFStringRef mimeType,
   CGDataProviderRef provider
);

which would require you to construct a CGDataProviderRef with your data.

Alternatively, you could use a file:

OSStatus PMPrinterPrintWithFile (
   PMPrinter printer,
   PMPrintSettings settings,
   PMPageFormat format,
   CFStringRef mimeType,
   CFURLRef fileURL
);

I apologize for not being able to give a concrete code example, but I really am not sure how you intend to construct the data that you wish to send to the printer. This should at least provide a basis for you to start hacking around with. I am sure there is more than enough stuff in the above-provided Apple docs to accomplish your task.

Good luck!

Delectate answered 9/8, 2012 at 16:28 Comment(1)
Thank you very much for your help and work, Michael! It might still not be 100% what I'm looking for, but it's a start, thank you!Slaw
A
3

We are printing from Mac bash shell using scripting (in professional automated End Of Line product tester).

Very simplified "Hello" label prints like this:

data="\x1Bia\x00"
data="${data}\x1B@"
data="${data}\x1BX\x32"
data="${data}Hello"
data="${data}\x0C"

# Check your own binary for debugging:
echo -ne $data | hexdump -C

# print the actual label
echo -ne $data | lp -d Brother_PT_P900W

Note, that you must replace the printer name with your own printer name.

Ammonia answered 5/12, 2017 at 14:21 Comment(3)
This is very helpful! Do you have a link to the reference for the different commands you can send?Slaw
Not sure what do you mean by 'commands': a) bash shell command; or b) ESC/P commands? If you mean b), I downloaded the PDF documents from the printer manufacturer, in my case Brother PT-P900WAmmonia
The command above is sent to the printer but it's not printing anything :,(Hymenium

© 2022 - 2024 — McMap. All rights reserved.