【问题标题】:Can not include style sheet file of wicked_pdf using Rails 3不能使用 Rails 3 包含 wicked_pdf 的样式表文件
【发布时间】:2015-05-28 05:50:32
【问题描述】:

我想在 Rails 3 中使用 wicked_pdf 在生成的 PDF 文件中包含样式表。我在test.css 文件中声明了一些样式标签,如下所示。

样式表/test.css:

h1{
    width: 100px;
    height: 100px;
    background-color: red;
}

我还在application.html.erb 文件中包含了wicked_pdf 样式表路径。

<!DOCTYPE html>
<html>
<head>
  <title>Generate4</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= wicked_pdf_stylesheet_link_tag "test" -%>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

但在输出的 PDF 文件中没有显示样式。请检查我的其他文件。

用户/test.html.erb:

<h1>Hello rails</h1>

users_controller.rb:

class UsersController < ApplicationController
    def index

    end
    def download_pdf
         #render pdf: 'test',
           #layout: '/layouts/test',
           #template: '/users/test',
           #handlers: [:erb],
           #formats: [:pdf],
           #:save_to_file => Rails.root.join('public', "test.pdf")
           pdf = render_to_string(pdf: "test.pdf", template: "users/test.html.erb", encoding: "UTF-8")

           send_data pdf ,:disposition => 'inline', filename: 'something.pdf', :type => 'application/pdf'
           save_path = Rails.root.join('pdfs','filename.pdf')
            File.open(save_path, 'wb') do |file|
              file << pdf
            end

    end
end

用户/index.html.erb:

<p>
    <%= link_to "Download Pdf",download_pdf_path(:format => "pdf"),:target => "_blank" %>
</p>

请帮我解决这个问题,并在样式表中显示带有正确声明标签的输出。

【问题讨论】:

  • 您可以为pdf指定布局
  • @ user123 : 你能编辑代码以获得正确的输出吗?

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


【解决方案1】:
def download_pdf
  pdf = render_to_string(pdf: "test.pdf", template: "users/test.html.erb", encoding: "UTF-8", layout: 'application')
  send_data pdf ,:disposition => 'inline', filename: 'something.pdf', :type => 'application/pdf'
  save_path = Rails.root.join('pdfs','filename.pdf')
  File.open(save_path, 'wb') do |file|
    file << pdf
  end
end

application.html.erb

<%= stylesheet_link_tag "test"%>

【讨论】:

  • @ user123 :添加布局后抛出错误模板丢失缺少模板布局/application.css with {:locale=>[:en], :formats=>[:html], :handlers =>[:erb, :builder, :coffee]}。在以下位置搜索:* "C:/Site/generate4/app/views"'
  • 你应该替换你的css名称而不是application.css
  • 布局值只能是 application 而不是 application.css
  • 在application.html.erb中添加一行
猜你喜欢
  • 2011-04-04
  • 2011-08-06
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 2017-03-09
  • 2013-12-29
相关资源
最近更新 更多