# Using OCRA, FXRuby and pdf-writer problem

**URL:** <https://www.ruby-forum.com/t/using-ocra-fxruby-and-pdf-writer-problem/226056>\
**Category:** Ruby\
**Created:** [December 9, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/using-ocra-fxruby-and-pdf-writer-problem/226056 "2009-12-09T15:35:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tutiplain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tutiplain](https://www.ruby-forum.com/u/tutiplain)\
**Post date:** [December 9, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/using-ocra-fxruby-and-pdf-writer-problem/226056/1 "2009-12-09T15:35:34Z")

</div>

Hello all,

I made a simple salary calculator as practice using FXRuby, OCRA to  
create an .exe file and pdf-writer to generate a small “report” with the  
results of the calculation. The program runs fine when run with the  
ruby interpreter, although I get some warnings:

C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer/lang.rb:33:  
warni  
ng: method redefined; discarding old language=  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer/strokestyle.rb:44  
: warning: method redefined; discarding old cap=  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer/strokestyle.rb:70  
: warning: method redefined; discarding old join=  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:434:  
warning:  
method redefined; discarding old compressed=  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:471:  
warning:  
method redefined; discarding old absolute\_left\_margin  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:476:  
warning:  
method redefined; discarding old absolute\_right\_margin  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:481:  
warning:  
method redefined; discarding old absolute\_top\_margin  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:486:  
warning:  
method redefined; discarding old absolute\_bottom\_margin  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:492:  
warning:  
method redefined; discarding old margin\_height  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:497:  
warning:  
method redefined; discarding old margin\_width  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:502:  
warning:  
method redefined; discarding old absolute\_x\_middle  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:507:  
warning:  
method redefined; discarding old absolute\_y\_middle  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:512:  
warning:  
method redefined; discarding old margin\_x\_middle  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:517:  
warning:  
method redefined; discarding old margin\_y\_middle  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:526:  
warning:  
method redefined; discarding old y=  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:535:  
warning:  
method redefined; discarding old pointer=  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:1878:  
warning:  
method redefined; discarding old column\_width  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:1885:  
warning:  
method redefined; discarding old column\_gutter  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:1891:  
warning:  
method redefined; discarding old column\_number  
C:/Ruby/lib/ruby/gems/1.8/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:1897:  
warning:  
method redefined; discarding old column\_count

Here is my code:  
require ‘fox16’  
require ‘pdf/writer’  
include Fox

#crea PdfViewer  
File.open(‘pdfviewer.bat’,‘w’) do |f| f.write ‘%1’ end

class SueldoCalc \< FXMainWindow  
def initialize(app)  
super(app,“SueldoCalc 1.0” , :opts=\>  
DECOR\_TITLE|DECOR\_BORDER|DECOR\_CLOSE)

```
@descuentos = Hash.new

#seccion principal
@packer = FXPacker.new(self, :opts=>LAYOUT_FILL)

#seccion horas
@seccion_horas = FXHorizontalFrame.new(@packer,:opts

```

=\>LAYOUT\_FILL\_X|PACK\_UNIFORM\_WIDTH)  
@lblHoras = FXLabel.new(@seccion\_horas,“Entre las horas  
trabajadas:”,:opts=\>LAYOUT\_FILL\_X|JUSTIFY\_LEFT)  
@txtHoras= FXTextField.new(@seccion\_horas,20,:opts=\>LAYOUT\_FILL\_X |  
TEXTFIELD\_INTEGER)

```
#seccion rate
@seccion_rate = FXHorizontalFrame.new(@packer,:opts

```

=\>LAYOUT\_FILL\_X|PACK\_UNIFORM\_WIDTH)  
@lblRate = FXLabel.new(@seccion\_rate,“Entre las  
rate/hora:”,:opts=\>LAYOUT\_FILL\_X | JUSTIFY\_LEFT)  
@txtRate= FXTextField.new(@seccion\_rate,20,:opts=\>LAYOUT\_FILL\_X |  
TEXTFIELD\_REAL )

