我想在ActiveAdmin的应用程序中添加导出到PDF,但我不知道如何。有人有经验吗?我如何在Show中为任何订单添加简单的csv / pdf输出,但不是全部?
ActiveAdmin.register Order do
actions :index, :show, :edit, :destroy
index download_links: [:pdf, :xml] #only for all Orders, but bot pdf
show download_links: [:pdf, :xml] #This not work for any Order
index do
column :id
column :name
column :address
column :email
column :pay_type
column :created_at
actions
end
show do
panel "Order Information" do
table_for(order.line_items) do |t|
t.column("Product") {|item| auto_link item.product }
t.column("Quantity") {|item| item.quantity }
t.column("Price") {|item| number_to_currency item.product.price * item.quantity }
end
end
panel "User Information" do
table_for(order) do |t|
column "Name", :name
column "Address", :address
column "Email", :email
column "Pay Type", :pay_type
column "Created:", :created_at
end
end
end
sidebar "Total price",:only => :show do
number_to_currency order.line_items.to_a.sum { |item| item.total_price }
end
end
答案 0 :(得分:0)
我在prawn gem https://github.com/prawnpdf/prawn
的帮助下做了类似的事情我将一个模块定义为PdfReport
,我想为Profile
生成pdf。
pdf_report.rb
require 'open-uri'
module PdfReport
def autogenerate_report
p = self
pdf_file_name = p.transliterate("Report-#{p.name}-#{Date.today.to_s}")
pdf_file_path = "#{Rails.root}/tmp/#{pdf_file_name}.pdf"
Prawn::Document.generate(pdf_file_path, :top_margin => 0, :bottom_margin => 0) do
font "Helvetica"
repeat :all do
# header
bounding_box [bounds.left, bounds.top], :width => bounds.width, :height => 100 do
move_down(5)
所以我包括那个
class Profile < ActiveRecord::Base
include Shared::AttachmentHelper
include PdfReport
答案 1 :(得分:0)
虾宝石很棒。但我已经在Order.rb中向我的ActiveAdmin应用程序添加了简单的javascript代码。现在我可以使用浏览器保存和制作pdf。我知道,它不是一个Ruby解决方案,但它的工作和它在pdf中保存我需要的东西 - 产品的顺序和客户名称等等。 javascript:if(window.print)window.print()
在我找到易于集成到ActiveAdmin的东西之前,还只是临时解决方案。