Search code examples
ruby-on-railsruby-on-rails-5erbwicked-pdf

WickedPDF template image_tag / wicked_pdf_image_tag not working on Rails API-only


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.


Solution

  • 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!