You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails ActiveAdmin PDF导出功能故障排查与WickedPdf集成求助

Hey Carlos, let's get that PDF download functionality working again—here's a step-by-step guide tailored to your setup:

1. Lock Down Dependencies First

Make sure your Gemfile has the right gems, and you've installed the required system tool for generating PDFs:

# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary' # Automatically installs wkhtmltopdf (perfect for dev environments)

Run bundle install to update your gems. Then install wkhtmltopdf if you haven’t already:

  • macOS: brew install wkhtmltopdf
  • Ubuntu/Debian: sudo apt-get install wkhtmltopdf
  • Windows: Download the official installer and add it to your system PATH.

2. Add a Custom PDF Action to ActiveAdmin

Since you’re working within ActiveAdmin, it’s easiest to add a custom member action directly to your app/admin/documents.rb file. This keeps all admin-related logic in one place and uses ActiveAdmin’s built-in authentication.

Update your ActiveAdmin.register Document block with this:

ActiveAdmin.register Document do
  # Add this custom action above your index block
  member_action :download_pdf, method: :get do
    document = Document.find(params[:id])
    
    # Your generate_pdf logic (with fixes for edge cases)
    pdf = WickedPdf.new.pdf_from_string(
      document.content,
      encoding: 'UTF-8',
      page_size: 'A4',
      orientation: 'Portrait',
      template: 'documents/documento.pdf.rb', # Double-check this file exists!
      margin: { top: 30, bottom: 30, left: 20, right: 20 },
      layout: 'layouts/pdf.html' # Verify this layout file exists too
    )

    send_data(
      pdf,
      filename: "document_#{document.type_document}_#{document.user&.name || 'anonymous'}.pdf",
      disposition: 'attachment',
      type: 'application/pdf' # Ensures browsers recognize it as a PDF
    )
  end

  # ... keep your existing index, form, csv blocks here ...

  # Update the download column to use the new action
  index do
    # ... your existing columns (id, user_name, etc.) ...
    column :download do |document|
      link_to 'Download PDF', download_pdf_admin_document_path(document)
    end
    default_actions
  end

  # ... rest of your existing code ...
end

Key fixes here:

  • Added user&.name || 'anonymous' to avoid errors if a document has no associated user
  • Added type: 'application/pdf' to ensure proper browser handling of the file
  • Switched to ActiveAdmin’s custom route (download_pdf_admin_document_path) instead of the front-end document_path

3. Verify Template & Layout Files

WickedPdf relies on a template and layout to render the PDF correctly. Double-check these files exist in your project:

  • app/views/documents/documento.pdf.rb (or .erb if you use ERB templates—this is more common)
  • app/views/layouts/pdf.html.erb

Your pdf.html.erb layout should look something like this (add styles if you want formatted PDFs):

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Document PDF</title>
  <%= wicked_pdf_stylesheet_link_tag 'application' %> <!-- Include CSS for styling -->
</head>
<body>
  <%= yield %> <!-- Renders content from your documento.pdf template -->
</body>
</html>

4. Test It Out

Restart your Rails server, navigate to the ActiveAdmin Documents index, and click the "Download PDF" link. If you run into issues:

  • Template not found: Confirm the path in the template parameter matches the actual file location (Rails looks in app/views/ by default).
  • Blank PDF: Test with static content (replace document.content with <h1>Test PDF</h1>) to rule out issues with the document’s content.
  • Permission errors: Since we’re using ActiveAdmin’s member action, you’re already authenticated—this shouldn’t be a problem, but if you used a front-end controller, you’d need to add authentication checks.

That should get your PDF download working again! Let me know if you hit any snags along the way.

内容的提问来源于stack exchange,提问作者Carlos Sosa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:15:00