veepsk
veepsk

Reputation: 1823

ActiveAdmin export as {PDF, XML} custom links for each page

I am using Rails 3.2.8 with activeadmin. The problem I am facing is if I set up custom links for exporting as pdf(using Prawn gem) or xml as,

ActiveAdmin.register Project do
  index :download_links => [:pdf, :xml]
  controller do
    ...
  end

  index do
    column "Icon" do |project|
    # And a couple of other columns.
  end
end

I end up losing all the custom columns(e.g. Icon) on the page.

I only want the export as PDF option on current page and hence I did not set up the global activeadmin initializer with download links for XML and PDF.

So, could anyone help me figure out how I could have the download link for pdf only in my current activeadmin page.

Upvotes: 0

Views: 2124

Answers (1)

veepsk
veepsk

Reputation: 1823

@sanghyun-park This is my activeadmin controller,

  controller do
    def index
      index! do |format|
        format.html
        format.pdf {
         pdf = ProjectPdf.new(@projects)
         send_data pdf.render, filename: "Project_status.pdf",
                               disposition: "inline"
        }
      end
    end
  end

And this is the model I have to render PDF documents,

class ProjectPdf < Prawn::Document
    def initialize(projects)
        super(top_margin: 50)
        text "Summary of Projects" , :style => :bold, :size => 20
        proj_array = [[ "Company Name" , "Project Name", "Project State", 
     "Latest Upload"]]
        projects.each do |item|
              proj_array << [ 
                item.company.name.nil? ? "" : item.company.name,
                item.name,
                item.project_state.nil? ? "" : item.project_state.name, 
                item.updated_at.strftime("%m/%y/%d \n %l:%M %p")
              ]
        end
    end

Your approach looks more concise and easier for exporting page to PDF in activeadmin.

Upvotes: 2

Related Questions