繁体   English   中英

rails ckeditor和wicked_pdf

[英]rails ckeditor and wicked_pdf

我在所见即所得的编辑器(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中

谢谢!

由于这些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创建时。

@ 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"

我唯一仍然困惑的是:styles哈希。 如何正确理解S3? 我希望这是一个有帮助的答案。 BS

此代码对我有用,向您发送html内容作为参数,即正文

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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