Fernando Osorto
Fernando Osorto

Reputation: 73

wicked_pdf_image_tag resolves to public folder when looking for images

I want to render an image that a user uploads in a PDF. I am saving the image to the current Rails directory public folder.

Images are being saved at the moment inside the public folder. Everything works in development but when in production, it is thinking that the image is part of the assets and raises the "image is not in the assets" error. In development, it is looking at the correct path inside the public folder. In production with the code below, it looks at public/public for the image.

This is the code that I have in my view that renders the PDF.


    <% @images.each do |image| %>
      <% if Rails.env != "development" %>
        <%= wicked_pdf_image_tag ("public/#{image.original_filename}")%>
      <% else %>
        <%= wicked_pdf_image_tag(image.original_filename)%>
      <% end %>

    <% end %>

I tried using

wicked_pdf_image_tag("public/#{image.original_filename}")

but it returns an assets error since its looking inside public/assets folder.

I also tried using

wicked_pdf_image_tag("/public#{image.original_filename}")

but it results in public/public folder, which the image is not inside there but instead one level up.

When I try to retrieve the image I expect for the wicked_pdf_image_tag to look at the public/ folder for the image.

Upvotes: 4

Views: 2883

Answers (1)

Vasfed
Vasfed

Reputation: 18464

wicked_pdf_image_tag is a shorthand for image_tag with asset path resolved with wicked pdf rules (see wicked_pdf/wicked_pdf_helper/assets.rb):

def wicked_pdf_image_tag(img, options = {})
  image_tag wicked_pdf_asset_path(img), options
end

def wicked_pdf_asset_path(asset)
  if (pathname = asset_pathname(asset).to_s) =~ URI_REGEXP
    pathname
  else
    "file:///#{pathname}"
  end
end

So when you need some variations, nothing prohibits you from using image_tag directly with correct image url/path. The catch is that wicked_pdf need absolute file path at local filesystem to render (and plain image url for html preview):

image_tag "file://#{File.expand_path("public/your_image_path_goes_here.jpg")}"

Also nore that original_filename is not the name of that file in your storage, it's the name of the file at client machine during upload. You need to use storage path, this depends on your upload library, for example for Paperclip this would be like yourmodel.image.path

Upvotes: 4

Related Questions