Prawn does not find an image in assets

849 views
Skip to first unread message

OfficeYA Héctor Jiménez

unread,
Jul 16, 2015, 8:48:11 AM7/16/15
to prawn...@googlegroups.com
Hi, I have been figthing unsuccessfuly with image for several days.

I am creating a report with Prawn, so in the header goes the logo of the company and its name.

The logo is stored inside: 
app/assets/images/logos/logo.png

This is my code for Prawn:

in the controller:

def listing
@phones = Phone.all.order :phone
respond_to do |format|
format.pdf do
pdf = PhonePDF.new( view_context )
send_data pdf.render,
type: 'application/pdf',
disposition: 'inline'
end
end
end

Now I created:
app/pdf/phone.pdf.rb

This is the code for phone.pdf.rb:

require "open-uri"
class PhonePDF < Prawn::Document
def initialize( view )
@view = view

super size: :letter, margin: 56, layout: :portrait

# creating the page heading with: logo and brand name
table [ [ { image: open('/assets/logos/logo_officeya_67x100.png') }, 'officeya' ] ]

  end
end

About the line in red:

I have try it like: 
'assets/logos/logo_officeya_67x100.png'
'http://localhost:3000/assets/logos/logo_officeya_67x100.png'
'http://127.0.0.1:3000/assets/logos/logo_officeya_67x100.png'

And I get the same error:

it does not find the logo file, but, if I paste the last 2 lines in the browser, I get the logo. 
Also I used 'http://lorempixel.com/30/30/' and, saddly, IT DID WORK!

Any idea what could be wrong. I do no want to use path relative to my file system.

Please help me before I ended pulling off all my hear.






Brad Ediger

unread,
Jul 16, 2015, 10:08:32 AM7/16/15
to prawn-ruby
On Thu, Jul 16, 2015 at 3:04 AM, OfficeYA Héctor Jiménez
<dynamis...@gmail.com> wrote:
> The logo is stored inside:
> app/assets/images/logos/logo.png

Prawn by itself doesn't have any integration with the Rails asset
pipeline. You're confusing methods that accept filesystem paths URLs
from your application. open-uri is a good guess, but when you pass it
an absolute path ("/assets/..."), you haven't given it a host to
connect to.

What I would do is open that path directly on the filesystem:

image: Rails.root.join("app/assets/images/logos/logo.png")

If you really want to pull it through the asset pipeline (which will
be slower), you can open it directly by URL:

image: open('https://my-application.com/assets/logos/logo.png')

-be

OfficeYA Héctor Jiménez

unread,
Jul 16, 2015, 11:44:05 AM7/16/15
to prawn...@googlegroups.com
I already have done that, but it does not work, at least, not locally. 

If I try:
table [ [ { image: open("http://127.0.0.1:3000/assets/logos/logo_officeya_67x100.png") }, 'officeya' ] ]
does not work, but if I try:
table [ [ { image: open("http://lorempixel.com/30/30") }, 'officeya' ] ]
IT DOES WORK

Now if I paste the http://127.0.0.1:3000/assets/logos/logo_officeya_67x100.png part in my browser, I get the logo on screen.

Brad Ediger

unread,
Jul 16, 2015, 11:46:41 AM7/16/15
to prawn-ruby
On Thu, Jul 16, 2015 at 10:44 AM, OfficeYA Héctor Jiménez
<dynamis...@gmail.com> wrote:
> I already have done that, but it does not work, at least, not locally.
>
> If I try:
>
> table [ [ { image:
> open("http://127.0.0.1:3000/assets/logos/logo_officeya_67x100.png") },
> 'officeya' ] ]
>
> does not work, but if I try:
>
> table [ [ { image: open("http://lorempixel.com/30/30") }, 'officeya' ] ]
>
> IT DOES WORK

If you know the path to the file on disk (which it appears you do, as
it's part of your application), I would highly recommend just using
that instead of round-tripping it through a web server.

-be

OfficeYA Héctor Jiménez

unread,
Jul 16, 2015, 12:11:34 PM7/16/15
to prawn...@googlegroups.com
I have found a way to make it work, I do not like it because it uses a path to my local file system.

This is my 'almost' solution:

Rails.root.to_s + '/app/assets/images/logos/logo_officeya_67x100.png'

El jueves, 16 de julio de 2015, 7:48:11 (UTC-5), OfficeYA Héctor Jiménez escribió:

Brad Ediger

unread,
Jul 16, 2015, 12:15:27 PM7/16/15
to prawn-ruby
On Thu, Jul 16, 2015 at 11:11 AM, OfficeYA Héctor Jiménez
<dynamis...@gmail.com> wrote:
> I have found a way to make it work, I do not like it because it uses a path
> to my local file system.
>
> This is my 'almost' solution:
>
> Rails.root.to_s + '/app/assets/images/logos/logo_officeya_67x100.png'

Why don't you like that solution? Your application needs to know where
that file is on disk anyway, even if you're pulling it through the web
server.

Note: Since Rails.root is a Pathname, the preferred way is actually to
use Pathname#join:

Rails.root.join('app/assets/images/logos/logo_officeya_67x100.png')

-be
Reply all
Reply to author
Forward
0 new messages