【问题标题】:how to load Css in pdf using wicked_pdf如何使用 wicked_pdf 在 pdf 中加载 Css
【发布时间】:2015-02-06 16:25:43
【问题描述】:

对于我的 rails 4 应用程序,我想将 html 和 Css 转换为 pdf 文件。 我使用 wkhtmltopdf 和 wicked_pdf gem 如果我使用 wicked helper,它不会显示 css,但只会呈现文本

在我的控制器中我有:

respond_to do |format|
      format.html
      format.pdf do
        render :pdf => "#{@card.name}",
            :template => 'cards/show.html.slim',
            :page_size => "A4",
            :disposition => 'attachment'

在show.html.slim中

    = link_to "Download PDF", :action => "show", :format => :pdf

应用程序.html.erb

<%= wicked_pdf_stylesheet_link_tag "pdf" -%>
<%= wicked_pdf_stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
<%= wicked_pdf_javascript_include_tag "application", "data-turbolinks-track" => true %>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

【问题讨论】:

  • 我以前从未见过-%&gt;。是这样写的,还是打错字了?
  • 我知道,我都试过了,但是 -%> 是文档所说的......github.com/mileszs/wicked_pdf
  • 显然是打错了...至于其余的,你不要写"pdf",我认为:你应该用它们来替换助手javascript_include_tagstylesheet_link_tag(我认为,再次)
  • 编辑代码^* application.html.erb

标签: ruby-on-rails pdf wkhtmltopdf wicked-pdf


【解决方案1】:
<%= wicked_pdf_stylesheet_link_tag "css_file_name" -%>

这是您的 app/assets/stylesheets/ 目录中必须有的 css 文件名

<%= wicked_pdf_stylesheet_link_tag "css_file_name" -%>
<%= wicked_pdf_stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= wicked_pdf_javascript_include_tag "application", "data-turbolinks-track" => true %>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

希望这对你有帮助。

【讨论】:

  • 我的 application.SomeNum.css 在公用文件夹中创建。在这种情况下,我该如何处理?文件名也是一个随机数。
  • 在我的示例中,css_file_namescss,我使用 @import "bootstrap.min" 导入 vendor 中的引导文件文件夹
猜你喜欢
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多