Code Monkey home page Code Monkey logo

escpos's Introduction

Build Status

Escpos

A ruby implementation of ESC/POS (thermal) printer command specification.

Installation

Add this line to your application's Gemfile:

gem 'escpos'

# see https://github.com/escpos/escpos-image
gem 'escpos-image' # add this if you want to print images

And then execute:

$ bundle

Or install it yourself as:

$ gem install escpos

Image support

To keep this gem lightweight and modular image support was implemented in another gem:

https://github.com/escpos/escpos-image

# Add this line to your application's Gemfile if you want to print images
gem 'escpos-image'
# And depending on your image processor of choice
gem 'mini_magick'
# or
gem 'chunky_png'

Or install it yourself as:

gem install escpos-image
# and then depending on your image processor of choice
gem install mini_magick
# or
gem install chunky_png

For more information about image processors, their options and supported formats please see https://github.com/escpos/escpos-image readme file.

Examples

Basic usage

@printer = Escpos::Printer.new
@printer << "Some text"
@printer << Escpos::Helpers.big "Big text"

@printer.to_escpos # returns ESC/POS data ready to be sent to printer
# on linux this can be piped directly to /dev/usb/lp0
# with network printer sent directly to printer socket (see example below)
# with serial port printer it can be sent directly to the serial port

@printer.to_base64 # returns base64 encoded ESC/POS data

Report class usage

# my_report.rb:

class MyReport < Escpos::Report
  def item(text)
    @count ||= 0
    @count += 1
    bold "#{@count}. #{text}"
  end

  def order
    options[:order]
  end
end
<% # my_report.erb: %>

<%= big "Order number #{order[:number]}" %>
<%= item "First item" %>
<%= item "Second item" %>
<%= item "Third item" %>
report = MyReport.new 'path/to/my_report.erb', {
  order: { number: 123 }
}
@printer << report.render
@printer.cut!

@printer.to_escpos # returns ESC/POS data ready to be sent to printer
# - on linux this can be piped directly to /dev/usb/lp0
# - with network printer sent directly to printer socket
# - with serial port printer it can be sent directly to the serial port
# see example below

@printer.to_base64 # returns base64 encoded ESC/POS data

Printing to Linux/UNIX/BSD block device (USB/Serial/LPT)

printer = Escpos::Printer.new
printer << "Some text"

# change /dev/usb/lp0 to match the printer device
# e.g. on Linux this may be /dev/ttyS0 for serial port
File.open("/dev/usb/lp0", "w") { |f| f.write printer.to_escpos }

Network printing

require "socket"

printer = Escpos::Printer.new
printer << "Some text"

# change 192.168.2.7 and 9100 to match the IP or host and port of the printer
socket = TCPSocket.new "192.168.2.7", 9100

socket.write printer.to_escpos
socket.close

Available helper methods

Method name Description
text Normal text formatting
encoding, set_encoding, set_printer_encoding Set printer encoding (see example below)
encode Encode text for the printer (see example below)
double_height Double height text
quad_text, big, title, header, double_width_double_height, double_height_double_width Double width & Double height text
double_width Double width text
underline, u Underlined text
underline2, u2 Stronger underlined text
bold, b Bold text
left Align to left
right Align to right
center Align to center
invert, inverted Color inverted text
black, default_color, color_black, black_color Default Color (Usually black)
red, alt_color, alternative_color, color_red, red_color Alternative Color (Usually Red)
barcode Print barcode (see example below)
partial_cut Partially cut the paper (may not be available on all devices)
cut Fully cut the paper (may not be available on all devices)

Encoding & diacritics

To print diacritics (accented characters) with ESC/POS two things have to be done. First the desired code page must be set on the printer (can be done using an ESC/POS command) and the desired text has to be encoded to the code page set on the printer.

printer = Escpos::Printer.new
printer << Escpos::Helpers.set_printer_encoding(Escpos::CP_ISO8859_2)
printer << Escpos::Helpers.encode("This is UTF-8 to ISO-8859-2 text: ěščřžýáíéúů", encoding: "ISO-8859-2")

Some printers (e.g. Epson TM line) allow setting a default code page in printer setup, then the set_printer_encoding call can be omitted.

Printing barcodes

The barcode helper accepts barcode data as first argument and an options hash as second.

Possible options:

