rails ckeditor和wicked_pdf

时间:2014-10-01 08:02:28

标签: ruby-on-rails ruby-on-rails-4 ckeditor wicked-pdf

我正在wysiwyg编辑器(ckeditor)中上传内嵌图像。它与Paperclip集成。 但是,当我生成pdf时,来自ckeditor的图像不会显示。 我究竟做错了什么?

以下是代码:

attachment_file.rb:

class Ckeditor::AttachmentFile < Ckeditor::Asset
  has_attached_file :data,
                    :url => "/ckeditor_assets/attachments/:id/:filename",
                    :path => ":rails_root/public/ckeditor_assets/attachments/:id/:filename"

  validates_attachment_presence :data
  validates_attachment_size :data, :less_than => 100.megabytes
  do_not_validate_attachment_file_type :data

  def url_thumb
    @url_thumb ||= Ckeditor::Utils.filethumb(filename)
  end
end

controller.rb:

  def download
    html = render_to_string("offer_template_one", :formats => [:html], :layout => "templates.html")
    pdf = WickedPdf.new.pdf_from_string(html)
    send_data(pdf,
    :filename    => "offer.#{@offer.id}.pdf",
    :disposition => 'attachment')
  end

application.rb中:

config.assets.paths << Rails.root.join("app", "assets", "fonts")
config.assets.precompile += %w( templates.css )
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)

ckeditor中上传的图像保存在public / ckeditor_assets / pictures

谢谢!

3 个答案:

答案 0 :(得分:0)

由于这些ckeditor图片不属于资产管道,因此您需要通过wkhtmtopdf的绝对文件系统路径引用它们,以将它们拉入您的PDF格式。

在你的观点中,你可能会做这样的事情:

<%= image_tag 'ckeditor_assets/pictures/foo.jpg' %>

这应该改为:

<%= image_tag Rails.root.join('public','ckeditor_assets','pictures','foo.jpg').to_s %>

因为在第一种情况下,那些图像标签将被渲染为相对路径,wkhtmltopdf无法在磁盘上找到与其创建的HTML临时文件相关的路径,因此需要指定一个完整路径来查找图像到在创建PDF时将其拉入其中。

答案 1 :(得分:0)

@ bogdan-popa @unixmonkey @flexo是的,我知道这不是一个答案,但它似乎是以尽可能少的麻烦以相关方式回应问题的唯一方法。无论如何,我提出了这个解决方案,作为阳光工作,即wicked_pdf正在渲染来自http://:image-bucket.s3.amazonaws.com/的图像......

has_attached_file :data,
      :storage => :s3,
      :bucket => "image-bucket",
      :path => "ckeditor/pictures/:id/:basename.:extension",
      :styles => { },
      :url => ':s3_alias_url',
      :s3_host_alias => "image-bucket.s3.amazonaws.com"

我唯一感到困惑的是:样式哈希。如何在S3上竖起大拇指?我希望这是一个有用的答案... reg。 BS

答案 2 :(得分:0)

此代码适用于我,发送html内容作为参数即body

def absolute_path_for_src(body)
  body.gsub(/(src|href)=('|")\//) { |s| "#{$1}=#{$2}#{request.protocol}#{request.host_with_port}/" }
end