[ruport] PNG formatter?

18 views
Skip to first unread message

MarkB

unread,
May 20, 2010, 11:37:19 AM5/20/10
to Ruby Reports
Hello - I am new to Ruport and relatively new to Rails as well. Using
the excellent Ruport book I was able to get up and running with a PDF
chart of my data using the Ruport::Controller and
Ruport::Formatter::PDF classes.

I would like to render my chart as a PNG (JPG would be fine too)
directly in the browser and allow the user to download a PDF of the
same chart. The PDF is working, but I cannot figure out how to create
a PNG formatter. The book's example seems geared towards PDF output
using PDF::Writer.new, pdf.render, etc.

Is there a way to add a PNG formatter? I've looked at the SVG/PNG
example (http://www.rubyreports.org/examples.html) using the
Ruport::Report class, but it lacks the formatting controls of the PDF
example and I don't know how I might extend it back to create a PDF.

I'm happy to go either way (add PNG to the PDF example or vice-versa),
but can't figure out how to do either. I'd really appreciate the
community's advice.

Thanks, in advance, for your help.

Mark

--
You received this message because you are subscribed to the Google Groups "Ruby Reports" group.
To post to this group, send email to ruby-r...@googlegroups.com.
To unsubscribe from this group, send email to ruby-reports...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruby-reports?hl=en.

Andrew France

unread,
May 20, 2010, 9:32:32 PM5/20/10
to ruby-r...@googlegroups.com
Welcome Mark,

On 21/05/10 01:37, MarkB wrote:
> Hello - I am new to Ruport and relatively new to Rails as well. Using
> the excellent Ruport book I was able to get up and running with a PDF
> chart of my data using the Ruport::Controller and
> Ruport::Formatter::PDF classes.
>
> I would like to render my chart as a PNG (JPG would be fine too)
> directly in the browser and allow the user to download a PDF of the
> same chart. The PDF is working, but I cannot figure out how to create
> a PNG formatter. The book's example seems geared towards PDF output
> using PDF::Writer.new, pdf.render, etc.
>

I'm guessing you're doing something like:
http://ruportbook.com/payr_formatting_2.html#adding_a_graph

I'm going to assume that you're using the Gruff graphing library, but
others should be similar.

> Is there a way to add a PNG formatter? I've looked at the SVG/PNG
> example (http://www.rubyreports.org/examples.html) using the
> Ruport::Report class, but it lacks the formatting controls of the PDF
> example and I don't know how I might extend it back to create a PDF.
>

Looking at the Gruff support:
http://github.com/ruport/ruport-util/blob/master/lib/ruport/util/graph/gruff.rb

The draw_graph method does write directly to the PDF, but it's actually
generating a PNG first. I think you should be able to get just a PNG or
JPEG with something like:
my_graph = Graph(table); # As you have already
my_graph.to_png (or my_graph.render(:png), I forget)

If you want the same graph for PNG and PDF formatters you could move the
graph generation code to a helper module inside the Ruport controller,
then it would be accessible to both formatters and you can call the
draw_graph/render method as appropriate.

Hope that makes sense, let us know how it goes.

Regards,
Andrew

MarkB

unread,
May 21, 2010, 1:43:50 AM5/21/10
to Ruby Reports
Thanks for the reply, Andrew.

I made various attempts to directly convert my graph to a PNG before
generating the PDF, to no avail. Your reminder that draw_graph uses
Gruff to create a PNG set me down a path where I ended up using Gruff
natively to create the PNG. So now I generate the data series once,
then call either Gruff or Ruport depending on my desired output
format:

g = Gruff::Line.new
g.data("Cost", axis_y)
g.labels = axis_x
send_data(g.to_blob, :disposition => 'inline', :type => 'image/
png', :filename => "GruffTest.png")

OR (in a separate class)

g = Graph(axis_x)
g.series axis_y, "Cost"
options.graph = g
...
draw_graph(options.graph, :title => "cost chart", :x => 100, :y =>
500, :width => width, :height => height)

I have a few things yet to be worked out, but the basic PNG/PDF
generators are working, so the rest should fall into place.

Regards,
Mark

On May 20, 6:32 pm, Andrew France <andrew-li...@odaeus.co.uk> wrote:
> Welcome Mark,
>
> On 21/05/10 01:37, MarkB wrote:
>
> > Hello - I am new to Ruport and relatively new to Rails as well.  Using
> > the excellent Ruport book I was able to get up and running with a PDF
> > chart of my data using the Ruport::Controller and
> > Ruport::Formatter::PDF classes.
>
> > I would like to render my chart as a PNG (JPG would be fine too)
> > directly in the browser and allow the user to download a PDF of the
> > same chart.  The PDF is working, but I cannot figure out how to create
> > a PNG formatter.  The book's example seems geared towards PDF output
> > using PDF::Writer.new, pdf.render, etc.
>
> I'm guessing you're doing something like:http://ruportbook.com/payr_formatting_2.html#adding_a_graph
>
> I'm going to assume that you're using the Gruff graphing library, but
> others should be similar.
>
> > Is there a way to add a PNG formatter?  I've looked at the SVG/PNG
> > example (http://www.rubyreports.org/examples.html) using the
> > Ruport::Report class, but it lacks the formatting controls of the PDF
> > example and I don't know how I might extend it back to create a PDF.
>
> Looking at the Gruff support:http://github.com/ruport/ruport-util/blob/master/lib/ruport/util/grap...
Reply all
Reply to author
Forward
0 new messages