【问题标题】:rails ckeditor and wicked_pdfrails ckeditor 和 wicked_pdf
【发布时间】:2014-10-01 08:02:28
【问题描述】:

我在所见即所得的编辑器 (ckeditor) 中上传内联图像。它与回形针集成。 但是,当我生成 pdf 时,来自 ckeditor 的图像不会出现。 我究竟做错了什么?

代码如下:

附件文件.r​​b:

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

应用程序.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中

谢谢!

【问题讨论】:

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


    【解决方案1】:

    因为这些 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 时将其拉入您的 PDF。

    【讨论】:

      【解决方案2】:

      @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 上获得正确的拇指?我希望这是一个有用的答案... reg。 BS

      【讨论】:

        【解决方案3】:

        此代码适用于我,将 html 内容作为参数发送给您,即正文

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

        【讨论】:

          猜你喜欢
          • 2011-07-22
          • 1970-01-01
          • 2020-04-04
          • 2012-02-21
          • 2011-12-16
          • 2017-05-01
          • 2014-07-13
          • 2016-07-02
          • 1970-01-01
          相关资源
          最近更新 更多