[fxruby-users] Graphics printing with FXRuby?

David Peoples davidp at touringcyclist.com
Tue Feb 22 16:59:49 EST 2005


David Peoples wrote:
> What is the current recommendation for sending output to printers from 
> FXRuby-based applications? --snip--

I didn't hear back from anyone, so I'm posting the solution I figured 
out in case anyone else needs to know later.

The basic idea is this:

1) Select the printer and configure it using FXPrintDialog. (This is the 
only part that requires FXRuby. The rest is straight Ruby code. Printer 
selection here is optional -- Ghostscript can be configured to do it in 
step 3 if desired.)
2) Generate the output as a PDF file using the ruby_fpdf library. 
(Ghostscript requires a real seekable file, so this can't be just a 
stream sent through a pipe.)
3) Use Ghostscript to process the PDF file and send it to a printer. Run 
Ghostscript using Kernel.system.
4) Delete the generated temporary PDF file.

This was tested using Ruby 1.8.2, FXRuby 1.2.3, Ghostscript 8.50 and 
Ruby_fpdf 1.53a. Tested on Windows only, other OSs will require a 
different Ghostscript command line at least.

This solution produces printed output only. There isn't an embedded PDF 
viewer for FXRuby that I know of for print previews, although 
Ghostscript or some other external viewer could be launched to do the 
print preview.

Construction of real PDF reports using ruby_fpdf is an exercise left to 
the reader. <g>

#--- Code start ---#

require 'fox12'
require 'fpdf'
include Fox

class PrintTester < FXMainWindow

   def initialize(app)
     super(app, "Dialog Test", nil, nil, DECOR_ALL, 0, 0, 200, 100)
     contents = FXHorizontalFrame.new(self, 
LAYOUT_SIDE_TOP|FRAME_NONE|LAYOUT_FILL_X|LAYOUT_FILL_Y|PACK_UNIFORM_WIDTH)
     modalButton = FXButton.new(contents,
       "&Printer Dialog", nil, nil, 0,
       FRAME_RAISED|FRAME_THICK|LAYOUT_CENTER_X|LAYOUT_CENTER_Y)
     modalButton.connect(SEL_COMMAND, method(:onCmdShowDialogModal))
   end

   def onCmdShowDialogModal(sender, sel, ptr)
     myPrinterDialog = FXPrintDialog.new(self, "Select printer")
     if myPrinterDialog.execute == 1
       pdf=FPDF.new
       pdf.AddPage
       pdf.SetFont('Arial','B',16)
       pdf.Cell(40,10,'Hello World!')
       pdf.Output('temp.pdf')
       # Lots of options available on the ghostscript command
       # line. Review the ghostscript documentation.
       systemCommand = 'gswin32c -dBATCH -dNOPAUSE -dQUIET ' +
         '-dNoCancel -sDEVICE=mswinpr2 -sOutputFile="%printer%' +
         myPrinterDialog.printer.name + '" temp.pdf'
       system(systemCommand)
       File.delete('temp.pdf')
     end
     return 1
   end

   def create
     super
     show(PLACEMENT_SCREEN)
   end
end

def run
   application = FXApp.new("Dialog", "FoxTest")
   PrintTester.new(application)
   application.create
   application.run
end

run

#--- Code end ---#


-- 
David Peoples  davidp at touringcyclist.com
The Touring Cyclist  http://www.touringcyclist.com
11816 St. Charles Rock Road, Bridgeton, MO 63044
tel: 314-739-4648  fax: 314-739-4972


More information about the fxruby-users mailing list