首页 文章

使用wicked-pdf以PDF格式渲染Dragonfly图像

提问于
浏览
0

我正在尝试使用wkhtmltopdf渲染我的模型的pdf,其中包含使用dragonfly存储的图像,但是我只能找到两个提到它的网站(a german bloga 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
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

任何建议将被认真考虑!

4 回答

  • 1

    诀窍是使用: image_tag(logo.path)

    请注意 image_tagpath 的用法,而不是 wicked_pdf_image_tagurl .

  • 0

    你需要用 wicked_pdf_image_tag 替换 image_tag

    <%= wicked_pdf_image_tag image.url if image.image_stored? %>
    
  • 0

    这对我有用

    <img src="<%=Rails.root.join(image.path)%>" >

    如果图像对象是孩子

    <img src="<%=Rails.root.join(parentObject.image.path)%>" >

  • 1

    我知道,我的解决方案很难看 . 但它的工作原理:)

    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 回调来删除图像 .

相关问题