Forum: Ruby $stdout to printer

Posted by Dimas Cyriaco (dimascyriaco)
on 2009-07-03 21:07
Still trying to print stuff...
someone knows how to redirect $stdout to a printer??

Thanks!
Posted by Tom Cloyd (Guest)
on 2009-07-03 23:15
(Received via mailing list)
Dimas Cyriaco 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 Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Posted by Eleanor McHugh (Guest)
on 2009-07-03 23:32
(Received via mailing list)
On 3 Jul 2009, at 22:15, Tom Cloyd wrote:
> Dimas Cyriaco 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
Posted by Tom Cloyd (Guest)
on 2009-07-03 23:57
(Received via mailing list)
Dimas Cyriaco 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 Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Posted by Dimas Cyriaco (dimascyriaco)
on 2009-07-04 01:03
Tom Cloyd wrote:
> Dimas Cyriaco 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 Cloyd, MS MA, LMHC - Private practice Psychotherapist
> Bellingham, Washington, U.S.A: (360) 920-1226
> << tc@tomcloyd.com >> (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...
Posted by Dimas Cyriaco (dimascyriaco)
on 2009-07-06 20:56
Anyone?
Posted by Eleanor McHugh (Guest)
on 2009-07-07 12:32
(Received via mailing list)
On 6 Jul 2009, at 19:56, Dimas Cyriaco 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
Posted by Brian Candler (candlerb)
on 2009-07-07 13:08
Dimas Cyriaco 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.
Posted by Brian Candler (candlerb)
on 2009-07-07 13:10
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")
  ... ?
Posted by Bertram Scharpf (Guest)
on 2009-07-07 15:33
(Received via mailing list)
Hi,

Am Dienstag, 07. Jul 2009, 20:10:53 +0900 schrieb Brian Candler:
> (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
Posted by Gilbo (Guest)
on 2009-07-08 14:00
(Received via mailing list)
On Jul 7, 2:32 pm, Bertram Scharpf <li...@bertram-scharpf.de> wrote:
>     IO.popen "lpr -o some_option", "w" do |f|
>
> --
> Bertram Scharpf
> 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"
Posted by Dimas Cyriaco (dimascyriaco)
on 2009-07-09 17:41
> 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 Candler, 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!
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.