Drextor
Drextor

Reputation: 433

Rails Gem “Axlsx” / “Rails-Axlsx” - Adding Image

I try to add one image to my workbook/worksheet.

The Code :

    wb.add_worksheet(:name => "Doc1", :page_setup => setup, :print_options => options) do |sheet|

        img = File.expand_path('../logo.jpg', __FILE__)
        sheet.add_image(:image_src => img, :noMove => true) do |image|
            image.width = 7
            image.height = 6
            image.start_at 2, 2
        end

    ...

    end

But if i open the document there is no image on the worksheet, how can i fix this ?

And how is the correct path to the image? For this test i copy the jpg in my view folder, but usually all images in "app/assets/images/logo.jpg". I try it with img = File.expand_path('../assets/images/logo.jpg', __FILE__), but it fails with "No file found!"

Upvotes: 1

Views: 2484

Answers (3)

AiHughes
AiHughes

Reputation: 1

In the lines where you set the image width and height you have them very small and you could be overlooking the image as this number is in pixels. Try setting them to higher number.

the following worked for me:

img = File.expand_path(Rails.root+'app/assets/images/result.png')
        sheet.add_image(:image_src => img, :noMove => true) do |image|
            image.width = 100
            image.height = 150
            image.start_at 2, 2
        end

Upvotes: 0

VijayGupta
VijayGupta

Reputation: 685

Here is another way you can achieve this:

Code:

img = File.open(Dir.glob("#{Rails.root}/app/assets/images/result.png, "r")

Upvotes: 0

devanand
devanand

Reputation: 5290

An example on https://pramodbshinde.wordpress.com/2013/12/29/design-spreadsheets-using-axlsx-in-rails/ show the following line of code

img = File.expand_path(Rails.root+'app/assets/images/result.png')

Upvotes: 4

Related Questions