简体   繁体   English

无法在rails中生成带有wicked_pdf gem的pdf?

[英]Unable to generate the pdf with wicked_pdf gem in rails?

I am trying to generate a pdf by using wicked_pdf gem in my rails Application. 我正在尝试通过在rails应用程序中使用wicked_pdf gem生成pdf i am having following code in my files. 我的文件中包含以下代码。

gemfile

 gem 'wicked_pdf'
 gem 'wkhtmltopdf-binary'

and in config/initializers/wicked_pdf.rb file 并在config / initializers / wicked_pdf.rb文件中

WickedPdf.config = {
  # Path to the wkhtmltopdf executable: This usually isn't needed if using
  # one of the wkhtmltopdf-binary family of gems.
  # exe_path: '/usr/local/bin/wkhtmltopdf',
  #   or
  # exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')

  # Layout file to be used for all PDFs
  # (but can be overridden in `render :pdf` calls)
  # layout: 'pdf.html',
}
  module WickedPdfHelper
  if Rails.env.development?
    if RbConfig::CONFIG['host_os'] =~ /linux/
      executable = RbConfig::CONFIG['host_cpu'] == 'x86_64' ?
'wkhtmltopdf_linux_x64' : 'wkhtmltopdf_linux_386'
    elsif RbConfig::CONFIG['host_os'] =~ /darwin/
      executable = 'wkhtmltopdf_darwin_386'
    else
      raise 'Invalid platform. Must be running linux or intel-based Mac OS.'
    end

    WickedPdf.config = { exe_path:
"#{Gem.bin_path('wkhtmltopdf-binary').match(/(.+)\/.+/).captures.first}/#{executable}"
}
  end
end

and in controller 并在控制器中

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

in /config/initializers/mime_types.rb /config/initializers/mime_types.rb

Mime::Type.register "application/xls", :xls
Mime::Type.register "application/xlsx", :xlsx
Mime::Type.register "application/pdf", :pdf unless Mime::Type.lookup_by_extension(:pdf)

and in the file views/invoises/show.pdf.erb 并在文件views/invoises/show.pdf.erb

    <p>
      Invoice No:
      <%= @invoise.invoice_no %>
    </p>

  <p>
    Due date:
    <%= @invoise.due_date %>
  </p>

  <p>
    Total Amount:
    <%= @invoise.total_amount %>
  </p>

and the url i am clicking in the browser is /invoises/BRUqWOeEVNSN6GCwxQqLGg%253D%253D.pdf 我在浏览器中单击的URL是/invoises/BRUqWOeEVNSN6GCwxQqLGg%253D%253D.pdf

Iam unable to generate pdf file. 我无法生成pdf文件。 And i am not getting any error also. 而且我也没有任何错误。 when i click the above url my webpage is keep on loading. 当我单击上面的URL时,我的网页正在继续加载。 i am not getting any output. 我没有任何输出。

You can do in this way (example based on RailsCasts series): 您可以通过这种方式进行操作(基于RailsCasts系列的示例):

environment.rb 的environment.rb

require 'pdf/writer'
Mime::Type.register 'application/pdf', :pdf

products_controller.rb products_controller.rb

def index
  @products = Product.find(:all)
  respond_to do |format|
    format.html
    format.pdf do
      send_data ProductDrawer.draw(@products), filename: 'products.pdf', type: 'application/pdf', disposition: 'inline'
    end
  end
end

product_drawer.rb product_drawer.rb

def self.draw(products)
  pdf = PDF::Writer.new
  products.each do |product|
    pdf.text product.name
  end
  pdf.render
end

views/products/index.html.erb views / products / index.html.erb

<p><%= link_to 'PDF Format', formatted_products_path(:pdf) %></p>

I think this is a better way to implement this feature. 我认为这是实现此功能的更好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM