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:

Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165

3 Answers3

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...]
mamackenzie
  • 1,156
  • 7
  • 13
  • 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. – Johannes Fahrenkrug Aug 09 '12 at 06:28
  • Also, I'd like to use a Cocoa/Foundation/C API without calling an external CLI command. If possible :) – Johannes Fahrenkrug Aug 09 '12 at 10:21
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!

mamackenzie
  • 1,156
  • 7
  • 13
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.

EmbeddedGuy
  • 349
  • 2
  • 11
  • This is very helpful! Do you have a link to the reference for the different commands you can send? – Johannes Fahrenkrug Dec 06 '17 at 15:11
  • 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-P900W – EmbeddedGuy Dec 07 '17 at 09:38
  • The command above is sent to the printer but it's not printing anything :,( – iSebbeYT Jul 09 '22 at 09:54