# Pdf/writer: table.render\_on best practices?

**URL:** <https://www.ruby-forum.com/t/pdf-writer-table-render-on-best-practices/59710>\
**Category:** Rails\
**Created:** [May 5, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/pdf-writer-table-render-on-best-practices/59710 "2006-05-05T18:02:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [May 5, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/pdf-writer-table-render-on-best-practices/59710/1 "2006-05-05T18:02:27Z")

</div>

Hi All,

I generate a pdf report that contains a table. The report is 14 pages,  
and  
it takes 20s to be rendered. Most of these 20s are spend in the  
render\_on  
method. I wonder if it is possible to speed up that process?

To create the report, I do the following:

1. Setup pdf layout:

# Setup pdf layout

```
  pdf.select_font("Helvetica", { :encoding => "WinAnsiEncoding"})
  pdf.text " ", :font_size => 12, :justification => :center
  s = 9
  t = 'Seite <PAGENUM>'
  y = pdf.absolute_bottom_margin - 5
  x = pdf.absolute_right_margin
  x -=(pdf.text_width('Seite XX', s))
  pdf.start_page_numbering(x, y, s, nil, t, nil)
  pdf.open_object do |heading|
     pdf.save_state
     pdf.stroke_color! Color::Black
     pdf.stroke_style! PDF::Writer::StrokeStyle::DEFAULT
     s = 9
     t = "My title - " + Date.today().to_s
     w = pdf.text_width(t, s) / 2.0
     x = pdf.margin_x_middle
     y = pdf.absolute_top_margin
     pdf.add_text(x - w, y, t, s)
     x = pdf.absolute_left_margin
     w = pdf.absolute_right_margin
     y -= (pdf.font_height(s) * 1.01)
     pdf.line(x, y, w, y).stroke
     y = pdf.absolute_bottom_margin + pdf.font_height(9) - 2
     pdf.line(x, y, w, y).stroke
     pdf.restore_state
     pdf.close_object
     pdf.add_object(heading, :all_pages)
  end

```

1. create a table:  
table = PDF::SimpleTable.new

2. populate the table  
table.data = dataArray

3. set which rows are displayed, and in which order:  
table.column\_order = rows  
(Note: only some rows are displayed)

4. I set the width:  
table.width = pdf.absolute\_right\_margin - pdf.absolute\_left\_margin

5. I call table.render\_on(pdf):  
table.render\_on(pdf)

6. I send the file with send\_file

Steps 1,2,3,4,5 and 7 takes less than 2 sec altogether. Step 6 takes  
about  
17s. I must say that the margins are not correct, and that the table  
goes  
over the header and footer of my document (does it matter for rendering  
time?)

Any clue of what I could do to speed this up? or maybe this is normal  
speed?

Regards,

Nicolas

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [May 5, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/pdf-writer-table-render-on-best-practices/59710/2 "2006-05-05T18:24:19Z")

</div>

On 5/5/06, Nicolas B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I generate a pdf report that contains a table. The report is 14 pages, and  
> it takes 20s to be rendered. Most of these 20s are spend in the render\_on  
> method. I wonder if it is possible to speed up that process?

SimpleTable#render\_on is slow. There may be ways to speed it up, but I  
have not had time to investigate them yet.

-austin

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [May 5, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/pdf-writer-table-render-on-best-practices/59710/3 "2006-05-05T18:42:43Z")

</div>

OK, then I’ll just live with it - and maybe show a blinking “please  
wait” to  
the user. There is no way to avoid a “render\_on”, right?

Thanks,

Nicolas

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [May 5, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/pdf-writer-table-render-on-best-practices/59710/4 "2006-05-05T18:49:00Z")

</div>

On 5/5/06, Nicolas B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> OK, then I’ll just live with it - and maybe show a blinking “please wait” to  
> the user. There is no way to avoid a “render\_on”, right?

Not if you want that table. 😉

-austin

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [May 5, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/pdf-writer-table-render-on-best-practices/59710/5 "2006-05-05T19:04:28Z")

</div>

On 5/5/06, Jodi S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey Austin,
> 
> I’m also generating a big table - probably 100 pages or more  
> (inventory report) - the client currently sets it up to print overnight.
> 
> While this is ‘ok’, I’d prefer to speed it up for them. I’d be  
> willing to take a look at the table rendering code - Do you have any  
> hints on where I’d focus my attention?

Transaction::Simple. Or at least the use thereof.

Transaction::Simple does its job well, but I’m not currently doing a  
good job of clearing unused data and keeping too much rewind  
information, I think. Reducing that should bring a performance boost.  
Figuring out a way to precalculate the size of the data so that rewind  
doesn’t have to happen would be good, too. It would be cheaper to  
simulate to set up pagination points than to recover if we do need to  
paginate.

-austin

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [May 5, 2006, 11:13pm UTC](https://www.ruby-forum.com/t/pdf-writer-table-render-on-best-practices/59710/6 "2006-05-05T23:13:59Z")

</div>

On 5-May-06, at 1:03 PM, Austin Z. wrote:

> -austin

Ok. after much rumination and consideration about writing a  
preprocessor it became clearer that the transactions were the  
(slowdown) culprit(duh), and if I’m dealing with a huge number of  
rows then split\_rows should NEVER be set to true, as the (rewind)  
rollback condition would never occur, and thus the transaction space  
would fill up - 1 transaction per row.

…and \me had split\_rows set to false (default value).

grin.

after using the correct setting I’m looking at a 1000% speedup.

So the only code improvement I might suggest Austin would be to set  
split\_rows to true if render\_on detects more rows than can ever be  
fit on one page - or perhaps a private that does the same thing  
without changing the programmers interface.

Nicolas you might check to see if you’ve set split\_rows correctly -  
you’ll get the same pdf output, but transactions won’t be used to  
hold row state.

cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [May 6, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/pdf-writer-table-render-on-best-practices/59710/7 "2006-05-06T16:39:31Z")

</div>

Thanks Jodi, great advice!

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [May 5, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/pdf-writer-table-render-on-best-practices/59710/8 "2006-05-05T19:01:20Z")

</div>

Hey Austin,

I’m also generating a big table - probably 100 pages or more  
(inventory report) - the client currently sets it up to print overnight.

While this is ‘ok’, I’d prefer to speed it up for them. I’d be  
willing to take a look at the table rendering code - Do you have any  
hints on where I’d focus my attention?

cheers,  
Jodi
