简体   繁体   中英

rails ckeditor and wicked_pdf

I am uploading inline images inside a wysiwyg editor (ckeditor). It integrates with Paperclip. However, when I'm generating the pdf, images from ckeditor don't show up. What am I doing wrong?

Here's the code:

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)

The images uploaded inside ckeditor are saved into public/ckeditor_assets/pictures

Thanks!

Because those ckeditor images are not part of the asset pipeline, you'll need to reference them by their absolute filesystem path for wkhtmtopdf to pull them into your PDF.

In your views you are likely doing something like this:

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

This should be changed to something like:

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

Because in the first case, those image tags are going to be rendered as relative paths, which wkhtmltopdf cannot find on disk in relation to the HTML tempfiles it creates, so it needs a full path to be specified to find the images to pull into your PDF when it gets created.

@bogdan-popa @unixmonkey @flexo Yes, I know it wasn't an answer, but it seemed the only way to respond in a relevant way to the issue with as little bothering as possible. Anyway I came up with this solution, working as sunshine, ie wicked_pdf is rendering the images from 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"

the only thing I'm still puzzled about is the :styles hash. How to get the thumbs right on S3? I hope this is an helpfull answer ... reg. BS

This code works for me, send you html content as parameter ie body

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM