【问题标题】:Rails PDFKit yields ActionController:UnknownFormat only on HerokuRails PDFKit 仅在 Heroku 上产生 ActionController:UnknownFormat
【发布时间】:2019-02-28 06:23:27
【问题描述】:

所以我正在尝试开发一个应用程序,让您可以将产品转换为带有二维码的标签。使用 gem PDFKit 和 wkhtmltopdf 它可以在本地正常工作。但是在 Heroku 上它给我一个 HTTP 406 和以下 rails 错误:ActionController::UnknownFormat (OrderController#print is missing a template for this request format and variant. request.formats: ["application/pdf"] request.variants: [])。我搜索了论坛,但似乎找不到遇到同样问题的人。我还按照 PDFKit github 上的指南了解 heroku 的使用,link to guide。这意味着我同时拥有 wkhtmltopdf-binary gem 和 wkhtmltopdf-heroku gem。

相关代码sn-ps:

#in app/views/order/show.html.erb
<%= link_to "Print QR-code", print_order_path(@order, :format => :pdf), class 'btn btn-primary' %>



#in app/config/initializers/pdfkit.rb
PDFKit.configure do  |config|
if File.executable? 'app/.apt/usr/local/bin/wkhtmltopdf'
   config.wkhtmltopdf = 'app/.apt/usr/local/bin/wkhtmltopdf'
end


#app/controllers/order_controller
def print
@order = Order.find(params[:id])
end

#app/views/order/print.html.erb
<div style="display: inline-block">
   <table class="QR" style="width:100%">
       <tr style="margin:30px">
           <td class="QR">
               <% @qr = RQRCode::QRCode.new(@order.qr_code.to_s, :size => 6)%>
               <%= raw @qr.as_svg(offset:0, color: '000',shape_rendering: 'crispEdges', module_size:4) %>
           </td>
           <td style="font-size:50px;padding:20px; text-align:center">
               <%=@order.garment.to_s%><br>
               <%=@order.size.to_s%>
           </td>
        </tr>
   </table>
</div>

使用 srng 提供的 respond_to 添加块后,我现在收到以下错误:

ActionView::MissingTemplate(Missing template order/print, application/print with {:locale =>[:sv], :formats =>[:pdf], :variants => [], :handlers => [:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}

【问题讨论】:

  • wkhtmltopdf-heroku 推荐not touching the binary path。这有点矛盾,但也许可以尝试注释掉 PDFKit 配置?
  • 我把它注释掉了,但它仍然产生同样的错误。我在想这可能是一个错字,但这没有意义,因为它可以在本地服务器上运行。我对 rails 不够精通,无法准确确定发生 unknownformat 错误时发生的情况。我假设它正在尝试解析 .pdf 但由于 wkhtmltopdf 无法正常工作而没有得到解析?
  • 你能发布你的控制器方法吗?在处理文件下载时,我经常遇到 406。
  • 添加到帖子@srng

标签: ruby-on-rails heroku wkhtmltopdf pdfkit


【解决方案1】:

我相信您缺少格式块。这就是它要寻找的东西。

您需要在 Order.rb #print 方法中添加一个块;

// order_controller.rb 
def print
    @order = Order.find(params[:id])
    respond_to do |format|
        # format.html
  format.pdf do
    send_data PDFKit.new( render_to_string 'print', order: @order ).to_pdf
        end
    end
end

编辑:天哪,这很有趣。

1- 将名为 print.pdf.erb 的文件添加到您的视图/订单文件夹中 2- 内容与 print.html.erb 相同 2-使用上面的格式块,我已经将html格式注释掉了

EDIT2:鉴于它在本地和 Heroku 上运行,我将在这里复制/粘贴所有代码以进行直接比较。我没有qr类,属性不一样,不过你可以理解。

//orders_controller.rb
def print
    @order = Order.find(params[:order_id])
    respond_to do |format|
      format.html
      format.pdf do
        send_data PDFKit.new( render_to_string 'print', order: @order ).to_pdf
      end
    end
  end

  # GET /orders
  # GET /orders.json
  def index
    @orders = Order.all
  end

  # GET /orders/1
  # GET /orders/1.json
  def show
  end
....

//views/orders/show.html.erb
<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @order.name %>
</p>

<p>
  <strong>Thing:</strong>
  <%= @order.thing %>
</p>
<%= link_to 'Print', order_print_path(@order, :format => :pdf) %> |

<%= link_to 'Edit', edit_order_path(@order) %> |
<%= link_to 'Back', orders_path %>

//views/order/print.html.erb
<div style="display: inline-block">
  <table class="QR" style="width:100%">
    <tr style="margin:30px">
      <td class="QR">

      </td>
      <td style="font-size:50px;padding:20px; text-align:center">
        <%=@order.name%><br>
        <%=@order.thing%>
      </td>
    </tr>
  </table>
</div>

//views/orders/print.pdf.erb
<div style="display: inline-block">
  <table class="QR" style="width:100%">
    <tr style="margin:30px">
      <td class="QR">

      </td>
      <td style="font-size:50px;padding:20px; text-align:center">
        <%=@order.name%><br>
        <%=@order.thing%>
      </td>
    </tr>
  </table>
</div>

//Gemfile
gem 'pdfkit'
gem 'wkhtmltopdf-heroku'

//config/routes.rb
resources :orders do
    get 'print', to: 'orders#print'
  end

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
相关资源
最近更新 更多