Skip to content

Commit

Permalink
Merge pull request #13 from mtking2/windows_printing_support
Browse files Browse the repository at this point in the history
Windows Printing Support
  • Loading branch information
bbulpett authored Jun 12, 2017
2 parents ead84a4 + 749a654 commit dc26bdf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
49 changes: 33 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#### This is a gem based on a terrific older gem by Cassio Marques. Although the new printers are mostly compatible with old Eltron (Epl) code, my needs require many of the new Zebra (ZPL) functions.
#### This is a gem based on a terrific older gem by Cassio Marques. Although the new printers are mostly compatible with old Eltron (Epl) code, my needs require many of the new Zebra (ZPL) functions.

# Zebra::Zpl

### ToDo: Update documentation with instructions for new features such as font sizing, margins, and text alignment

Zebra::Zpl offers a Ruby DSL to design and print labels using the ZPL programming language.
Zebra::Zpl offers a Ruby DSL to design and print labels using the ZPL programming language.

## Installation

Expand Down Expand Up @@ -38,21 +38,21 @@ With a label, you can start adding elements to it:
label = Zebra::Zpl::Label.new :print_density => 8, :print_speed => 3
text = Zebra::Zpl::Text.new :data => "Hello, printer!", :position => [100, 100], :font_size => Zebra::Zpl::FontSize::SIZE_2
label << text

You can add as many elements as you want.

### Printing the labels

You need to have your printer visible to CUPS. Once your printer is configured and you know its name on CUPS, you can send the labels to the printer using a `Zebra::PrintJob` instance.
You need to have your printer visible to CUPS (or shared on the network in Windows). Once your printer is configured and you know its name on CUPS (or the Windows shared printer name), you can send the labels to the printer using a `Zebra::PrintJob` instance.

label = Zebra::Zpl::Label.new(
:width => 200,
:length => 200,
:print_speed => 3,
:print_density => 6
)


barcode = Zebra::Zpl::Barcode.new(
:data => "12345678",
:position => [50, 50],
Expand All @@ -62,14 +62,31 @@ You need to have your printer visible to CUPS. Once your printer is configured a
:wide_bar_width => 8,
:type => Zebra::Zpl::BarcodeType::CODE_128_AUTO
)

label << barcode
print_job = Zebra::PrintJob.new "your-printer-name-on-cups"

print_job = Zebra::PrintJob.new "<your-printer-name-on-cups/windows-shared-printer-name>"

print_job.print label

This will persist the label contents to a tempfile (using Ruby's tempfile core library) and copy the file to the printer using either `lpr -P <your-printer-name-on-cups> -o raw <path-to-the-temp-file>` (if you're on Mac OSX) or `lp -d <your-printer-name-on-cups> -o raw <path-to-the-tempfile>` (if you're on Linux). All the tempfile creation/path resolution, as well as which command has to be used, are handled by the `PrintJob` class.
This will persist the label contents to a tempfile (using Ruby's tempfile core library) and copy the file to the printer using either `lpr -P <your-printer-name-on-cups> -o raw <path-to-the-temp-file>` (if you're on Mac OSX) or `lp -d <your-printer-name-on-cups> -o raw <path-to-the-tempfile>` (if you're on Linux). All the tempfile creation/path resolution, as well as which command has to be used, are handled by the `PrintJob` class.

### Printing to directly to Windows LPD
This gem also supports printing directly to shared printer on Windows using LPD.
In order to print directly to a LPD on a Windows machine you need two things:
- [rlpr](http://manpages.ubuntu.com/manpages/xenial/man1/rlpr.1.html) installed on the (UNIX) system running your app that uses this gem.<sup>[1](#fn1)</sup>
- LPD Print Service and LPR Port Monitor features enabled on the Windows machine.<sup>[2](#fn2)</sup>

<p align="center">
<img align="center" src="http://i.imgur.com/3CWkEWU.png" style="height: 250px"/>
<p/>

<hr/>

<a name="fn1">1</a>. On a distro such as Ubuntu simply do: `sudo apt-get install rlpr`
If using OSX then you will have to manually build it from source and add it to your `$PATH` environment variable.

<a name="fn2">2</a>. The printer name that you pass in must correspond with the **shared printer name** on the Windows machine.

### Printing QR codes

Expand All @@ -92,17 +109,17 @@ This will persist the label contents to a tempfile (using Ruby's tempfile core l
print_job = Zebra::PrintJob.new "your-qr-printer-name-on-cups"

print_job.print label

### Available elements

#### Text

You create text elements to print using instances of the `Zebra::Zpl::Text` class. It accepts the following options:

* `position`: An array with the coordinates to place the text, in dots.
* `position`: An array with the coordinates to place the text, in dots.
* `rotation`: The rotation for the text. More about the possible values below.
* `data`: The text to be printed.
* `v_multiplier`: The vertical multiplier to use.
* `v_multiplier`: The vertical multiplier to use.
* `h_multiplier`: The horizontal multipler to use.
* `print_mode`: The print mode. Can be normal ("N") or reverse ("R").
* `font_size`: The font size to use. You can use values between 1 and 5.
Expand All @@ -117,7 +134,7 @@ For the print modes, you can also use the constants:

You create barcode elements to print using instances of the `Zebra::Zpl::Barcode` class. It accepts the following options:

* `position`: An array with the coordinates to place the text, in dots.
* `position`: An array with the coordinates to place the text, in dots.
* `height`: The barcode's height, in dots.
* `rotation`: The rotation for the text. More about the possible values below.
* `data`: The text to be printed.
Expand Down Expand Up @@ -150,7 +167,7 @@ You can create QR Codes elements to print using instances of the `Zebra::Zpl::Qr
You can draw boxes in your labels:

box = Zebra::Zpl::Box.new :position => [20, 20], :end_position => [100, 100], :line_thickness => 39

#### Elements Rotation

All printable elements can be rotated on the label, using the `:Rotation` option. The accepted rotation values are:
Expand Down
3 changes: 2 additions & 1 deletion lib/zebra/print_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def print(label, ip)

def send_to_printer(path)
puts "* * * * * * * * * * * * Sending file to printer #{@printer} at #{@remote_ip} * * * * * * * * * * "
`lp -h #{@remote_ip} -d #{@printer} -o raw #{path}`
result = system("rlpr -H #{@remote_ip} -P #{@printer} -o raw #{path} 2>&1") # try printing to LPD on windows machine first
system("lp -h #{@remote_ip} -d #{@printer} -o raw #{path}") if !result # print to unix (CUPS) if rlpr failed
end
end
end

0 comments on commit dc26bdf

Please sign in to comment.