【问题标题】:Wicked_PDF not rendering correct templateWicked_PDF 未呈现正确的模板
【发布时间】:2018-10-15 21:58:56
【问题描述】:

您好,我有一份撰写提案的申请。我正在尝试设置 wicked_pdf 来生成 pdf,并且由于某种原因它没有使用正确的模板。它使用我的应用程序模板,而不是我的 pdf 模板。

app/controllers/proposals_controller.rb

def show
    @proposal = Proposal.find(params[:id])
    @custom_content = @proposal.custom_contents
    respond_to do |format|
      format.html
      format.pdf do
        render pdf: "some_name",
               template: "proposals/show.pdf.erb",
               layout: "pdf.html",
               margin: { top: 35, bottom: 35 }
      end
    end
  end

app/views/layouts/pdf.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>PDF</title>
  <%= wicked_pdf_stylesheet_link_tag "application" -%>
</head>
<body>
<div class='container d-flex justify-content-center'>
  test 2
  <%= yield %>
</div>
</body>
</html>

app/views/proposals/show.pdf.erb

<div class="row full-page justify-content-center" id="cover">
  <div class="container">
    <div class="row p-0">
      <div class="col-12 d-flex align-items-end ">
        <%= wicked_pdf_image_tag '1209232_1920-1.jpg' %>
      </div>
    </div>
    <div class="row pt-1 d-flex align-items-end text-justify">
      <div class="col-12 text-justify cover-background">
        <div class="row pt-1">
          <h2 class="text-white text-justify">Proposal For <br> Maintenance</h2>
        </div>
        <div class="row p-0">
          <div class="col-6">
            <h5 class="text-white text-justify">Prepared For: <%= @proposal.contact.name %></h5>
            <h6></h6>
          </div>
          <div class="col-6">
            <h5 class="text-white text-justify">Prepared By: <%= @proposal.user.name %></h5>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

config/initializers/wicked_pdf.rb

# WickedPDF Global Configuration
#
# Use this to set up shared configuration options for your entire application.
# Any of the configuration options shown here can also be applied to single
# models by passing arguments to the `render :pdf` call.
#
# To learn more, check out the README:
#
# https://github.com/mileszs/wicked_pdf/blob/master/README.md

WickedPdf.config = {
    }

config/initializers/mime_types.rb

# Be sure to restart your server when you modify this file.

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
Mime::Type.register "application/pdf", :pdf

我觉得我看了这么久,我可能错过了明显的东西。我什至运行了一个示例应用程序进行测试......并且没有任何问题。

我跟随这个很棒的教程 - https://medium.com/@yazed.jamal/using-wicked-pdf-with-rails-5-3d1a4b0a09ba

非常感谢任何帮助

另外需要注意的是,创建的文件不是“some_name”而是id .pdf

我的按钮位于我的 app/views/proposals/_proposal-cover.html.erb 这是我的 app/views/proposals/show.html.erb 中的一部分

<%= link_to 'Download', proposal_path(@proposal, :format => :pdf), class: 'btn btn-default'  %>

【问题讨论】:

  • 观看此视频,了解如何使用 gem 'prawn' youtube.com/watch?v=BW5zwqj37Lo&t=625s 将 Ruby html 转换为 pdf 文件
  • @nourza 你认为放弃 wicked_pdf?
  • 不确定,但我正在查看 github 页面。你安装了这个 gem gem 'wkhtmltopdf-binary' 吗?
  • @nourza 确实做到了
  • 我认为是因为proposals_controller.rb中的渲染在这里def show respond_to do |format| format.html format.pdf do render pdf: "file_name" # 不包括 ".pdf" 扩展名。结束结束结束

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


【解决方案1】:

我要感谢@dollarchills 和@nourza。两者都对这个有很大帮助。

原来是其他开发者安装了以下

如果您想让 WickedPdf 自动生成 PDF 视图 对于所有(或几乎所有)页面,通过将 .pdf 附加到 URL,添加 跟随您的 Rails 应用程序:

在 application.rb (Rails3) 或 environment.rb (Rails2) 中

require 'wicked_pdf'
config.middleware.use WickedPdf::Middleware

覆盖其他所有内容

【讨论】:

    【解决方案2】:

    试试下面的。我在我的应用程序中使用了 wicked,这个结构对我有用,希望对你有用。

    显示

    <%= link_to 'Download', proposal_path(format: 'pdf'), class: 'btn btn-default'  %>
    

    控制器

    format.pdf do
     render pdf: "some_name",
      template: "proposals/show",
      margin: { top: 35, bottom: 35 }
    end
    

    【讨论】:

      猜你喜欢
      • 2011-09-01
      • 2015-04-01
      • 2015-06-18
      • 1970-01-01
      • 2017-08-28
      • 2010-10-02
      • 2020-09-06
      • 2012-03-23
      • 2014-04-23
      相关资源
      最近更新 更多