```
#seccion botones
@botones = FXHorizontalFrame.new(@packer,:opts

```

=\>LAYOUT\_FILL\_X|PACK\_UNIFORM\_WIDTH)  
@btnSubmit = FXButton.new(@botones,“Calcular”, :opts=\>  
BUTTON\_NORMAL|LAYOUT\_FILL\_X)  
@btnDesc =  
FXButton.new(@botones,“Descuentos”,:opts=\>BUTTON\_NORMAL|LAYOUT\_FILL\_X)  
@btnExit = FXButton.new(@botones,“Salir”, :opts=\>  
BUTTON\_NORMAL|LAYOUT\_FILL\_X)

```
#eventos
@btnSubmit.connect(SEL_COMMAND) do
  sueldo = @txtHoras.to_s.to_f * @txtRate.to_s.to_f

  #calcula descuentos
  total_descuentos = 0
  @descuentos.each_pair { |key,desc|
    cant_descontar = (desc/100) * sueldo
    total_descuentos = total_descuentos +cant_descontar
  }

  sueldo_neto = sueldo-total_descuentos

  pdf = PDF::Writer.new
  #pdf.select_font "Times-Roman"
  pdf.text "SueldoCalc 1.0", :justification =>

```

:center,:font\_size=\>50  
pdf.text “\_” \* 21

```
  pdf.text "Informe de cÃ¡lculo", :justification =>

```

:center,:font\_size=\>24

```
  pdf.text "Sueldo Bruto: #{sprintf('%5.2f',sueldo)}",:font_size=>16
  pdf.text "Sueldo Neto: #{sprintf('%5.2f',sueldo_neto)}"
  pdf.text "Descuentos Aplicados:"
  #mensaje full
  #mensaje = "Sueldo Bruto: #{sprintf('%5.2f',sueldo)}\n"
  #mensaje = mensaje +"Sueldo Neto:

```

#{sprintf(’%5.2f’,sueldo\_neto)}\n"  
#mensaje = mensaje + “Descuentos Aplicados:\n”

```
  @descuentos.each_pair { |key,value|
    pdf.text " "*5+key+": "+value.to_s+" \% \n"
  }
  pdf.text ' '
  pdf.text "Gracias por utlizar nuestro programa!", :justification

```

=\> :center

```
  File.open("Sueldocalc.pdf","w") { |f| f.write pdf.render }

  system "PdfViewer.bat Sueldocalc.pdf"

  #FXMessageBox.information(self,MBOX_OK,"SueldoCalc",mensaje)

end

@btnExit.connect(SEL_COMMAND) do
  exit
end

@btnDesc.connect(SEL_COMMAND) do
  desc_form = Descuento.new(app,@descuentos)
  desc_form.create
end

```

end

def create  
super  
show(PLACEMENT\_SCREEN)  
end

end

class Descuento \< FXMainWindow

def Carga\_Descuentos  
@lstDescuentos.clearItems()  
@descuentos.each\_pair { |key,value| @lstDescuentos.appendItem( key +  
': '+ value.to\_s) }  
end

def initialize(app,descuentos)  
super(app,“Descuentos”,:opts=\>DECOR\_TITLE|DECOR\_BORDER|DECOR\_CLOSE)

```
#trae los descuentos de la forma principal
@descuentos = descuentos

#frame principal (vertical)
@main_frame =

```

FXVerticalFrame.new(self,:opts=\>LAYOUT\_FILL|PACK\_UNIFORM\_WIDTH)

```
#seccion de aÃ±Ã¡dir descuentos
#seccion de escribir el nombre
@add_desc_frame = FXHorizontalFrame.new(@main_frame,:opts=>

```

