Active Admin查看为PDF [英] Active Admin view to PDF
问题描述
我最近开始在Ruby on Rails中进行开发,因此我认为自己是新手。
I'm starting to develop in Ruby on Rails recently, therefore I consider myself beginner.
我正在测试Active Admin gem( http://activeadmin.info/ ),将视图导出到PDF时发现了问题。
I'm testing the Active Admin gem ( http://activeadmin.info/ ) and I found a problem when export a view to PDF.
在官方文档中说明了如何自定义下载链接( http:// activeadmin.info/docs/3-index-pages.html ,位于文档末尾),但对我不起作用。
In the official documentation says how to customize the download links (http://activeadmin.info/docs/3-index-pages.html, it is at the end of the document) but does not work for me.
何时我输入了以下代码行:
When I put the following line of code:
Mymodel ActiveAdmin.register do
index: download_links => [: pdf]
end
结果是指向localhost:3000 / admin /的链接mymodel.pdf并显示以下错误:无法加载PDF文档。
The result is a link to localhost:3000/admin/mymodel.pdf and the following error displays: "Failed to load PDF document".
两个问题:
- 您有什么建议?
- 您能举个例子吗?
我将非常感激。
推荐答案
这是一阵子,但我要添加一个示例对于将来的搜索者来说,ActiveAdmin更具针对性,因为@ user2163649的答案是一种更通用的方法。
This was questioned for a while ago, but I'm adding an example that is a little more specific to ActiveAdmin for the future searchers as @user2163649's answer is a more general approach.
首先,我们需要弄清楚ActiveAdmin不支持PDF导出的盒子。您需要自己实现它。
First we need to make it clear that ActiveAdmin does not support PDF export out of the box. You need to implement it yourself.
我选择了WickedPDF gem来创建PDF,但是您可以从其他一些选择,例如虾,Pdfkit等,根据您的需求和限制。
I picked up WickedPDF gem for PDF creation, but you can choose from several other options such as Prawn, Pdfkit, etc according to your needs and limits.
# Gemfile
gem 'wicked_pdf'
# app/admin/pages.rb
ActiveAdmin.register Page do
controller do
# if you want /admin/pages/12345.pdf
def show
super do |format|
format.pdf { render(pdf: "page-#{resource.id}.pdf") }
end
end
end
# if you want /admin/pages/12345/pdf, pdf_admin_page_path(@page)
member_action :pdf, method: :get do
render(pdf: "page-#{resource.id}.pdf")
end
end
# app/views/admin/order_items/show.pdf.erb
<h1>page <%= resource.id %></h1>
<p><%= resource.body %></p>
# app/views/admin/order_items/show.html.erb
<h1>page <%= resource.id %></h1>
<p><%= resource.body %></p>
这篇关于Active Admin查看为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!