0

I'm trying to generate a PDF with Wicked PDF. So far it's going fine, but when I try to add images all I get are blank boxes. My images are in both ../app/assets/images/foo.png and ../public/images/foo.png for testing purposes.

From what I understand, wicked_pdf_image_tag('foo.png') will output <img src="file:///home/users/my_username/my_app_name/public/images/foo.png"> so being that I have my images in /public/images the following code should work:

<%= wicked_pdf_image_tag("foo.png", width: 100, height: 100) %>    

...but it just doesn't, it only gives me a blank box. I've also all of the following tried:

    <%= image_tag Rails.root.join("app/assets/images/foo.png"), width: 100, height: 100 %>
    <img src="<%= Rails.root.join("app/assets/images/foo.png")%>" width="100" height="100">
    <%= image_tag Rails.root.join("public/images/foo.png"), width: 100, height: 100 %>
    <img src="<%= Rails.root.join("public/images/foo.png")%>" width="100" height="100">
    <%= wicked_pdf_image_tag("foo.png", width: 100, height: 100) %>
    <img src="<%= "images/foo.png"%>" width="100" height="100">

All what's generated is this

image_tag returnin

Any help is appreciated. Thanks in advance

UPDATE:

I think it's important to mention that the Rails application is API-only. That being said, I added the following code on my .erb file:

<%= image_tag image_url("foo.png"), width: 100, height: 100 %>

This alone doesn't work, but it does if I change the application_controller.rb class from:

class ApplicationController < ActionController::API

to:

class ApplicationController < ActionController::Base

That's the ONLY way I have discovered to make an image appear yet. If someone can give insight on this, I would appreciate it.

3 Answers3

0

I finally got the thing to work. There's no need to change the application_controller.rb class. All I had to do is to specify the host of the image and with that construct the rest of the URL like this:

wicked_pdf_image_tag("localhost:3000#{image_url("foo.png")}")

Of course, the host will change once the application gets on productive. So for that, I created a helper that manages the image URL for me:

def manage_image_url
    return case Rails.env
    when 'development' then 'http://localhost:3000'
    when 'develop' then 'https://yourpagehere.com'
    else 'https://yourotherpagehere.com'
    end
end

In the controller that generates the PDF, I pass the result of that method as variable, and the final code should look like this:

wicked_pdf_image_tag("#{manage_image_url}#{image_url("foo.png")}")

The image_tag method also works. Note that if your Rails app is not API-only you probably won't need to do this. I'm still not sure of what causes the images to not show if you try to load them from the assets or public folders, but my guess is that it has to do with how API-only apps work. Have a nice day!

Meerten
  • 27
  • 5
0

time without knowing about you ... I hope you are ok ... try adding this in your application controller:

class ApplicationController <ActionController::API
   include ActionController::Helpers
   #....
end

and after that

 <%= image_tag("foo.png", size: "100")  %>
0

I've also had this problem in the past, and if I remember well my issue was:

either an issue with the way you indent a wicked_pdf_image_tag, you should not use any parenthesis like so:

<%= wicked_pdf_image_tag 'foo.png', class: "my-class", size: "50x50" %>

either with the version of the wicked_pdf binary you are using

askprod
  • 99
  • 8
  • wicked_pdf_image_tag supports both syntaxes with and without parenthesis. I didn't try to change the wicked_pdf binary though, that might be something to test next time. Ty for the help! – Orion Melgarejo Jul 06 '20 at 02:30