wicked-pdf:错误:无法生成PDF

时间:2014-10-22 18:25:53

标签: ruby-on-rails-3 heroku bundler production-environment wicked-pdf

我正在尝试在我们的应用程序上创建pdf订单收据(在Heroku上)。我正在运行PDF生成器作为延迟作业。 我有: - 在应用程序的根目录中创建了一个bin文件夹 - 将二进制文件(x86-64)添加到该文件夹 -added wicked pdf gem to my gemfile

我正在我们的暂存应用上测试它。

这是我得到的错误:

Error: Failed to execute: ["/app/vendor/bundle/bin/wkhtmltopdf", "-q", "--page-size", "Letter", "file:///tmp/wicked_pdf20141022-213-3dpi58.html", "/tmp/wicked_pdf_generated_file20141022-213-1tqzjod.pdf"] Error: PDF could not be generated! Command Error: /app/vendor/bundle/ruby/2.0.0/gems/bundler-1.6.3/lib/bundler/rubygems_integration.rb:305:in 'block in replace_bin_path': undefined method name' for nil:NilClass (NoMethodError) from /app/vendor/bundle/bin/wkhtmltopdf:16:in

有关可能发生的事情的任何想法?我有点茫然。

CODE:

#config/initializers/wicked-pdf.rb

WickedPdf.config do |config|  
  if Rails.env == 'production' then
   config.exe_path = Rails.root.to_s + "/bin/wkhtmltopdf-amd64"
  else 
   if /darwin/ =~ RUBY_PLATFORM then
      config.exe_path = '/usr/local/bin/wkhtmltopdf' 
    else
      raise "UnableToLocateWkhtmltopdf"
    end
  end
end 

我打算在我的订单控制器中排队作业

#jobs/pdf_receipt_job

class PdfReceiptJob < Struct.new(:order_id)

def perform

    order = Order.find(order_id)

    # create an instance of ActionView, so we can use the render method outside of a controller
    view = ActionView::Base.new(ActionController::Base.view_paths, {})
    html_template = view.render(file: "order/receipt_attachment.html.erb", locals:{order:order})

    # use wicked_pdf gem to create PDF from the HTML receipt template
    pdf_receipt = WickedPdf.new.pdf_from_string(html_template, :page_size => 'Letter')

    # save PDF to disk. Later, to be stored in fog, sent to S3, than saved back to our DB
    pdf_path = Rails.root.join("tmp", "marmoset-receipt#{order.id}.pdf") #ex: will be at tmp/marmoset-receipt215.pdf

    File.open(pdf_path, 'wb') do |file|
      file << pdf_receipt
    end

    end
end

收据模板(成为PDF收据):

#views/order/receipt_attachment.html.erb

<!DOCTYPE html>

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-Language" content="en-us" />
        <title>Order Receipt from Marmoset</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>

    <body>

      <div style="width: 75%">
        <header>
          <div style="display: inline; width:100%">
            <img src="http://stillmotionblog.com/wp-content/uploads/2013/08/RGB-marmoset-seal-purple.png" alt="Marmoset" style="height: 200px; padding:20; float:left">
            <h3 style="font-family: Helvetica; float:right; color:gray;">Order Number: <%= order.id %></br>
              Charge Key: <%= order.stripe_charge_key %>
            </h3>

          </div>
        </header>
        <div style="margin: 0 auto">
          <table cellpadding="5" cellspacing="2" summary="Order Receipt" width="100%" style="font-family: Georgia;">
            <thead>
              <tr>
                <th bgcolor="#8A4FAB" style="color:white" scope="col">Track</th>
                <th bgcolor="#8A4FAB" style="color:white" scope="col">License</th>
                <th bgcolor="#8A4FAB" style="color:white" scope="col">Price</th>
              </tr>
            </thead>

            <tbody style="font-family: Georgia;">
              <% order.line_items.each do |line_item| %>
                <tr>
                  <td bgcolor="#EBEBEB" style="font-family: Georgia; color: #5C5C5C"><%= line_item.track.display_name %></td>
                  <td bgcolor="#EBEBEB" style="font-family: Georgia; color: #5C5C5C"><%= line_item.license.display_name %></td>
                  <td bgcolor="#EBEBEB" style="font-family: Georgia; color: #5C5C5C"><%= number_to_currency line_item.price %></td>
                </tr>
              <% end %>
                <tr>
                  <td colspan="2" style="text-align: right;font-family: Georgia; color: #5C5C5C">Subtotal</td>
                  <td style="font-family: Georgia; color: #5C5C5C"><%= number_to_currency order.price %></td>
                </tr>
              <% if order.discount > 0 %>
                <tr>
                  <td colspan="2" style="text-align: right;font-family: Georgia; color: #5C5C5C">Discount</td>
                  <td style="font-family: Georgia; color: #5C5C5C">-<%= number_to_currency order.discount %></td>
                </tr>
              <% end %>


              <tr>
                <td colspan="2" style="text-align: right;font-family: Georgia; color: #5C5C5C ">Total</td>
                <td style="font-family: Georgia; color: #5C5C5C"><%= number_to_currency order.total %></td>
              </tr>
            </tbody>
          </table>
        </div>

        <hr style="margin: 30px 0px 30px 0px">
        <p style="font-family: Georgia; color: #5C5C5C">Licensing information for your order:</p>
        <ul style="font-family: Georgia; color: #5C5C5C">

          <% order.line_items.map{|l| l.license}.uniq.each do |license| %>
            <li><strong><%= license.name %> <%= "(#{license.range.html_safe})" if license.range.present? %>:</strong>
              <ul>
                <% if license.permitted_content.present? %>
                  <li>
                    <b>Permitted Content:</b>
                      <%= license.permitted_content.html_safe %>
                  </li>
                <% end %>
                <% if license.non_permitted_content.present? %>
                  <li>
                    <b>Non-Permitted Content:</b>
                       <%= license.non_permitted_content.html_safe %>
                  </li>
                <% end %>
                <% if license.number_of_uses.present? %>
                  <li>
                    <b>Number of Uses:</b>
                      <%= license.number_of_uses %>
                  </li>
                <% end %>
                <% if license.distribution.present? %> 
                  <li>
                    <b>Distribution:</b>
                      <%= license.distribution %>
                  </li>
                <% end %>  
                <% if license.term.present? %>
                  <li>
                    <b>Lifespan:</b>
                      <%= license.term %>
                  </li>
                <% end %>
              </ul>
            </li>
          <% end %>
        </ul>

          <p style="font-family: Georgia; font-size: .9em; color:gray; font-style:italic; padding: 5px; margin-top:30px; text-align: center">
            Thank you for supporting our family of musicians with your purchase. Your purchase makes an impact on the lives of hard working, blue collar artists. We hope you enjoy the music and we can't wait to see what stories you tell. Give us a shout at <a style="text-decoration: none" href="mailto:compass@marmosetmusic.com" target="_blank"><span style="color:#8A4FAB; font-style: normal">compass@marmosetmusic.com</span></a> to share your masterpiece when it's finished.</p>
      </div>

    </body>
    </html>

编辑:如果我在我的终端中运行wkhtmltopdf www.marmosetmusic.com myhomepage.pdf,它在本地运行,如果我登录到我的heroku应用程序的bash并运行它,我会得到同样的错误/app/vendor/bundle/ruby/2.0.0/gems/bundler-1.6.3/lib/bundler/rubygems_integration.rb:305:in 'block in replace_bin_path': undefined method 'name' for nil:NilClass (NoMethodError) from /app/vendor/bundle/bin/wkhtmltopdf:16:in&#39;`

0 个答案:

没有答案