# $stdout to printer

**URL:** <https://www.ruby-forum.com/t/stdout-to-printer/171516>\
**Category:** Ruby\
**Created:** [July 3, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516 "2009-07-03T21:07:50Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 3, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/1 "2009-07-03T21:07:50Z")

</div>

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

Thanks!

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 3, 2009, 11:15pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/2 "2009-07-03T23:15:53Z")

</div>

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.

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 3, 2009, 11:32pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/3 "2009-07-03T23:32:15Z")

</div>

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](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 4, 2009, 1:03am UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/4 "2009-07-04T01:03:19Z")

</div>

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.
> 
> –
> 
> ```auto
> Tom C., MS MA, LMHC - Private practice Psychotherapist
> Bellingham, Washington, U.S.A: (360) 920-1226
> << removed_email_address@domain.invalid >> (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…

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 6, 2009, 8:56pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/5 "2009-07-06T20:56:50Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 3, 2009, 11:57pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/6 "2009-07-03T23:57:57Z")

</div>

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](http://rubyreports.org). Looks flexible, and it appears that a lot of  
thought and work has gone into this tool.

t.

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 7, 2009, 1:08pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/7 "2009-07-07T13:08:19Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 7, 2009, 1:10pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/8 "2009-07-07T13:10:46Z")

</div>

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”)  
… ?

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 7, 2009, 12:32pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/9 "2009-07-07T12:32:48Z")

</div>

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](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 7, 2009, 3:33pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/10 "2009-07-07T15:33:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 8, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/11 "2009-07-08T14:00:48Z")

</div>

On Jul 7, 2:32 pm, Bertram S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”

---

<div class="post-metadata">

**Author:** ![dimascyriaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dimascyriaco](https://www.ruby-forum.com/u/dimascyriaco)\
**Post date:** [July 9, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/stdout-to-printer/171516/12 "2009-07-09T17:41:31Z")

</div>

> 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!
