Printing a file to a printer in Ruby
Solution 1
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}\"")
Solution 2
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
Solution 3
I'm not sure if there is a way to print something on default printer from web. In my bank when I want to get payment confirmation they give me pdf and I have to print it manualy. I think it is good solution. Just add some information about it.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on June 09, 2022Comments
-
Admin about 2 years
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.