LAYOUT\_FILL\_X|PACK\_UNIFORM\_WIDTH)  
@lblNombre= FXLabel.new(@add\_desc\_frame,“Nombre de  
descuento:”,:opts=\>LAYOUT\_FILL\_X|JUSTIFY\_LEFT)  
@txtNombreDesc =  
FXTextField.new(@add\_desc\_frame,20,:opts=\>LAYOUT\_FILL\_X)

```
#seccion de escribir el descuento
@add_num_frame = FXHorizontalFrame.new(@main_frame,:opts=>

```

LAYOUT\_FILL\_X|PACK\_UNIFORM\_WIDTH)  
@lblCantidad= FXLabel.new(@add\_num\_frame,“Cantidad a  
descontar:”,:opts=\>LAYOUT\_FILL\_X|JUSTIFY\_LEFT)  
@txtCantidad =  
FXTextField.new(@add\_num\_frame,20,:opts=\>LAYOUT\_FILL\_X | TEXTFIELD\_REAL)

```
#secciÃ³n botones
@desc_botones =

```

FXHorizontalFrame.new(@main\_frame,:opts=\>LAYOUT\_FILL|PACK\_UNIFORM\_WIDTH)  
@btnAdd=FXButton.new(@desc\_botones,“Anadir”,:opts=\>LAYOUT\_FILL\_X|BUTTON\_NORMAL)  
@btnDel=FXButton.new(@desc\_botones,“Borrar”,:opts=\>LAYOUT\_FILL\_X|BUTTON\_NORMAL)  
@btnReturn =  
FXButton.new(@desc\_botones,“Volver”,:opts=\>LAYOUT\_FILL\_X|BUTTON\_NORMAL)

```
#seccion lista
@lblDesc=FXLabel.new(@main_frame,"Descuentos

```

actuales:",:opts=\>JUSTIFY\_LEFT)  
@lstDescuentos = FXList.new(@main\_frame,:opts=\>  
LAYOUT\_FILL\_X|FRAME\_SUNKEN )

```
#eventos
@btnReturn.connect(SEL_COMMAND) do
  destroy
end

@btnAdd.connect(SEL_COMMAND) do
  @descuentos [@txtNombreDesc.to_s] = @txtCantidad.to_s.to_f if

```

@txtNombreDesc.to\_s.length \>0 and @txtCantidad.to\_s.to\_f \>0  
self.Carga\_Descuentos  
@txtNombreDesc.text=’’  
@txtCantidad.text=’’  
end  
end

def create  
super  
show(PLACEMENT\_SCREEN)  
end

end

app=FXApp.new  
sc = SueldoCalc.new(app)

app.create  
app.run

* * *

