Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a file to a printer in Ruby

I need help with sending a formatted text to a printer using Ruby on Ruby on Rails OR sending a pdf file to a printer from Ruby program. I can write the code to create a pdf file from Rails app but don't know how to print that pdf file to a default printer. I am trying to write a small ticketing application with Ruby on Rails.

like image 671
Shreyans Avatar asked Oct 22 '09 16:10

Shreyans


2 Answers

On Linux and Mac OS X you can use the "lpr" command line program, passing it the name of the PDF file (not sure about Windows though). For example:

def print_to_paper
  your_code_to_write_a_pdf_file("file.pdf")
  system("lpr", "file.pdf") or raise "lpr failed"
end
like image 111
wdebeaum Avatar answered Nov 15 '22 00:11

wdebeaum


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

You can call foxit.exe (standalone executable) to silently print a PDF in background.

Syntax : foxit.exe /t "your file.pdf" "The Printer Name"

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

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

like image 36
P.Bader Avatar answered Nov 15 '22 00:11

P.Bader