【问题标题】:PDFkit *.css stylesheets not being applied未应用 PDFkit *.css 样式表
【发布时间】:2015-06-08 18:51:52
【问题描述】:

我正在尝试使用 pdfkit 将显示图像的 html 页面从 facebook cdn 转换为 pdf。我正在使用 rails 4.2、pdfkit 0.6.2 和 wkhtmltopdf-binary 0.9.9.3。

# Gemfile
gem 'pdfkit'
gem 'wkhtmltopdf-binary'

# controller
def generate_pdf
  @booklet = Booklet.find params[:id]
  @cover = Image.last
  @images = @booklet.images.sort_by(&:uploaded_at)
  respond_to do |format|
    format.html
    format.pdf do
      html = render_to_string(layout: true , action: "generate_pdf.html.haml")
      kit = PDFKit.new(html, page_size: 'A4', orientation: 'Landscape')
      `sass vendor/assets/stylesheets/bootstrap.scss tmp/bootstrap.css`
      `sass vendor/assets/stylesheets/custom.scss tmp/custom.css`
      kit.stylesheets << "#{Rails.root}/tmp/bootstrap.css"
      kit.stylesheets << "#{Rails.root}/tmp/custom.css"
      pdf = kit.to_pdf
      send_data pdf, filename: 'booklet.pdf', type: 'application/pdf'
    end
  end
end

# application.scss
@import 'bootstrap';                                                                                                                                           
@import 'custom';

# config/application.rb
require 'pdfkit'
config.middleware.use PDFKit::Middleware

# config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf unless Mime::Type.lookup_by_extension(:pdf)

正在生成 pdf 并显示 facebook cdn 图像,但未应用样式表。我错过了什么?

我在这里为上述问题创建了一个示例存储库:https://github.com/prasadsurase/topdf

仅供参考,bootstrap.css 和 custom.css 位于 vendor/assets 中,并且扩展名已重命名为 scss。在 sass 中,资产(字体和图像)已使用 'font-path' 和 'image-url' rails helper 进行引用。资产已预编译,应用程序-..css 已复制到 pdf.css,资产从根路径 (/) 引用

【问题讨论】:

  • 由于资产管道,这似乎是 Rails 3.1 和 PDF 工具包的一个运行问题。我在这里发现了一个类似的问题,希望能有所帮助。 stackoverflow.com/questions/8044659/pdfkit-does-not-style-pdfs
  • @cmw2379 我尝试了解决方案,但没有奏效。你可以克隆回购并试一试吗?
  • 今晚如果有时间,我会试一试。
  • 切换到 wicked_pdf gem。包含的资产助手工作得很好,并且 gem 实际上组织和​​记录得更好。
  • 我很着急,所以在发布这个问题几天后搬到了 wicked_pdf。有效。谢谢。

标签: ruby-on-rails-4 pdfkit


【解决方案1】:

Bootstrap 是否适用于您应用程序中的其他页面? -- 目的是确保您正确配置元标记以包含 Bootstrap 文件,并且您指向 Bootstrap 文件(似乎在 /tmp 中?)

另外,你在什么操作系统上编码?我可以建议将"#{Rails.root}/tmp/bootstrap.css" 替换为Rails.root.join('tmp','bootstrap.css')

让我知道这些是否有帮助 - 或没有。

【讨论】:

    猜你喜欢
    • 2016-02-28
    • 2021-09-13
    • 2012-11-12
    • 2016-10-13
    • 2016-12-07
    • 2021-09-09
    • 2019-12-05
    • 2022-10-01
    • 1970-01-01
    相关资源
    最近更新 更多