Skip to content

Instantly share code, notes, and snippets.

@danil-z
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danil-z/9642347 to your computer and use it in GitHub Desktop.
Save danil-z/9642347 to your computer and use it in GitHub Desktop.
print receipt
- (NSData *)create2InchReceipt
{
User *user = [User getInstance];
NSMutableData *commands = [[NSMutableData alloc] init];
[commands appendBytes:"\x1d\x57\x80\x31"
length:sizeof("\x1d\x57\x80\x31") - 1]; // Page Area Setting <GS> <W> nL nH (nL = 128, nH = 1)
[commands appendBytes:"\x1b\x61\x01"
length:sizeof("\x1b\x61\x01") - 1]; // Center Justification <ESC> a n (0 Left, 1 Center, 2 Right)
[commands appendBytes:"\x1d\x21\x11" length:sizeof("\x1d\x21\x11") - 1]; // Width and Height
[commands appendBytes:"\x1b\x45\x01" length:sizeof("\x1b\x45\x01") - 1]; // Set Emphasized Printing ON
NSString *venueName = [NSString stringWithFormat:@"%@\n",user.currentVenue.name];
[commands appendData:[venueName dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendBytes:"\x1b\x45\x00" length:sizeof("\x1b\x45\x00") - 1]; // Set Emphasized Printing OFF (same command as on)
[commands appendBytes:"\x1d\x21\x00" length:sizeof("\x1d\x21\x00") - 1]; // Cancel Expansion
[commands appendData:[@"scan4beer\n\n" dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendBytes:"\x1b\x61\x00"
length:sizeof("\x1b\x61\x00") - 1]; // Left Alignment
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatDay = [[NSDateFormatter alloc]init];
[dateFormatDay setDateFormat:@"MM/dd/YYYY"];
NSDateFormatter *dateFormatTime = [[NSDateFormatter alloc]init];
[dateFormatTime setDateFormat:@"h:mm a"];
NSString *dateAndTime = [NSString stringWithFormat:@"Date: %@ Time: %@\n--------------------------------\n",[dateFormatDay stringFromDate:today],[dateFormatTime stringFromDate:today]];
[commands appendData:[dateAndTime dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendData:[@"1 Coke $3.00\n"
"2 Miller Beer $8.00\n"
"5 Water $6.00\n\n"
"Tip $1.00\n"
"--------------------------------\n"
"Total "
dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendBytes:"\x1d\x21\x11"
length:sizeof("\x1d\x21\x11") - 1]; // Width and Height Character Expansion <GS> ! n
[commands appendData:[@" $100.00\n" dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendBytes:"\x1d\x21\x00"
length:sizeof("\x1d\x21\x00") - 1]; // Cancel Expansion - Reference Star Portable Printer Programming Manual
[commands appendData:[@"\n\n" dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendBytes:"\x1b\x61\x01"
length:sizeof("\x1b\x61\x01") - 1]; // Center Justification <ESC> a n (0 Left, 1 Center, 2 Right)
[commands appendBytes:"\x1d\x21\x55" length:sizeof("\x1d\x21\x55") - 1]; // Width and Height Character Expansion <GS> ! n
[commands appendData:[@"ABCD\n" dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendBytes:"\x1d\x21\x00"
length:sizeof("\x1d\x21\x00") - 1]; // Cancel Expansion - Reference Star Portable Printer Programming Manual
[commands appendData:[@"\n" dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendBytes:"\x1b\x61\x00"
length:sizeof("\x1b\x61\x00") - 1]; // Left Alignment
[commands appendData:[@"\n"
" -------------------------------\n"
dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendBytes:"\x1d\x21\x22" length:sizeof("\x1d\x21\x22") - 1]; // Width and Height
[commands appendBytes:"\x1b\x45\x01" length:sizeof("\x1b\x45\x01") - 1]; // Set Emphasized Printing ON
[commands appendData:[@" 999 99 99\n" dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendBytes:"\x1b\x45\x00" length:sizeof("\x1b\x45\x00") - 1]; // Set Emphasized Printing OFF
[commands appendBytes:"\x1d\x21\x00" length:sizeof("\x1d\x21\x00") - 1]; // Cancel Expansion
[commands appendData:[@" Section Row Seat \n"
dataUsingEncoding:NSASCIIStringEncoding]];
[commands appendData:[@"\n\n\n\n\n" dataUsingEncoding:NSASCIIStringEncoding]];
return commands;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment