我试图使用wkhtmltopdf渲染我的模型的pdf文件,其中包含存储有dragonfly的图像,但我无法使其正常工作。当我使用wkhtmltopdf的debug选项时,html页面呈现得很好,但pdf本身只是得到了一个空的框,图像应该放在那里。到目前为止,我只找到了两个站点(a german blog和a google forum thread),但这两个解决方案都不能解决我的问题。我怀疑我应该使用blog中建议的图像文件的绝对路径,但当我尝试这种解决方案时,服务器会陷入:
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\"]***************"我的代码如下所示:
image.rb
class Image < ActiveRecord::Base
dragonfly_accessor :image
endimages_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
endshow.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
任何建议都将不胜感激!
发布于 2016-10-17 05:30:20
我知道,我的解决方案很难看。但它是有效的:)
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现在我像这样渲染图像:
<img src="<%=Rails.root.join("tmp","image", image.id.to_s)%>" alt=<%= image.name %> >现在,您甚至可以附加一个after_destroy回调来删除图像。
发布于 2017-01-27 16:24:58
诀窍是使用:image_tag(logo.path)
注意image_tag和path而不是wicked_pdf_image_tag和url的用法。
发布于 2016-10-07 19:10:58
您需要用wicked_pdf_image_tag替换image_tag
<%= wicked_pdf_image_tag image.url if image.image_stored? %>https://stackoverflow.com/questions/39914391
复制相似问题