Printing a file on a printer in Ruby

I need help sending formatted text to a printer using Ruby on Ruby on Rails or sending a PDF file to a printer from a Ruby program. I can write code to create a pdf file from a Rails application, but I don’t know how to print this pdf file on the default printer. I am trying to write a small ticket selling application with Ruby on Rails.

+5
source share
4 answers

Here is the solution in Windows environement: Foxit! http://www.foxitsoftware.com/pdf/reader/

You can call foxit.exe (a stand-alone executable) to quietly print the PDF in the background.

: foxit.exe/t " .pdf" " "

shell = WIN32OLE.new('Shell.Application')

shell.ShellExecute("foxit.exe","/t \"#{filename}\" \"#{printer}\"")

+8

Linux Mac OS X "lpr", PDF ( Windows). :

def print_to_paper
  your_code_to_write_a_pdf_file("file.pdf")
  system("lpr", "file.pdf") or raise "lpr failed"
end
+6

, - . , , pdf, . , . .

0

. , Dell, , CSV, MS SCCM. . , , MAC- .

gLabels. gLabels, CSV , PDF , lpr Dymo Labelwriter.

"", , .

# Print all computers with printed = false
def self.print
  printed_labels = 0 
  csv_file = Tempfile.new(["computers", ".csv"])
  logger.debug("Writing #{csv_file.path}")
  begin
    Computer.transaction do
      Computer.unprinted.each do |computer|
        csv_file.puts "\"#{computer.mac(' ')}\",\"#{computer.hostname}\""
        computer.printed = true
        computer.save
        printed_labels += 1
      end
    end 
  ensure
    csv_file.close
    if csv_file.length > 0 
      pdf_file = Tempfile.new(["computers", ".pdf"])
      begin
        pdf_file.close
        system '/usr/bin/glabels-batch', "--input=#{csv_file.path}", "--output=#{pdf_file.path}", AssetBase::Application.config.computer_label
        system '/usr/bin/lpr', '-P', 'LabelWriter-450', pdf_file.path
      ensure
        pdf_file.unlink
      end
    end
    csv_file.unlink
  end   
  printed_labels
end

Fedora Linux, - CUPS, PDF, . CUPS .

There are other methods for creating structured text in PDF, but for shortcuts gLabels is great.

0
source

All Articles