I noticed that when I run this code with rubyw, it does not execute,  
but with ruby.exe, it runs fine. Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![tutiplain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tutiplain](https://www.ruby-forum.com/u/tutiplain)\
**Post date:** [December 9, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/using-ocra-fxruby-and-pdf-writer-problem/226056/2 "2009-12-09T17:45:22Z")

</div>

On Dec 9, 11:35 am, Omar C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all,
> 
> I made a simple salary calculator as practice using FXRuby, OCRA to  
> create an .exe file and pdf-writer to generate a small “report” with the  
> results of the calculation. The program runs fine when run with the  
> ruby interpreter, although I get some warnings:

Warnings are normal,

> […]

When you want to attach an application code, use services like Pastie  
or Gist from GitHub to avoid long emails…

> * * *
> 
> I noticed that when I run this code with rubyw, it does not execute,  
> but with ruby.exe, it runs fine. Am I doing something wrong?

For some reason your or one of the libraries is outputting to the  
standard console.

RubyW is a windowless environment and thus, all puts and things will  
fail because there is no standard console attached to it.

When you run it with ruby.exe, do you get all these warnings? I guess  
not, but just to verify.

---

<div class="post-metadata">

**Author:** ![tutiplain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tutiplain](https://www.ruby-forum.com/u/tutiplain)\
**Post date:** [December 9, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/using-ocra-fxruby-and-pdf-writer-problem/226056/3 "2009-12-09T20:41:15Z")

</div>

> When you want to attach an application code, use services like Pastie  
> or Gist from GitHub to avoid long emails…

Ok, I’ll keep that in mind. I’m still rather new to this forum. Where  
can I find info about these services?

> RubyW is a windowless environment and thus, all puts and things will  
> fail because there is no standard console attached to it.
> 
> When you run it with ruby.exe, do you get all these warnings? I guess  
> not, but just to verify.

Yes, the warnings appear only when using RubyW, or when running the .exe  
made with OCRA with the script saved as .rb (which internally uses  
ruby.exe, it seems). when running with RubyW, or the OCRA-made .exe  
with script saved as .rbw, no warnings appear and the program simply  
terminates.

---

<div class="post-metadata">

**Author:** ![tutiplain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tutiplain](https://www.ruby-forum.com/u/tutiplain)\
**Post date:** [December 10, 2009, 4:17am UTC](https://www.ruby-forum.com/t/using-ocra-fxruby-and-pdf-writer-problem/226056/4 "2009-12-10T04:17:04Z")

</div>

Hi again,

I did some more testing and found the following:

I commented out any lines that had to do with the pdf generation, ran  
the app using RubyW and it worked. I then uncommented the the line:

require ‘pdf/writer’

Now, running with rubyw, nothing happens. It seems that something  
inside pdf/writer does not let rubyw work properly. Have you, or anyone  
else out there, encountered this problem?

---

<div class="post-metadata">

**Author:** ![tutiplain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tutiplain](https://www.ruby-forum.com/u/tutiplain)\
**Post date:** [December 10, 2009, 5:05pm UTC](https://www.ruby-forum.com/t/using-ocra-fxruby-and-pdf-writer-problem/226056/5 "2009-12-10T17:05:59Z")

</div>

On Dec 9, 10:17 pm, Omar C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## inside pdf/writer does not let rubyw work properly. Have you, or anyone else out there, encountered this problem?
> 
> Posted viahttp://www.ruby-forum.com/.

I think it is the warning messages that are causing the issue  
They abort execution when using ruybw because stdout (or it maybe  
stderr) doesn’t exist  
You may be able to redirect stdout and stderr to a file (or /dev/null)  
before requiring the pdf lib so the messages have a valid place to go

cheers  
Chris

---

<div class="post-metadata">

**Author:** ![tutiplain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tutiplain](https://www.ruby-forum.com/u/tutiplain)\
**Post date:** [December 11, 2009, 5:40pm UTC](https://www.ruby-forum.com/t/using-ocra-fxruby-and-pdf-writer-problem/226056/6 "2009-12-11T17:40:48Z")

</div>

On Thu, Dec 10, 2009 at 10:41 PM, Omar C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi, I did as you said, and it worked. I put this line on top:
> 
> $stderr2 = $stderr.clone  
> $stderr.reopen(‘tmp2.dat’)  
> require ‘pdf/writer’

A bit off topic, but is there a reason why you’re using PDF::Writer  
instead of Prawn?

-greg

---

<div class="post-metadata">

**Author:** ![tutiplain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tutiplain](https://www.ruby-forum.com/u/tutiplain)\
**Post date:** [December 11, 2009, 4:41am UTC](https://www.ruby-forum.com/t/using-ocra-fxruby-and-pdf-writer-problem/226056/7 "2009-12-11T04:41:53Z")

</div>

Hi, I did as you said, and it worked. I put this line on top:

$stderr2 = $stderr.clone  
$stderr.reopen(‘tmp2.dat’)  
require ‘pdf/writer’

And then, when the script exits:

```
  @stderr.reopen($stderr2)

```

to restore the original output device. The reason for this is that when  
I run OCRA, it runs the app to check its dependencies, but since there  
is no output, it fails.

Many thanks to all who helped me with this issue.
