Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom to top text in column of Prawn table

I was trying to create a pdf file using prawn in Rails 4. I needed to have vertical text as header row for a table with large width. Because if I use horizontal row heading, the table won't fit (The contents of the tables are text with 1 or 2 characters).

I tried using rotate option for prawn-table. But that only works when I set the width enough to hold the text while it is still in horizontal direction. But then it is a waste of space when I want it in vertical. But If I reduce the width of the column header the texts break.

It seems to be a bug reported here https://github.com/prawnpdf/prawn/issues/409. And it seems this pull request solves the issue https://github.com/prawnpdf/prawn-table/pull/32.

But since I'm using prawn-rails, it is difficult to me to change the gem to use that pull request, which has not yet merged to the master.

How should I proceed to solve this problem?

like image 778
Anwar Avatar asked Nov 15 '25 22:11

Anwar


1 Answers

I finally solved the issue using the PR of straydogstudio in my rails project. Here is the step by step solution of it.

First edit the Gemfile to include the prawn-table gem from straydogstudio's repo.

gem 'prawn-table', :git => 'https://github.com/straydogstudio/prawn-table.git', ref: '759a27b6'

I used ref: option for minimizing the download required for the whole repository.

Next, I used :rotate option without :valign. This is important, because when :valign is used, the text somehow get messed.

The Options I used inside the prawn table initialization block is

    cells.font_size = 6
    cells.padding = [1, 1]

    row(0).rotate = 90
    row(0).columns(3..52).rotate = 0 # every splitted column should be counted
    row(0).height = 40
    row(0).width = 15
    row(0).align = :center
    row(1).columns(0..-1).rotate = 90 # rotate the second row too!
    row(1).height = 30

The result is satisfactory. Here is an screenshot of the complex setup.

table with rotate fix

Hope this will help future visitors.

like image 91
Anwar Avatar answered Nov 17 '25 17:11

Anwar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!