不能使用Rails 3包含wicked_pdf的样式表文件

时间:2015-05-28 05:50:32

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

我想在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>

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

1 个答案:

答案 0 :(得分:0)

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