Reputation: 315
I am trying to render a pdf of my model containing images stored with dragonfly using wkhtmltopdf but I can't get it to work. The html page renders fine when I use the debug option for wkhtmltopdf but the pdf itself just gets an empty box where the image should go. So far I've only found two sites where this is brought up (a german blog and a google forum thread), but neither solution solved it for me. My suspicion is that I should use absolute paths to the image files, which is suggested in the blog, but when I try that solution the server gets stuck at:
Rendered images/show.pdf.erb (19.9ms)
"***************[\"/home/oskar/.rbenv/versions/2.2.3/bin/wkhtmltopdf\",
\"-q\", \"--encoding\", \"utf8\", \"--orientation\", \"Landscape\",
\"file:////tmp/wicked_pdf20161007-8095-1nlruhe.html\", \"/tmp
/wicked_pdf_generated_file20161007-8095-1f6vr27.pdf\"]***************"
My code looks like this:
image.rb
class Image < ActiveRecord::Base
dragonfly_accessor :image
end
images_controller.rb:
def show
respond_to do |format|
format.html
format.pdf do
render pdf: 'name',
background: true,
encoding: 'utf8',
orientation: 'Landscape',
:show_as_html => params[:debug].present?
end
end
end
show.pdf.erb:
<% if params[:debug].present? %>
<%= stylesheet_link_tag 'document' %>
<%= stylesheet_link_tag 'pdf' %>
<% else %>
<%= wicked_pdf_stylesheet_link_tag 'document' %>
<%= wicked_pdf_stylesheet_link_tag 'pdf' %>
<% end %>
...
<%= image_tag image.url if image.image_stored? %>
...
image.url = http://localhost:3000/media/W1siZiIsIjYyIl1d/imagename.png
Any advice will be appreciated!
Upvotes: 0
Views: 504
Reputation: 498
I had exactly the same problem and spent untold hours trying to figure it out. As with everything Rails, once you know the answer it is really easy.... All I had to do was add the dragonfly directory to the Rails asset path in the appropriate environment file.
config.assets.paths << "#{Rails.root}/public/system/dragonfly"
I was then able to render the image using the wicked_pdf_asset_base64 helper method.
= image_tag wicked_pdf_asset_base64(@product.main_image.path), width: 280, height: 200
Upvotes: 0
Reputation: 1
this worked for me use path
<img src="<%=Rails.root.join(image.path)%>" >
in case the image object is a child
<img src="<%=Rails.root.join(parentObject.image.path)%>" >
Upvotes: 0
Reputation: 378
The trick is to use: image_tag(logo.path)
Note the usage of image_tag
and path
instead of wicked_pdf_image_tag
and url
.
Upvotes: 1
Reputation: 1378
I know, my solution is ugly. But it works :)
def self.export_to(path)
all.each do |image_model|
image_model.image.to_file(Rails.root.join("tmp","image", image.id.to_s))
end
end
And now I render images like this:
<img src="<%=Rails.root.join("tmp","image", image.id.to_s)%>" alt=<%= image.name %> >
Now you even could attach an after_destroy
callback to remove the images.
Upvotes: 1
Reputation: 23691
You need to replace image_tag
with wicked_pdf_image_tag
<%= wicked_pdf_image_tag image.url if image.image_stored? %>
Upvotes: 0