wicked_pdf,无法呈现pdf

时间:2018-09-22 03:11:26

标签: ruby-on-rails wicked-pdf

我尝试使用wicked_pdf生成pdf,尽管出现错误“无法渲染模板”。我已遵循wicked_pdf指南,但遇到了麻烦。任何帮助表示赞赏。

url:

http://localhost:3000/app/letters/1.pdf

app / controllers / app / letters_controller.rb:

  def show
    respond_to do |format|
   format.html
   format.pdf do
     render pdf: "Letters"   # Excluding ".pdf" extension.
    end
    end
  end

显示页面链接:

<li><a href="<%= app_letter_path(@letter, format: :pdf) %>" target="_blank"> Print</a></li>

app / views / app / letters / show.pdf:

<!doctype html>
<html>
  <head>
  </head>
  <body>
    <div>
      <p>test</p>
    </div>
  </body>
</html>

宝石文件:

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'

错误:

无法加载PDF文档。

1 个答案:

答案 0 :(得分:0)

Wicked PDF在此处签出可用选项

  1. views/layouts/pdf.html.erb中为pdf文档创建布局
  <!DOCTYPE html>
  <html>
    <head>
      <title>My PDF</title>
      <%#= wicked_pdf_stylesheet_link_tag "style" -%>
    </head>
    <body>
      <div class='my_container'>
        <%= yield %>
      </div>
    </body>
  </html>

2-在控制器中(您必须添加选项disposition: 'inline'才能在新标签页中打开

  def show
    respond_to do |format|
      format.pdf do
        render pdf: "show",
        disposition: 'inline',
        stream: false,
        layout: 'layouts/pdf.html.erb'
      end
    end
  end

3-Pdf html文档名称应以.erb结尾,即app/views/app/letters/show.pdf.erb

<div>
  <p>test</p>
</div>