$stdout to printer

Still trying to print stuff…
someone knows how to redirect $stdout to a printer??

Thanks!

Dimas C. wrote:

Still trying to print stuff…
someone knows how to redirect $stdout to a printer??

Thanks!

Have been researching this, and not coming up with much. However, I’m
getting a strong sense that we need to know your OS.

t.

Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< [email protected] >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

On 3 Jul 2009, at 22:15, Tom C. wrote:

Dimas C. wrote:

Still trying to print stuff…
someone knows how to redirect $stdout to a printer??

Thanks!

Have been researching this, and not coming up with much. However,
I’m getting a strong sense that we need to know your OS.

On most platforms the printer will be mapped to a device file by the
operating system, and this device file can be opened from Ruby like
any other regular file. Of course as to whether or not plain text
dumped to this file will result in the expected printed output is open
to question: for example a PostScript printer may well expect the
content written to the file to be a valid PostScript program.

Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net

raise ArgumentError unless @reality.responds_to? :reason

Tom C. wrote:

Dimas C. wrote:

Still trying to print stuff…
someone knows how to redirect $stdout to a printer??

Thanks!

Have been researching this, and not coming up with much. However, I’m
getting a strong sense that we need to know your OS.

t.

Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< [email protected] >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

I’m using windows.
I manage to get the printer`s name using wxruby’s PrintDialog, and tried
to redirect $stdout to it, but it doesn’t work…
In fact i’m trying to print a report made using Ruport. But i did not
find a way to do that inside ruport…

Anyone?

Dimas C. wrote:

Still trying to print stuff…
someone knows how to redirect $stdout to a printer??

Thanks!

Here’s a path which may solve a lot of problems for you - Ruby Ruports -
http://rubyreports.org. Looks flexible, and it appears that a lot of
thought and work has gone into this tool.

t.

Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< [email protected] >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

Dimas C. wrote:

I’m using windows.
I manage to get the printer`s name using wxruby’s PrintDialog, and tried
to redirect $stdout to it, but it doesn’t work…

So how did you attempt to redirect $stdout to it? From within Ruby?
(Show the code) From a batch file which runs your Ruby script? (Show the
code)

In fact i’m trying to print a report made using Ruport.

It’s a long time since I fought with Ruport and its awful API, but I did
document what I found out on the Wiki. Here it is:

http://stonecode.svnrepository.com/ruport/trac.cgi/wiki/DimwitsGuide

If Ruport currently returns the report to you as a string, then just:

report = … do something with Ruport …
File.open(“out.txt”,“w”) { |f| f.write(report) }

But it should be possible to tell it to write directly to a file, by
passing :io as an option to the relevant rendering call. Something like
this:

ro = { … rendering options … }
fmt = :html
File.open(“out.txt”, “w”) { |f| report.as(fmt, {:io=>f}.merge(ro)) }

HTH,

Brian.

All this assumes you can open a channel to your printer from Ruby.
Options you could try, as I’m afraid I don’t have a Windows box with a
printer:

(1) File.open(“prn:”,“w”) { |f| … }

(2) IO.popen(“print”,“w”) { |f| … }

(or whatever the command-line tool is for printing a file)

(3) Write the report to a file, then you must be able to print it
somehow.

system(“print out.txt”)
system(“type out.txt >prn”)
… ?

On 6 Jul 2009, at 19:56, Dimas C. wrote:

Anyone?

What OS are you using? What printer do you want to output to? Do you
know the device name of the printer?

Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net

raise ArgumentError unless @reality.responds_to? :reason

Hi,

Am Dienstag, 07. Jul 2009, 20:10:53 +0900 schrieb Brian C.:

(1) File.open(“prn:”,“w”) { |f| … }
(2) IO.popen(“print”,“w”) { |f| … }

The original question was how to redirect $stdout.

def print_it
IO.popen “lpr -o some_option”, “w” do |f|
$stdout = f
yield
end
ensure
$stdout = STDOUT
end

Sorry, I can only test and will only provide the UNIX version.

Bertram

On Jul 7, 2:32 pm, Bertram S. [email protected] wrote:

IO.popen "lpr -o some_option", "w" do |f|


Bertram S.
Stuttgart, Deutschland/Germanyhttp://www.bertram-scharpf.de

Not sure about redirecting stdout on windows, best i can see is that
you redirect to a temp file
and print that.

if the file is a recognised type on windows (e.g. .pdf and adobe
reader is installed)
try this :
require ‘win32ole’
SHELL_APP = WIN32OLE.new(‘Shell.Application’)
SHELL_APP.ShellExecute(“test.pdf”, ‘’, “C:\Temp”, ‘print’, 0)

otherwise you can try printing to a shared network printer:
PRINTER = “\\192.168.1.1\shared_printer”
system “print /d:#{PRINTER} C:\Temp\test.txt”

What OS are you using? What printer do you want to output to? Do you
know the device name of the printer?

I’m using windows.
I actually like the user to choose the printer. I can get the printer
name with wxRuby’s
PrintDialog#get_print_dialog_data.get_print_data.get_printer_name.

So how did you attempt to redirect $stdout to it? From within Ruby?
(Show the code) From a batch file which runs your Ruby script? (Show the
code)

I have tried to redirect $stdout to this name, but don’t know if that’s
suposed to work…

Brian C., i have already writed the ruport data to a file
(test.pdf). I’m having trouble passing this file to the printer.

The original question was how to redirect $stdout.

def print_it
IO.popen “lpr -o some_option”, “w” do |f|
$stdout = f
yield
end
ensure
$stdout = STDOUT
end

I will try that. Thanks

require ‘win32ole’
SHELL_APP = WIN32OLE.new(‘Shell.Application’)
SHELL_APP.ShellExecute(“test.pdf”, ‘’, “C:\Temp”, ‘print’, 0)

otherwise you can try printing to a shared network printer:
PRINTER = “\\192.168.1.1\shared_printer”
system “print /d:#{PRINTER} C:\Temp\test.txt”

I have tried both. I was able to print with these, but after the
printing they leave a blank Adobe Reader window open. I tried to change
last argument of the ShellExecute, but it doesn’t work.
It’s the solution i’m using at the moment, but it’s not working
properlly.

Thanks everybody!