Rails wicked_pdf,将页面转换为 pdf
Rails wicked_pdf, convert page as pdf
我有一个正常的 Rails 布局,这是我的正常站点。
我想要这个页面上的一个按钮 convert/transform 这个页面进入
一个 PDF 文件。
我想用这个 gem:
https://github.com/mileszs/wicked_pdf
我在我的 Gemfile 中添加了 gem 'wicked_pdf'
,但现在我该如何转换实际页面?
这是我使用 wicked_pdf
:
所做的
# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary' # if you need the binary too
然后运行
$ bundle install
$ (bundle exec) rails g wicked_pdf # generates the initializer
然后您可以在 config/initializers/wicked_pdf.rb
中配置全局设置,如 github 上的邪恶自述文件所述。
Rails 4 已经知道 pdf mime 类型,但在旧版本中,您可能需要将 pdf
注册为 mime 类型:
# config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
在你的控制器中
respond_to do |format|
format.html
format.pdf do
render pdf: "your-filename"
end
end
然后您在 app/views/<your-view>/<controller-action>.pdf.erb
下放置一个包含您不想要的内容的视图。
要通过 link 引用此内容,您可以执行以下操作:
= link_to "Get PDF", your_path(format: :pdf)
这是否解决了您的问题?
请按照以下link。这是一个详细教程,这将使您的生活更轻松:
http://dchua.com/2014/10/30/generate-pdfs-with-html-templates-in-rails/
备注
如果您不想单独拥有您的 pdf 视图并希望使用相同的 html 视图,那么请使用
template: invoices/show.html.erb
而不是
template: invoices/show.pdf.erb
我有一个正常的 Rails 布局,这是我的正常站点。
我想要这个页面上的一个按钮 convert/transform 这个页面进入 一个 PDF 文件。
我想用这个 gem: https://github.com/mileszs/wicked_pdf
我在我的 Gemfile 中添加了 gem 'wicked_pdf'
,但现在我该如何转换实际页面?
这是我使用 wicked_pdf
:
# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary' # if you need the binary too
然后运行
$ bundle install
$ (bundle exec) rails g wicked_pdf # generates the initializer
然后您可以在 config/initializers/wicked_pdf.rb
中配置全局设置,如 github 上的邪恶自述文件所述。
Rails 4 已经知道 pdf mime 类型,但在旧版本中,您可能需要将 pdf
注册为 mime 类型:
# config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
在你的控制器中
respond_to do |format|
format.html
format.pdf do
render pdf: "your-filename"
end
end
然后您在 app/views/<your-view>/<controller-action>.pdf.erb
下放置一个包含您不想要的内容的视图。
要通过 link 引用此内容,您可以执行以下操作:
= link_to "Get PDF", your_path(format: :pdf)
这是否解决了您的问题?
请按照以下link。这是一个详细教程,这将使您的生活更轻松:
http://dchua.com/2014/10/30/generate-pdfs-with-html-templates-in-rails/
备注
如果您不想单独拥有您的 pdf 视图并希望使用相同的 html 视图,那么请使用
template: invoices/show.html.erb
而不是
template: invoices/show.pdf.erb