Option Possible values Description
format Escpos::BARCODE_UPC_A: Barcode type UPC-A
Escpos::BARCODE_UPC_E: Barcode type UPC-E
Escpos::BARCODE_EAN13: Barcode type EAN13
Escpos::BARCODE_EAN8: Barcode type EAN8
Escpos::BARCODE_CODE39: Barcode type CODE39
Escpos::BARCODE_ITF: Barcode type ITF
Escpos::BARCODE_NW7: Barcode type NW7
Type of barcode
text_position Escpos::BARCODE_TXT_OFF: no text, only barcode
Escpos::BARCODE_TXT_ABV: text positioned above the barcode
Escpos::BARCODE_TXT_BLW: text positioned below the barcode
Escpos::BARCODE_TXT_BTH: text positioned both above and below the barcode
Text position
height 1 to 255 Barcode height
width 2 to 6 Barcode width
barcode_data = Escpos::Helpers.barcode("12345678", {
  format: Escpos::BARCODE_CODE39,
  text_position: Escpos::BARCODE_TXT_BLW,
  height: 50,
  width: 3
})

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/escpos/escpos.

  1. Fork it ( https://github.com/escpos/escpos/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

escpos's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

escpos's Issues

Is it possible to use ActionView::Helpers inside Escpos::Report?

Hi, I'm trying to build a feature using the Escpos::Report.

But when I try to use ActionView::Helpers (already included in the Report Class), it throws a weird error:

#<NoMethodError: undefined method `in_rendering_context' for <EscPosServices::Reports::Invoice:0x00007f849e226af8>>

I leave an example:

# invoice.rb
class EscPosServices::Reports::Invoice < Escpos::Report
  include ActionView::Helpers

  def sale
    options[:sale]
  end

  def current_tenant
    options[:current_tenant]
  end
end
<%= right(bold("Subtotal: ") + number_to_currency(sale.subtotal)) %>
<%= right(bold("Descuento: ") + number_to_currency(sale.discount_amount)) %>

Versions:
Rails 6.0.3.7
Escpos gem 0.0.10
Ruby 2.7.3

Chinese scrambled code

Hi , I use @printer.to_escpos the Chinese but show my scrambled code. how can i solve it。thanks for your help

concatenating helpers?

hi,

thank you for your effort in putting this great implementation together.

do you have any advice for concatenating helper methods?
for example, applying 'big' and 'center' to a string simultaneously.

thank you,
spencer

not well documented, need clarification

Docs not giving actual example but using hard coded data like for some static report

<%= item "First item" %>
<%= item "Second item" %>
<%= item "Third item" %>

There should have been some actual object like an order with items etc.

Now how to pass an object report.erb from
report = MyReport.new 'path/to/my_report.erb'

than using multiple styles, its just showing quad_text
& exactly at which point it sends data to printer

Helper methods not available

Hi,

First of all thank you for your work.
I have a problem with helpers, if I try to use them I always get for instance "NoMethodError (undefined method `center'").

this is my report class:

class MerchantInfo < Escpos::Report

  def cfe_data
    options[:cfe_data]
  end

  def merchant_title text
    center "#{text}"
  end

  def self.create_merchant_info
    layout_dir = Rails.root.join('app/views/api/v1/thermal_cfe/merchant_info.erb').to_s
    cfe_layout = MerchantInfo.new(layout_dir,
                                  {
                                      cfe_data:Hash(merchant:COMMERCES[0])
                                  }
    )
    puts "cfe_layout #{cfe_layout.to_json}"
    cfe_layout
  end
end

this is my view file:
#merchant_info.erb

<%=merchant_title cfe_data[:merchant]%>

ruby version 2.5.3
rails version 5.2.1

Thank you in advance for your help.

Set UTF-8 LATIN1 encoding

How I set th encoding to work on LATIN1 on CP_CP858

I trying to

text = Escpos::Printer.new
text << Escpos::Helpers.set_printer_encoding(Escpos::CP_CP858)
text << Escpos::Helpers.encode(ptr_text, encoding: "CP-858")

but I having the error code converter not found (UTF-8 to CP-858)

how I use this code table?

Printer compatibility

This looks like a brilliant gem. Could you confirm which printers you know work with this?

I've tried your sample code with my Brother TD-4000 but so far it's stubbornly silent.

Can't make it work on Debian

Hello,
I have everything setup as said in the documentation but have nothing when I try. Is there anything else to install or configure?

wrong example codes

[29] pry(main)> @printer = Escpos::Printer.new
=> #<Escpos::Printer:0x00007efc2e1bbd78 @data="\e@">
[30] pry(main)> @printer << "Some text"
=> "\e@Some text"
[31] pry(main)> @printer << Escpos::Helpers.big("Big text")
NoMethodError: undefined method `big' for Escpos::Helpers:Module

even had to wrap big call in brackets, otherwise syntax error.

Ruby 3.2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.